53 #define _USE_MATH_DEFINES 64 #include <GL/osmesa.h> 66 #include "pedigree/native/graphics/Graphics.h" 71 Gears *g_pGears = NULL;
73 bool g_bRunning =
false;
75 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
76 static GLint gear1, gear2, gear3;
77 static GLfloat angle = 0.0;
79 static unsigned int frames = 0;
80 static unsigned int start_time = 0;
85 gettimeofday(&now, NULL);
89 start_time = now.tv_sec;
91 else if ((now.tv_sec - start_time) >= 5)
93 GLfloat seconds = now.tv_sec - start_time;
94 GLfloat fps = frames / seconds;
95 klog(LOG_INFO,
"%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, fps);
96 start_time = now.tv_sec;
102 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
103 GLint teeth, GLfloat tooth_depth)
111 r1 = outer_radius - tooth_depth / 2.0;
112 r2 = outer_radius + tooth_depth / 2.0;
114 da = 2.0 * M_PI / teeth / 4.0;
116 glShadeModel(GL_FLAT);
118 glNormal3f(0.0, 0.0, 1.0);
121 glBegin(GL_QUAD_STRIP);
122 for (i = 0; i <= teeth; i++) {
123 angle = i * 2.0 * M_PI / teeth;
124 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
125 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
127 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
128 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
136 da = 2.0 * M_PI / teeth / 4.0;
137 for (i = 0; i < teeth; i++) {
138 angle = i * 2.0 * M_PI / teeth;
140 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
141 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
142 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
144 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
149 glNormal3f(0.0, 0.0, -1.0);
152 glBegin(GL_QUAD_STRIP);
153 for (i = 0; i <= teeth; i++) {
154 angle = i * 2.0 * M_PI / teeth;
155 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
156 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
158 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
160 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
167 da = 2.0 * M_PI / teeth / 4.0;
168 for (i = 0; i < teeth; i++) {
169 angle = i * 2.0 * M_PI / teeth;
171 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
173 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
175 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
176 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
181 glBegin(GL_QUAD_STRIP);
182 for (i = 0; i < teeth; i++) {
183 angle = i * 2.0 * M_PI / teeth;
185 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
186 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
187 u = r2 * cos(angle + da) - r1 * cos(angle);
188 v = r2 * sin(angle + da) - r1 * sin(angle);
189 len = sqrt(u * u + v * v);
192 glNormal3f(v, -u, 0.0);
193 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
194 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
195 glNormal3f(cos(angle), sin(angle), 0.0);
196 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
198 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
200 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
201 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
202 glNormal3f(v, -u, 0.0);
203 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
205 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
207 glNormal3f(cos(angle), sin(angle), 0.0);
210 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
211 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
215 glShadeModel(GL_SMOOTH);
218 glBegin(GL_QUAD_STRIP);
219 for (i = 0; i <= teeth; i++) {
220 angle = i * 2.0 * M_PI / teeth;
221 glNormal3f(-cos(angle), -sin(angle), 0.0);
222 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
223 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
232 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
235 glRotatef(view_rotx, 1.0, 0.0, 0.0);
236 glRotatef(view_roty, 0.0, 1.0, 0.0);
237 glRotatef(view_rotz, 0.0, 0.0, 1.0);
240 glTranslatef(-3.0, -2.0, 0.0);
241 glRotatef(angle, 0.0, 0.0, 1.0);
246 glTranslatef(3.1, -2.0, 0.0);
247 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
252 glTranslatef(-3.1, 4.2, 0.0);
253 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
264 reshape(
int width,
int height)
266 GLfloat h = (GLfloat) height / (GLfloat) width;
268 glViewport(0, 0, (GLint) width, (GLint) height);
269 glMatrixMode(GL_PROJECTION);
271 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
272 glMatrixMode(GL_MODELVIEW);
274 glTranslatef(0.0, 0.0, -40.0);
282 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
283 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
284 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
285 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
287 glLightfv(GL_LIGHT0, GL_POSITION, pos);
288 glEnable(GL_CULL_FACE);
289 glEnable(GL_LIGHTING);
291 glEnable(GL_DEPTH_TEST);
294 gear1 = glGenLists(1);
295 glNewList(gear1, GL_COMPILE);
296 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
297 gear(1.0, 4.0, 1.0, 20, 0.7);
300 gear2 = glGenLists(1);
301 glNewList(gear2, GL_COMPILE);
302 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
303 gear(0.5, 2.0, 2.0, 10, 0.7);
306 gear3 = glGenLists(1);
307 glNewList(gear3, GL_COMPILE);
308 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
309 gear(1.3, 2.0, 0.5, 10, 0.7);
312 glEnable(GL_NORMALIZE);
319 Gears() :
Widget(), fb(NULL), m_bGlInit(
false), m_bContextValid(
false), m_nWidth(0), m_nHeight(0)
326 gl_ctx = OSMesaCreateContext(OSMESA_BGRA, NULL);
332 glResize(m_nWidth, m_nHeight);
340 OSMesaDestroyContext(gl_ctx);
345 m_nWidth = newrt.getW();
346 m_nHeight = newrt.getH();
347 glResize(newrt.getW(), newrt.getH());
352 if(!(m_bGlInit && m_bContextValid && fb))
361 dirty.update(0, 0, m_nWidth, m_nHeight);
368 bool glResize(
size_t w,
size_t h)
378 m_bContextValid =
false;
379 fprintf(stderr,
"Couldn't get a framebuffer to use.\n");
383 if(!OSMesaMakeCurrent(gl_ctx, fb, GL_UNSIGNED_BYTE, w, h))
385 m_bContextValid =
false;
386 fprintf(stderr,
"OSMesaMakeCurrent failed.\n");
391 OSMesaPixelStore(OSMESA_Y_UP, 0);
395 m_bContextValid =
true;
403 bool m_bContextValid;
405 OSMesaContext gl_ctx;
407 size_t m_nWidth, m_nHeight;
410 bool callback(WidgetMessages message,
size_t msgSize,
const void *msgData)
417 if(g_pGears->
render(rt, dirty))
426 g_pGears->reposition(*rt);
431 const uint64_t *key =
reinterpret_cast<const uint64_t*
>(msgData);
432 klog(LOG_INFO,
"gears: keypress %d '%c'", (uint32_t) *key, (
char) (*key & 0xFF));
436 char realChar = *key & 0xFF;
441 else if(realChar ==
'd')
445 else if(realChar ==
'w')
449 else if(realChar ==
's')
459 klog(LOG_INFO,
"gears: unhandled callback");
464 int main (
int argc,
char ** argv) {
468 sprintf(endpoint,
"gears.%d", getpid());
470 g_pGears =
new Gears();
471 if(!g_pGears->
construct(endpoint,
"OSMesa 3D Gears", callback, rt)) {
472 klog(LOG_ERR,
"gears: not able to construct widget");
476 klog(LOG_INFO,
"gears: widget constructed");
478 g_pGears->initOpenGL();
480 klog(LOG_INFO,
"gears: reshaping");
484 klog(LOG_INFO,
"gears: entering main loop");
491 callback(RepaintNeeded, 0, 0);
497 g_pGears->deinitOpenGL();
virtual bool render(PedigreeGraphics::Rect &rt, PedigreeGraphics::Rect &dirty)