#include "c3/c3driver_context.h"
#include "c3/c3stl.h"
+#include <cairo/cairo.h>
+
int _w = 800, _h = 600;
c3cam cam;
c3arcball arcball;
c3context_p c3;
+c3context_p hud;
c3object_p head;
extern reprap_t reprap;
}
}
+
static void
-_c3_geometry_prepare(
+_c3_geometry_project(
c3context_p c,
- const struct c3driver_context_t *d,
- c3geometry_p g)
+ const struct c3driver_context_t * d,
+ c3geometry_p g,
+ c3mat4p m)
{
switch(g->type.type) {
case C3_TRIANGLE_TYPE: {
c3texture_p t = (c3texture_p)g;
g->type.subtype = GL_TRIANGLE_FAN;
g->mat.color = c3vec4f(0.0, 1.0, 0.0, 0.5);
- printf("_c3_geometry_prepare xrure %d!\n", g->textures.count);
+ printf("_c3_geometry_project xrure %d!\n", g->textures.count);
if (!g->texture) {
GLuint texID = 0;
dumpError("cp_gl_texture_load_argb flush");
- glEnable(GL_TEXTURE_RECTANGLE_ARB);
+ if (g->mat.mode == 0)
+ g->mat.mode = GL_TEXTURE_RECTANGLE_ARB;
+
+ printf("C3_TEXTURE_TYPE %d\n",g->mat.mode);
+ glEnable(g->mat.mode);
dumpError("cp_gl_texture_load_argb GL_TEXTURE_RECTANGLE_ARB");
glGenTextures(1, &texID);
dumpError("cp_gl_texture_load_argb glBindTexture GL_TEXTURE_RECTANGLE_ARB");
- glPixelStorei(GL_UNPACK_ROW_LENGTH, t->pixels.row / 4);
- glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+// glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
+// GL_MODULATE ); //set texture environment parameters
+ dumpError("glTexEnvf");
+
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, t->pixels.row / t->pixels.psize);
+ glTexParameteri(g->mat.mode, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ dumpError("GL_TEXTURE_MAG_FILTER");
+ glTexParameteri(g->mat.mode, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ dumpError("GL_TEXTURE_MIN_FILTER");
+ glTexParameteri(g->mat.mode, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
+ dumpError("GL_TEXTURE_WRAP_S");
+ glTexParameteri(g->mat.mode, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+ dumpError("GL_TEXTURE_WRAP_T");
+ glTexParameteri(g->mat.mode, GL_GENERATE_MIPMAP, GL_TRUE);
+ dumpError("GL_GENERATE_MIPMAP");
+ glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
+ dumpError("GL_GENERATE_MIPMAP_HINT");
g->mat.texture = texID;
g->texture = 1;
}
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g->mat.texture);
- glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8,
+ glBindTexture(g->mat.mode, g->mat.texture);
+ dumpError("glBindTexture");
+ glTexImage2D(g->mat.mode, 0,
+ t->pixels.format == C3PIXEL_A ? GL_ALPHA8 : GL_RGBA8,
t->pixels.w, t->pixels.h, 0,
- GL_RGBA, GL_UNSIGNED_BYTE,
+ t->pixels.format == C3PIXEL_A ? GL_ALPHA : GL_BGRA,
+ GL_UNSIGNED_BYTE,
t->pixels.base);
+ dumpError("glTexImage2D");
+ glGenerateMipmap(GL_TEXTURE_2D);
+ dumpError("glGenerateMipmap");
+
+ } break;
+ case C3_LINES_TYPE: {
+ // glLineWidth(1);
+ float lineWidth = 0.2;
+
+ c3vertex_array_p v = &g->projected;
+ c3tex_array_p tex = &g->textures;
+ c3tex_array_clear(tex);
+ c3vertex_array_clear(v);
+ for (int l = 0; l < g->vertice.count; l += 2) {
+ c3vec3 a = c3mat4_mulv3(m, g->vertice.e[l]);
+ c3vec3 b = c3mat4_mulv3(m, g->vertice.e[l+1]);
+
+ c3vec3 e = c3vec3_mulf(c3vec3_normalize(c3vec3_sub(b, a)), lineWidth);
+
+ c3vec3 N = c3vec3f(-e.y, e.x, 0);
+ c3vec3 S = c3vec3_minus(N);
+ c3vec3 NE = c3vec3_add(N, e);
+ c3vec3 NW = c3vec3_sub(N, e);
+ c3vec3 SW = c3vec3_minus(NE);
+ c3vec3 SE = c3vec3_minus(NW);
+#if 0
+ c3vertex_array_add(v, c3vec3_add(a, SW));
+ c3vertex_array_add(v, c3vec3_add(a, NW));
+ c3vertex_array_add(v, c3vec3_add(a, S));
+ c3vertex_array_add(v, c3vec3_add(a, N));
+ c3vertex_array_add(v, c3vec3_add(b, S));
+ c3vertex_array_add(v, c3vec3_add(b, N));
+ c3vertex_array_add(v, c3vec3_add(b, SE));
+ c3vertex_array_add(v, c3vec3_add(b, NE));
+#endif
+
+ const float ts = 1;
+
+ c3vertex_array_add(v, c3vec3_add(a, SW));
+ c3vertex_array_add(v, c3vec3_add(a, S));
+ c3vertex_array_add(v, c3vec3_add(a, NW));
+ c3tex_array_add(tex, c3vec2f(ts * 0 , ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0 , ts * 1 ));
+
+ c3vertex_array_add(v, c3vec3_add(a, S));
+ c3vertex_array_add(v, c3vec3_add(a, N));
+ c3vertex_array_add(v, c3vec3_add(a, NW));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0 , ts * 1 ));
+
+ c3vertex_array_add(v, c3vec3_add(a, N));
+ c3vertex_array_add(v, c3vec3_add(b, S));
+ c3vertex_array_add(v, c3vec3_add(b, N));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1 ));
+
+ c3vertex_array_add(v, c3vec3_add(a, N));
+ c3vertex_array_add(v, c3vec3_add(a, S));
+ c3vertex_array_add(v, c3vec3_add(b, S));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0 ));
+
+ c3vertex_array_add(v, c3vec3_add(b, N));
+ c3vertex_array_add(v, c3vec3_add(b, S));
+ c3vertex_array_add(v, c3vec3_add(b, SE));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1 ));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 1 , ts * 0 ));
+
+ c3vertex_array_add(v, c3vec3_add(b, N));
+ c3vertex_array_add(v, c3vec3_add(b, SE));
+ c3vertex_array_add(v, c3vec3_add(b, NE));
+ c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1 ));
+ c3tex_array_add(tex, c3vec2f(ts * 1 , ts * 0 ));
+ c3tex_array_add(tex, c3vec2f(ts * 1 , ts * 1 ));
+
+ }
+ g->type.subtype = GL_TRIANGLES;
} break;
default:
break;
c3geometry_p g )
{
glColor4fv(g->mat.color.n);
+ dumpError("glColor");
// glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, g->mat.color.n);
glVertexPointer(3, GL_FLOAT, 0,
g->projected.count ? g->projected.e : g->vertice.e);
glEnableClientState(GL_VERTEX_ARRAY);
+ dumpError("GL_VERTEX_ARRAY");
+ glDisable(GL_TEXTURE_2D);
if (g->textures.count && g->texture) {
- glDisable(GL_TEXTURE_2D);
- glEnable(GL_TEXTURE_RECTANGLE_ARB);
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g->mat.texture);
- glTexCoordPointer(2, GL_FLOAT, 0,
- g->textures.e);
+ glEnable(g->mat.mode);
+ // printf("tex mode %d texture %d\n", g->mat.mode, g->mat.texture);
+ dumpError("glEnable texture");
+ glBindTexture(g->mat.mode, g->mat.texture);
+ dumpError("glBindTexture");
+ glTexCoordPointer(2, GL_FLOAT, 0, g->textures.e);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- } else
- glDisable(GL_TEXTURE_RECTANGLE_ARB);
+ dumpError("GL_TEXTURE_COORD_ARRAY");
+ }
if (g->normals.count) {
- glNormalPointer(GL_FLOAT, 0,
- g->normals.e);
+ glNormalPointer(GL_FLOAT, 0, g->normals.e);
glEnableClientState(GL_NORMAL_ARRAY);
}
- glDrawArrays(g->type.subtype, 0, g->vertice.count);
+ glDrawArrays(g->type.subtype, 0, g->projected.count ? g->projected.count : g->vertice.count);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
+ if (g->textures.count && g->texture)
+ glDisable(g->mat.mode);
}
const c3driver_context_t c3context_driver = {
- .geometry_prepare = _c3_geometry_prepare,
+ .geometry_project = _c3_geometry_project,
.geometry_draw = _c3_geometry_draw,
};
glLoadIdentity(); // Start with an identity matrix
gluPerspective(60, _w / _h, 60, 400);
-
-// glDepthMask(GL_TRUE);
-// glCullFace(GL_BACK);
-// glEnable(GL_CULL_FACE);
+#if 0
+ glCullFace(GL_BACK);
+ glEnable(GL_CULL_FACE);
+#endif
+ glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
+ glEnable(GL_LIGHTING);
+// glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
glEnable(GL_BLEND); // Enable Blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Type Of Blending To Use
if (c3->root->dirty) {
// printf("reproject\n");
- c3context_prepare(c3);
+ c3context_project(c3);
qsort(c3->projected.e, c3->projected.count,
sizeof(c3->projected.e[0]), _c3_z_sorter);
c3context_draw(c3);
glMatrixMode(GL_PROJECTION); // Select projection matrix
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_LIGHTING);
glLoadIdentity(); // Start with an identity matrix
glOrtho(0, _w, 0, _h, 0, 10);
glScalef(1,-1,1);
glTranslatef(0, -1 * _h, 0);
-
glMatrixMode(GL_MODELVIEW); // Select modelview matrix
+ if (hud)
+ c3context_draw(hud);
+
glutSwapBuffers();
}
glutMotionFunc(_gl_motion_cb);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+ /*
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
-
glEnable(GL_LINE_SMOOTH);
-
+ */
// enable color tracking
glEnable(GL_COLOR_MATERIAL);
// set material properties which will be assigned by glColor
static const c3driver_context_t * list[] = { &c3context_driver, NULL };
c3->driver = list;
+ c3texture_p line_aa_tex = NULL;
+ {
+ cairo_surface_t * image = cairo_image_surface_create_from_png ("gfx/BlurryCircle.png");
+ printf("image = %p %p\n", image, cairo_image_surface_get_data (image));
+ c3texture_p b = c3texture_new(c3->root);
+
+ c3pixels_p dst = &b->pixels;
+ c3pixels_init(dst,
+ cairo_image_surface_get_width (image),
+ cairo_image_surface_get_height (image),
+ 1, cairo_image_surface_get_width (image),
+ NULL);
+ c3pixels_alloc(dst);
+
+ c3pixels_p src = c3pixels_new(
+ cairo_image_surface_get_width (image),
+ cairo_image_surface_get_height (image),
+ 4, cairo_image_surface_get_stride(image),
+ cairo_image_surface_get_data (image));
+
+ uint32_t * _s = (uint32_t *)src->base;
+ uint8_t * _d = (uint8_t *)dst->base;
+ int max = 0;
+ for (int i = 0; i < dst->h * dst->w; i++)
+ if ((_s[i] & 0xff) > max)
+ max = _s[i] & 0xff;
+ for (int i = 0; i < dst->h * dst->w; i++)
+ *_d++ = ((_s[i] & 0xff) * 255) / max;// + (0xff - max);
+
+ b->geometry.dirty = 1;
+ c3mat4 i = identity3D();
+ b->geometry.mat.mode = GL_TEXTURE_2D;
+ b->pixels.format = C3PIXEL_A;
+ c3geometry_project(&b->geometry, &i);
+ //_c3_geometry_project(NULL, NULL, &b->geometry, &i);
+ line_aa_tex = b;
+
+ c3pixels_p p = &b->pixels;
+ printf("struct { int w, h, stride, size, format; uint8_t pix[] } img = {\n"
+ "%d, %d, %d, %d, %d\n",
+ p->w, p->h, (int)p->row, p->psize, cairo_image_surface_get_format(image));
+ for (int i = 0; i < 32; i++)
+ printf("0x%08x", ((uint32_t*)src->base)[i]);
+ printf("\n");
+ }
c3object_p grid = c3object_new(c3->root);
{
for (int x = 0; x < 20; x++) {
c3vec3f(x*10,-1+y*10,0), c3vec3f(x*10,1+y*10,0),
};
c3geometry_p g = c3geometry_new(
- c3geometry_type(C3_RAW_TYPE, GL_LINES), grid);
+ c3geometry_type(C3_LINES_TYPE, 0), grid);
g->mat.color = c3vec4f(0.0, 0.0, 0.0, 1.0);
+ g->mat.texture = line_aa_tex->geometry.mat.texture;
+ g->mat.mode = GL_TEXTURE_2D;//GL_TEXTURE_RECTANGLE_ARB;
+ g->texture = 1;
c3vertex_array_insert(&g->vertice,
g->vertice.count, p, 4);
}
}
}
+
+ {
+ c3vec3 p[4] = {
+ c3vec3f(-5,-5,0), c3vec3f(205,-5,0),
+ };
+ c3geometry_p g = c3geometry_new(
+ c3geometry_type(C3_LINES_TYPE, 0), grid);
+ g->mat.color = c3vec4f(0.0, 0.0, 0.0, 0.8);
+ g->mat.texture = line_aa_tex->geometry.mat.texture;
+ g->mat.mode = GL_TEXTURE_2D;//GL_TEXTURE_RECTANGLE_ARB;
+// g->mat.mode = GL_TEXTURE_RECTANGLE_ARB;
+ g->texture = 1;
+
+ printf("AA texture is %d\n", line_aa_tex->geometry.mat.texture);
+ c3vertex_array_insert(&g->vertice,
+ g->vertice.count, p, 2);
+
+ }
head = c3stl_load("gfx/buserror-nozzle-model.stl", c3->root);
//head = c3object_new(c3->root);
c3transform_new(head);
memset(b->pixels.base, 0xff, 10 * b->pixels.row);
#endif
+
+ hud = c3context_new(_w, _h);
+ hud->driver = list;
+
+ {
+ c3vec3 p[4] = {
+ c3vec3f(10,10,0), c3vec3f(700,40,0),
+ };
+ c3geometry_p g = c3geometry_new(
+ c3geometry_type(C3_LINES_TYPE, 0), hud->root);
+ g->mat.color = c3vec4f(0.0, 0.0, 0.0, 0.8);
+ g->mat.texture = line_aa_tex->geometry.mat.texture;
+ g->mat.mode = GL_TEXTURE_2D;//GL_TEXTURE_RECTANGLE_ARB;
+// g->mat.mode = GL_TEXTURE_RECTANGLE_ARB;
+ g->texture = 1;
+ c3vertex_array_insert(&g->vertice,
+ g->vertice.count, p, 2);
+ }
return 1;
}