4 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 simavr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with simavr. If not, see <http://www.gnu.org/licenses/>.
23 #define GL_GLEXT_PROTOTYPES
24 #include <GLUT/glut.h>
25 #include <OpenGL/gl.h>
26 #include <OpenGL/glext.h>
28 #define GL_GLEXT_PROTOTYPES
38 #include "reprap_gl.h"
42 #include "c3driver_context.h"
47 #include "c3program.h"
51 #include <cairo/cairo.h>
53 struct cairo_surface_t;
55 int _w = 800, _h = 600;
57 c3context_p c3 = NULL;
58 c3context_p hud = NULL;
60 c3object_p head = NULL; // hotend
61 c3texture_p fbo_c3; // frame buffer object texture
62 c3program_p fxaa = NULL; // full screen antialias shader
65 int glsl_version = 110;
67 extern reprap_t reprap;
69 static int dumpError(const char * what)
73 while ((e = glGetError()) != GL_NO_ERROR) {
74 printf("%s: %s\n", what, gluErrorString(e));
80 #define GLCHECK(_w) {_w; dumpError(#_w);}
84 _gl_reshape_cb(int w, int h)
89 c3vec2 size = c3vec2f(_w, _h);
91 glBindFramebuffer(GL_FRAMEBUFFER, 0);
92 glViewport(0, 0, _w, _h);
93 c3gl_fbo_resize(&fbo, size);
94 c3texture_resize(fbo_c3, size);
97 glUseProgram((GLuint)fxaa->pid);
98 GLCHECK(glUniform2fv((GLuint)fxaa->params.e[0].pid, 1, size.n));
109 int y) /* called on key press */
113 // avr_vcd_stop(&vcd_file);
114 c3context_dispose(c3);
118 printf("Starting VCD trace; press 's' to stop\n");
119 // avr_vcd_start(&vcd_file);
122 printf("Stopping VCD trace\n");
123 // avr_vcd_stop(&vcd_file);
126 if (fbo_c3->geometry.mat.program)
127 fbo_c3->geometry.mat.program = NULL;
129 fbo_c3->geometry.mat.program = fxaa;
136 _gl_display_cb(void) /* function called whenever redisplay needed */
141 glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)fbo.fbo);
142 // draw (without glutSwapBuffers)
143 dumpError("glBindFramebuffer fbo");
144 glViewport(0, 0, _w, _h);
146 c3context_view_set(c3, 0);
147 c3vec3 headp = c3vec3f(
148 stepper_get_position_mm(&reprap.step_x),
149 stepper_get_position_mm(&reprap.step_y),
150 stepper_get_position_mm(&reprap.step_z));
151 c3mat4 headmove = translation3D(headp);
152 c3transform_set(head->transform.e[0], &headmove);
154 if (c3->root->dirty) {
155 // printf("reproject head %.2f,%.2f,%.2f\n", headp.x, headp.y,headp.z);
156 c3context_project(c3);
158 float z_min = c3context_view_get(c3)->z.min,
159 z_max = c3context_view_get(c3)->z.max;
163 if (z_max < z_min || z_max > 1000)
166 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
169 // Set up projection matrix
170 glMatrixMode(GL_PROJECTION); // Select projection matrix
171 c3mat4 p = perspective3D(50, (float)_w / (float)_h, z_min, z_max);
176 glEnable(GL_CULL_FACE);
178 glDepthMask(GL_TRUE);
179 glDepthFunc(GL_LEQUAL);
180 glEnable(GL_DEPTH_TEST);
181 glEnable(GL_LIGHTING);
182 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
184 glEnable(GL_ALPHA_TEST);
185 glAlphaFunc(GL_GREATER, 1.0f / 255.0f);
186 glEnable(GL_BLEND); // Enable Blending
187 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Type Of Blending To Use
189 glMatrixMode(GL_MODELVIEW);
194 * Draw back FBO over the screen
196 glBindFramebuffer(GL_FRAMEBUFFER, 0);
197 dumpError("glBindFramebuffer 0");
198 glViewport(0, 0, _w, _h);
200 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
201 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
203 glDisable(GL_DEPTH_TEST);
204 glDisable(GL_LIGHTING);
205 glDisable(GL_ALPHA_TEST);
209 glMatrixMode(GL_PROJECTION); // Select projection matrix
210 glLoadIdentity(); // Start with an identity matrix
212 c3mat4 pro = screen_ortho3D(0, _w, 0, _h, 0, 10);
213 glLoadMatrixf(pro.n);
215 glMatrixMode(GL_MODELVIEW); // Select modelview matrix
217 if (hud->root->dirty) {
218 // printf("reproject head %.2f,%.2f,%.2f\n", headp.x, headp.y,headp.z);
219 c3context_project(hud);
226 #if !defined(GLUT_WHEEL_UP)
227 # define GLUT_WHEEL_UP 3
228 # define GLUT_WHEEL_DOWN 4
242 button = s == GLUT_DOWN ? b : 0;
243 move = c3vec2f(x, y);
244 c3context_view_p view = c3context_view_get_at(c3, 0);
245 // printf("button %d: %.1f,%.1f\n", b, move.x, move.y);
247 case GLUT_LEFT_BUTTON:
248 case GLUT_RIGHT_BUTTON: // call motion
251 case GLUT_WHEEL_DOWN:
252 if (view->cam.distance > 10) {
253 const float d = 0.004;
254 c3cam_set_distance(&view->cam,
255 view->cam.distance * ((b == GLUT_WHEEL_DOWN) ? (1.0+d) : (1.0-d)));
256 c3cam_update_matrix(&view->cam);
257 view->dirty = 1; // resort the array
268 c3vec2 m = c3vec2f(x, y);
269 c3vec2 delta = c3vec2_sub(move, m);
270 c3context_view_p view = c3context_view_get_at(c3, 0);
272 // printf("%s b%d click %.1f,%.1f now %d,%d delta %.1f,%.1f\n",
273 // __func__, button, move.n[0], move.n[1], x, y, delta.x, delta.y);
276 case GLUT_LEFT_BUTTON: {
277 c3mat4 rotx = rotation3D(view->cam.side, delta.n[1] / 4);
278 c3mat4 roty = rotation3D(c3vec3f(0.0, 0.0, 1.0), delta.n[0] / 4);
279 rotx = c3mat4_mul(&rotx, &roty);
280 c3cam_rot_about_lookat(&view->cam, &rotx);
281 c3cam_update_matrix(&view->cam);
283 view->dirty = 1; // resort the array
285 case GLUT_RIGHT_BUTTON: {
286 // offset both points, but following the plane
287 c3vec3 f = c3vec3_mulf(
288 c3vec3f(-view->cam.side.y, view->cam.side.x, 0),
290 view->cam.eye = c3vec3_add(view->cam.eye, f);
291 view->cam.lookat = c3vec3_add(view->cam.lookat, f);
292 c3cam_movef(&view->cam, delta.n[0] / 8, 0, 0);
293 c3cam_update_matrix(&view->cam);
295 view->dirty = 1; // resort the array
301 // gl timer. if the lcd is dirty, refresh display
306 glutTimerFunc(1000 / 24, _gl_timer_cb, 0);
310 const c3driver_context_t * c3_driver_list[3] = { NULL, NULL };
317 glutInit(&argc, argv); /* initialize GLUT system */
319 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);
320 glutInitWindowSize(_w, _h); /* width=400pixels height=500pixels */
321 /*window =*/ glutCreateWindow("Press 'q' to quit"); /* create window */
323 glutDisplayFunc(_gl_display_cb); /* set window's display callback */
324 glutKeyboardFunc(_gl_key_cb); /* set window's key callback */
325 glutTimerFunc(1000 / 24, _gl_timer_cb, 0);
327 glutMouseFunc(_gl_button_cb);
328 glutMotionFunc(_gl_motion_cb);
329 glutReshapeFunc(_gl_reshape_cb);
331 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
332 glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
334 // enable color tracking
335 glEnable(GL_COLOR_MATERIAL);
336 // set material properties which will be assigned by glColor
337 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
339 /* setup some lights */
340 glShadeModel(GL_SMOOTH);
341 glEnable(GL_LIGHTING);
342 GLfloat global_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
343 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
346 GLfloat specular[] = {1.0f, 1.0f, 1.0f , 0.8f};
347 GLfloat position[] = { 250.0f, -50.0f, 100.0f, 1.0f };
348 glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
349 glLightfv(GL_LIGHT1, GL_POSITION, position);
354 * Extract the GLSL version as a numeric value for later
356 const char * glsl = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
359 if (sscanf(glsl, "%d.%d", &M, &m) == 2)
360 glsl_version = (M * 100) + m;
363 printf("GL_SHADING_LANGUAGE_VERSION %s = %d\n", glsl, glsl_version);
365 c3gl_fbo_create(&fbo, c3vec2f(_w, _h), (1 << C3GL_FBO_COLOR)|(1 << C3GL_FBO_DEPTH));
367 c3_driver_list[0] = c3gl_getdriver();
369 c3 = c3context_new(_w, _h);
370 c3->driver = c3_driver_list;
372 c3cam_p cam = &c3context_view_get_at(c3, 0)->cam;
373 cam->lookat = c3vec3f(100.0, 100.0, 0.0);
374 cam->eye = c3vec3f(100.0, -100.0, 100.0);
377 * Create a light, attach it to a movable object, and attach a sphere
378 * to it too so it's visible.
381 c3object_p ligthhook = c3object_new(c3->root);
382 c3transform_p pos = c3transform_new(ligthhook);
384 pos->matrix = translation3D(c3vec3f(-30.0f, -30.0f, 200.0f));
386 c3light_p light = c3light_new(ligthhook);
387 light->geometry.name = str_new("light0");
388 light->color.specular = c3vec4f(1.0f, 1.0f, 1.0f , 0.8f);
389 light->position = c3vec4f(0, 0, 0, 1.0f );
392 c3geometry_p g = c3sphere_uv(ligthhook, c3vec3f(0, 0, 0), 3, 10, 10);
393 g->mat.color = c3vec4f(1.0, 1.0, 0.0, 1.0);
394 g->hidden = 0; // hidden from light scenes
399 const char *path = "gfx/hb.png";
400 cairo_surface_t * image = cairo_image_surface_create_from_png (path);
401 printf("image = %p %p\n", image, cairo_image_surface_get_data (image));
402 c3texture_p b = c3texture_new(c3->root);
404 c3pixels_p dst = c3pixels_new(
405 cairo_image_surface_get_width (image),
406 cairo_image_surface_get_height (image),
407 4, cairo_image_surface_get_stride(image),
408 cairo_image_surface_get_data (image));
409 dst->name = str_new(path);
410 b->geometry.mat.texture = dst;
411 b->size = c3vec2f(200, 200);
412 b->geometry.mat.color = c3vec4f(1.0, 1.0, 1.0, 1.0);
413 // c3transform_new(head);
415 c3pixels_p line_aa_tex = NULL;
417 const char *path = "gfx/BlurryCircle.png";
418 cairo_surface_t * image = cairo_image_surface_create_from_png (path);
419 printf("image = %p %p\n", image, cairo_image_surface_get_data (image));
422 c3pixels_p dst = &b->pixels;
424 cairo_image_surface_get_width (image),
425 cairo_image_surface_get_height (image),
426 1, cairo_image_surface_get_width (image),
429 b->size = c3vec2f(32, 32);
432 c3pixels_p src = c3pixels_new(
433 cairo_image_surface_get_width (image),
434 cairo_image_surface_get_height (image),
435 4, cairo_image_surface_get_stride(image),
436 cairo_image_surface_get_data (image));
438 uint32_t * _s = (uint32_t *)src->base;
439 uint8_t * _d = (uint8_t *)dst->base;
441 for (int i = 0; i < dst->h * dst->w; i++)
442 if ((_s[i] & 0xff) > max)
444 for (int i = 0; i < dst->h * dst->w; i++)
445 *_d++ = ((_s[i] & 0xff) * 255) / max;// + (0xff - max);
446 b->pixels.format = C3PIXEL_A;
448 c3pixels_p dst = c3pixels_new(
449 cairo_image_surface_get_width (image),
450 cairo_image_surface_get_height (image),
451 4, cairo_image_surface_get_stride(image),
452 cairo_image_surface_get_data (image));
453 dst->format = C3PIXEL_ARGB;
455 dst->name = str_new(path);
456 uint8_t * line = dst->base;
457 for (int y = 0; y < dst->h; y++, line += dst->row) {
458 uint32_t *p = (uint32_t *)line;
459 for (int x = 0; x < dst->w; x++, p++) {
461 *p = ((0xff - b) << 24);//|(b << 16)|(b << 8)|(b);
468 printf("struct { int w, h, stride, size, format; uint8_t pix[] } img = {\n"
469 "%d, %d, %d, %d, %d\n",
470 p->w, p->h, (int)p->row, p->psize, cairo_image_surface_get_format(image));
471 for (int i = 0; i < 32; i++)
472 printf("0x%08x ", ((uint32_t*)p->base)[i]);
476 c3object_p grid = c3object_new(c3->root);
478 for (int x = 0; x <= 20; x++) {
479 for (int y = 0; y <= 20; y++) {
481 c3vec3f(-1+x*10,y*10,0.01), c3vec3f(1+x*10,y*10,0.01),
482 c3vec3f(x*10,-1+y*10,0.02), c3vec3f(x*10,1+y*10,0.02),
484 c3geometry_p g = c3geometry_new(
485 c3geometry_type(C3_LINES_TYPE, 0), grid);
486 g->mat.color = c3vec4f(0.0, 0.0, 0.0, 0.8);
487 g->mat.texture = line_aa_tex;
488 c3lines_init(g, p, 4, 0.2);
495 c3vec3f(-5,-5,1), c3vec3f(205,-5,1),
497 c3geometry_p g = c3geometry_new(
498 c3geometry_type(C3_LINES_TYPE, 0), grid);
499 g->mat.color = c3vec4f(0.0, 0.0, 0.0, 1.0);
500 g->mat.texture = line_aa_tex;
503 c3vertex_array_insert(&g->vertice,
504 g->vertice.count, p, 2);
507 head = c3stl_load("gfx/buserror-nozzle-model.stl", c3->root);
508 //head = c3object_new(c3->root);
509 c3transform_new(head);
510 if (head->geometry.count > 0) {
511 c3geometry_factor(head->geometry.e[0], 0.1, (20 * M_PI) / 180.0);
512 head->geometry.e[0]->mat.color = c3vec4f(0.6, 0.5, 0.0, 1.0);
516 c3texture_p b = c3texture_new(head);
517 c3pixels_init(&b->pixels, 64, 64, 4, 4 * 64, NULL);
518 b->geometry.dirty = 1;
519 memset(b->pixels.base, 0xff, 10 * b->pixels.row);
523 hud = c3context_new(_w, _h);
524 hud->driver = c3_driver_list;
527 * This is the offscreen framebuffer where the 3D scene is drawn
531 * need to insert a header since there is nothing to detect the version number
532 * reliably without it, and __VERSION__ returns idiocy
535 sprintf(head, "#version %d\n#define GLSL_VERSION %d\n", glsl_version, glsl_version);
537 const char *uniforms[] = { "g_Resolution", NULL };
538 fxaa = c3program_new("fxaa", uniforms);
539 c3program_array_add(&hud->programs, fxaa);
540 c3program_load_shader(fxaa, GL_VERTEX_SHADER, head,
541 "gfx/postproc.vs", C3_PROGRAM_LOAD_UNIFORM);
542 c3program_load_shader(fxaa, GL_FRAGMENT_SHADER, head,
543 "gfx/postproc.fs", C3_PROGRAM_LOAD_UNIFORM);
545 c3texture_p b = c3texture_new(hud->root);
547 c3pixels_p dst = c3pixels_new(_w, _h, 4, _w * 4, NULL);
548 dst->name = str_new("fbo");
549 dst->texture = fbo.buffers[C3GL_FBO_COLOR].bid;
553 b->geometry.mat.texture = dst;
554 b->geometry.mat.program = fxaa;
555 b->size = c3vec2f(_w, _h);
556 b->geometry.mat.color = c3vec4f(1.0, 1.0, 1.0, 1.0);
562 c3vec3f(10,10,0), c3vec3f(800-10,10,0),
564 c3geometry_p g = c3geometry_new(
565 c3geometry_type(C3_LINES_TYPE, 0), hud->root);
566 g->mat.color = c3vec4f(0.5, 0.5, 1.0, .3f);
567 g->mat.texture = line_aa_tex;
568 c3lines_init(g, p, 2, 10);
576 c3context_dispose(c3);
577 c3context_dispose(hud);
578 c3gl_fbo_dispose(&fbo);