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 #include <GLUT/glut.h>
32 #include "reprap_gl.h"
35 #include "c3/c3camera.h"
36 #include "c3/c3arcball.h"
37 #include "c3/c3driver_context.h"
40 int _w = 800, _h = 600;
46 extern reprap_t reprap;
48 static int dumpError(const char * what)
52 while ((e = glGetError()) != GL_NO_ERROR) {
53 printf("%s: %s\n", what, gluErrorString(e));
63 int y) /* called on key press */
67 // avr_vcd_stop(&vcd_file);
68 c3context_dispose(c3);
72 printf("Starting VCD trace; press 's' to stop\n");
73 // avr_vcd_start(&vcd_file);
76 printf("Stopping VCD trace\n");
77 // avr_vcd_stop(&vcd_file);
85 const struct c3driver_context_t *d,
88 switch(g->type.type) {
89 case C3_TRIANGLE_TYPE: {
90 g->type.subtype = GL_TRIANGLES;
91 //g->mat.color = c3vec4f(0.0, 0.0, 1.0, 1.0);
93 case C3_TEXTURE_TYPE: {
94 c3texture_p t = (c3texture_p)g;
95 g->type.subtype = GL_TRIANGLE_FAN;
96 g->mat.color = c3vec4f(0.0, 1.0, 0.0, 0.5);
97 printf("_c3_geometry_prepare xrure %d!\n", g->textures.count);
100 dumpError("cp_gl_texture_load_argb flush");
102 glEnable(GL_TEXTURE_RECTANGLE_ARB);
103 dumpError("cp_gl_texture_load_argb GL_TEXTURE_RECTANGLE_ARB");
105 glGenTextures(1, &texID);
106 dumpError("cp_gl_texture_load_argb glBindTexture GL_TEXTURE_RECTANGLE_ARB");
108 glPixelStorei(GL_UNPACK_ROW_LENGTH, t->pixels.row / 4);
109 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
110 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
111 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
112 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
114 g->mat.texture = texID;
117 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g->mat.texture);
118 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8,
119 t->pixels.w, t->pixels.h, 0,
120 GL_RGBA, GL_UNSIGNED_BYTE,
131 const struct c3driver_context_t *d,
134 glColor4fv(g->mat.color.n);
135 // glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, g->mat.color.n);
136 glVertexPointer(3, GL_FLOAT, 0,
137 g->projected.count ? g->projected.e : g->vertice.e);
138 glEnableClientState(GL_VERTEX_ARRAY);
139 if (g->textures.count && g->texture) {
140 glDisable(GL_TEXTURE_2D);
141 glEnable(GL_TEXTURE_RECTANGLE_ARB);
142 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g->mat.texture);
143 glTexCoordPointer(2, GL_FLOAT, 0,
145 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
147 glDisable(GL_TEXTURE_RECTANGLE_ARB);
148 if (g->normals.count) {
149 glNormalPointer(GL_FLOAT, 0,
151 glEnableClientState(GL_NORMAL_ARRAY);
153 glDrawArrays(g->type.subtype, 0, g->vertice.count);
154 glDisableClientState(GL_VERTEX_ARRAY);
155 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
156 glDisableClientState(GL_NORMAL_ARRAY);
159 const c3driver_context_t c3context_driver = {
160 .geometry_prepare = _c3_geometry_prepare,
161 .geometry_draw = _c3_geometry_draw,
165 * Computes the distance from the eye, sort by this value
172 c3geometry_p g1 = *(c3geometry_p*)_p1;
173 c3geometry_p g2 = *(c3geometry_p*)_p2;
174 // get center of bboxes
175 c3vec3 c1 = c3vec3_add(g1->bbox.min, c3vec3_divf(c3vec3_sub(g1->bbox.max, g1->bbox.min), 2));
176 c3vec3 c2 = c3vec3_add(g2->bbox.min, c3vec3_divf(c3vec3_sub(g2->bbox.max, g2->bbox.min), 2));
178 c3f d1 = c3vec3_length2(c3vec3_sub(c1, cam.eye));
179 c3f d2 = c3vec3_length2(c3vec3_sub(c2, cam.eye));
182 * make sure transparent items are drawn after everyone else
184 if (g1->mat.color.n[3] < 1)
186 if (g2->mat.color.n[3] < 1)
189 return d1 < d2 ? 1 : d1 > d2 ? -1 : 0;
193 _gl_display_cb(void) /* function called whenever redisplay needed */
195 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
196 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198 // Set up projection matrix
199 glMatrixMode(GL_PROJECTION); // Select projection matrix
200 glLoadIdentity(); // Start with an identity matrix
202 gluPerspective(60, _w / _h, 60, 400);
204 // glDepthMask(GL_TRUE);
205 // glCullFace(GL_BACK);
206 // glEnable(GL_CULL_FACE);
207 glEnable(GL_DEPTH_TEST);
209 glEnable(GL_BLEND); // Enable Blending
210 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Type Of Blending To Use
212 glMatrixMode( GL_MODELVIEW );
214 // glMultMatrixf(arcball.rot.n);
215 glMultMatrixf(cam.mtx.n);
216 glTranslatef( -cam.eye.n[VX], -cam.eye.n[VY], -cam.eye.n[VZ] );
217 // glMultMatrixf(arcball.rot.n);
219 c3vec3 headp = c3vec3f(
220 stepper_get_position_mm(&reprap.step_x),
221 stepper_get_position_mm(&reprap.step_y),
222 stepper_get_position_mm(&reprap.step_z));
223 c3mat4 headmove = translation3D(headp);
224 c3transform_set(head->transform.e[0], &headmove);
226 if (c3->root->dirty) {
227 // printf("reproject\n");
228 c3context_prepare(c3);
230 qsort(c3->projected.e, c3->projected.count,
231 sizeof(c3->projected.e[0]), _c3_z_sorter);
235 glMatrixMode(GL_PROJECTION); // Select projection matrix
236 glLoadIdentity(); // Start with an identity matrix
237 glOrtho(0, _w, 0, _h, 0, 10);
239 glTranslatef(0, -1 * _h, 0);
241 glMatrixMode(GL_MODELVIEW); // Select modelview matrix
257 button = s == GLUT_DOWN ? b : 0;
259 move = c3vec2f(x, y);
261 c3arcball_mouse_down(&arcball, x, y);
263 c3arcball_mouse_up(&arcball);
271 c3vec2 m = c3vec2f(x, y);
272 c3vec2 delta = c3vec2_sub(move, m);
274 // printf("%s b%d click %.1f,%.1f now %d,%d\n",
275 // __func__, button, move.n[0], move.n[1], x, y);
278 case GLUT_LEFT_BUTTON: {
280 // c3cam_eye_yaw(&cam, delta.n[0] / 4);
281 // c3cam_eye_pitch(&cam, delta.n[1] / 4);
283 c3mat4 rotx = rotation3D(c3vec3f(1.0, 0, 0), delta.n[1] / 4);
284 c3mat4 roty = rotation3D(c3vec3f(0.0, 0.0, 1.0), delta.n[0] / 4);
285 rotx = c3mat4_mul(&rotx, &roty);
286 c3cam_rot_about_lookat(&cam, &rotx);
288 c3cam_update_matrix(&cam);
289 c3->root->dirty = 1; // resort the array
290 // c3arcball_mouse_motion(&arcball, x, y, 0,0,0);
292 case GLUT_RIGHT_BUTTON: {
299 // gl timer. if the lcd is dirty, refresh display
304 //static int oldstate = -1;
306 c3arcball_idle(&arcball);
307 glutTimerFunc(1000 / 24, _gl_timer_cb, 0);
316 glutInit(&argc, argv); /* initialize GLUT system */
318 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
319 glutInitWindowSize(_w, _h); /* width=400pixels height=500pixels */
320 /*window =*/ glutCreateWindow("Press 'q' to quit"); /* create window */
322 glutDisplayFunc(_gl_display_cb); /* set window's display callback */
323 glutKeyboardFunc(_gl_key_cb); /* set window's key callback */
324 glutTimerFunc(1000 / 24, _gl_timer_cb, 0);
326 glutMouseFunc(_gl_button_cb);
327 glutMotionFunc(_gl_motion_cb);
329 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
330 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
331 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
332 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
334 glEnable(GL_LINE_SMOOTH);
336 // enable color tracking
337 glEnable(GL_COLOR_MATERIAL);
338 // set material properties which will be assigned by glColor
339 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
342 glShadeModel(GL_SMOOTH);
344 // glEnable(GL_DEPTH_TEST);
345 glEnable(GL_LIGHTING);
346 GLfloat global_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
347 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
350 GLfloat specular[] = {1.0f, 1.0f, 1.0f , 0.8f};
351 GLfloat position[] = { -50.0f, -50.0f, 100.0f, 1.0f };
352 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
353 glLightfv(GL_LIGHT0, GL_POSITION, position);
357 GLfloat specular[] = {1.0f, 1.0f, 1.0f , 0.8f};
358 GLfloat position[] = { 250.0f, -50.0f, 100.0f, 1.0f };
359 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
360 glLightfv(GL_LIGHT0, GL_POSITION, position);
364 // glEnable(GL_BLEND);
365 // Works for the UI !!
366 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
369 cam.lookat = c3vec3f(100.0, 100.0, 0.0);
370 cam.eye = c3vec3f(100.0, -100.0, 100.0);
371 c3cam_update_matrix(&cam);
373 c3arcball_init_center(&arcball, c3vec2f(_w/2, _h/2), 100);
374 // hd44780_gl_init();
376 c3 = c3context_new(_w, _h);
377 static const c3driver_context_t * list[] = { &c3context_driver, NULL };
380 c3object_p grid = c3object_new(c3->root);
382 for (int x = 0; x < 20; x++) {
383 for (int y = 0; y < 20; y++) {
385 c3vec3f(-1+x*10,y*10,0), c3vec3f(1+x*10,y*10,0),
386 c3vec3f(x*10,-1+y*10,0), c3vec3f(x*10,1+y*10,0),
388 c3geometry_p g = c3geometry_new(
389 c3geometry_type(C3_RAW_TYPE, GL_LINES), grid);
390 g->mat.color = c3vec4f(0.0, 0.0, 0.0, 1.0);
391 c3vertex_array_insert(&g->vertice,
392 g->vertice.count, p, 4);
396 head = c3stl_load("gfx/buserror-nozzle-model.stl", c3->root);
397 //head = c3object_new(c3->root);
398 c3transform_new(head);
399 if (head->geometry.count > 0) {
400 head->geometry.e[0]->mat.color = c3vec4f(0.6, 0.5, 0.0, 1.0);
404 c3texture_p b = c3texture_new(head);
405 c3pixels_init(&b->pixels, 64, 64, 4, 4 * 64, NULL);
406 b->geometry.dirty = 1;
407 memset(b->pixels.base, 0xff, 10 * b->pixels.row);
416 c3context_dispose(c3);