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>
31 #include "reprap_gl.h"
34 #include "c3/c3camera.h"
35 #include "c3/c3arcball.h"
37 int _w = 800, _h = 600;
42 c3geometry_array_t geo_sorted = C_ARRAY_NULL;
44 extern reprap_t reprap;
50 int y) /* called on key press */
54 // avr_vcd_stop(&vcd_file);
58 printf("Starting VCD trace; press 's' to stop\n");
59 // avr_vcd_start(&vcd_file);
62 printf("Stopping VCD trace\n");
63 // avr_vcd_stop(&vcd_file);
70 _gl_display_cb(void) /* function called whenever redisplay needed */
72 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74 // Set up projection matrix
75 glMatrixMode(GL_PROJECTION); // Select projection matrix
76 glLoadIdentity(); // Start with an identity matrix
78 gluPerspective(45, _w / _h, 0, 10000);
80 // glEnable(GL_TEXTURE_2D);
81 glShadeModel(GL_SMOOTH);
83 glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
84 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
86 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87 glEnable(GL_BLEND); // Enable Blending
88 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Type Of Blending To Use
90 glMatrixMode( GL_MODELVIEW );
92 // glMultMatrixf(arcball.rot.n);
93 glMultMatrixf(cam.mtx.n);
94 glTranslatef( -cam.eye.n[VX], -cam.eye.n[VY], -cam.eye.n[VZ] );
95 // glMultMatrixf(arcball.rot.n);
97 c3vec3 headp = c3vec3f(
98 stepper_get_position_mm(&reprap.step_x),
99 stepper_get_position_mm(&reprap.step_y),
100 stepper_get_position_mm(&reprap.step_z));
101 c3mat4 headmove = translation3D(headp);
102 c3transform_set(head->transform.e[0], &headmove);
105 printf("reproject\n");
106 c3mat4 m = identity3D();
107 c3object_project(root, &m);
108 c3geometry_array_clear(&geo_sorted);
109 c3object_get_geometry(root, &geo_sorted);
112 for (int gi = 0; gi < geo_sorted.count; gi++) {
113 c3geometry_p g = geo_sorted.e[gi];
114 glColor4fv(g->mat.color.n);
115 glVertexPointer(3, GL_FLOAT, 0, g->projected.count ? g->projected.e : g->vertice.e);
116 glEnableClientState(GL_VERTEX_ARRAY);
118 glDrawArrays(g->type, 0, g->vertice.count);
120 glDisableClientState(GL_VERTEX_ARRAY);
124 glMatrixMode(GL_PROJECTION); // Select projection matrix
125 glLoadIdentity(); // Start with an identity matrix
126 glOrtho(0, _w, 0, _h, 0, 10);
128 glTranslatef(0, -1 * _h, 0);
130 glMatrixMode(GL_MODELVIEW); // Select modelview matrix
134 glLoadIdentity(); // Start with an identity matrix
138 colors[color][0], /* background */
139 colors[color][1], /* character background */
140 colors[color][2], /* text */
141 colors[color][3] /* shadow */ );
158 button = s == GLUT_DOWN ? b : 0;
160 move = c3vec2f(x, y);
162 c3arcball_mouse_down(&arcball, x, y);
164 c3arcball_mouse_up(&arcball);
172 c3vec2 m = c3vec2f(x, y);
173 c3vec2 delta = c3vec2_sub(move, m);
175 // printf("%s b%d click %.1f,%.1f now %d,%d\n",
176 // __func__, button, move.n[0], move.n[1], x, y);
179 case GLUT_LEFT_BUTTON: {
181 // c3cam_eye_yaw(&cam, delta.n[0] / 4);
182 // c3cam_eye_pitch(&cam, delta.n[1] / 4);
184 c3mat4 rotx = rotation3D(c3vec3f(1.0, 0, 0), delta.n[1] / 4);
185 c3mat4 roty = rotation3D(c3vec3f(0.0, 0.0, 1.0), delta.n[0] / 4);
186 rotx = c3mat4_mul(&rotx, &roty);
187 c3cam_rot_about_lookat(&cam, &rotx);
189 c3cam_update_matrix(&cam);
190 // c3arcball_mouse_motion(&arcball, x, y, 0,0,0);
192 case GLUT_RIGHT_BUTTON: {
199 // gl timer. if the lcd is dirty, refresh display
204 //static int oldstate = -1;
206 c3arcball_idle(&arcball);
207 glutTimerFunc(1000 / 24, _gl_timer_cb, 0);
216 glutInit(&argc, argv); /* initialize GLUT system */
218 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
219 glutInitWindowSize(_w, _h); /* width=400pixels height=500pixels */
220 /*window =*/ glutCreateWindow("Press 'q' to quit"); /* create window */
222 glutDisplayFunc(_gl_display_cb); /* set window's display callback */
223 glutKeyboardFunc(_gl_key_cb); /* set window's key callback */
224 glutTimerFunc(1000 / 24, _gl_timer_cb, 0);
226 glutMouseFunc(_gl_button_cb);
227 glutMotionFunc(_gl_motion_cb);
229 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
230 glEnable(GL_LINE_SMOOTH);
232 cam.lookat = c3vec3f(100.0, 100.0, 0.0);
233 cam.eye = c3vec3f(100.0, -100.0, 100.0);
234 c3cam_update_matrix(&cam);
236 c3arcball_init_center(&arcball, c3vec2f(_w/2, _h/2), 100);
237 // hd44780_gl_init();
239 root = c3object_new(NULL);
241 c3object_p grid = c3object_new(root);
243 c3geometry_p g = c3geometry_new(GL_LINES, grid);
244 g->mat.color = c3vec4f(1.0, 1.0, 1.0, 1.0);
245 for (int x = 0; x < 20; x++) {
246 for (int y = 0; y < 20; y++) {
248 c3vec3f(-1+x*10,y*10,0), c3vec3f(1+x*10,y*10,0),
249 c3vec3f(x*10,-1+y*10,0), c3vec3f(x*10,1+y*10,0),
251 c3vertex_array_insert(&g->vertice,
252 g->vertice.count, p, 4);
256 head = c3object_new(root);
257 c3transform_new(head);
259 c3geometry_p g = c3geometry_new(GL_LINES, head);
260 g->mat.color = c3vec4f(1.0, 0.0, 0.0, 1.0);
262 c3vec3f(-1, 0, 0), c3vec3f(1, 0, 0),
263 c3vec3f(0, -1, 0), c3vec3f(0, 1, 0),
265 c3vertex_array_insert(&g->vertice,
266 g->vertice.count, p, 4);