// The well known 3-D gear wheels by Brian Paul // PS3 implementation by Shine // // compile: // gcc -Wall -O2 -I /usr/src/linux-20061110/include -lm -lGLU -lOSMesa gears.c -o gears // // tested on Gentoo, installed with this guide: http://wiki.ps2dev.org/ps3:linux:installing_gentoo #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include OSMesaContext ctx; int width, height, memoryWidth, memoryHeight; int count; void writeTga(const char *filename, const uint32_t *buffer) { FILE *f = fopen(filename, "wb"); if (f) { int x, y; uint8_t tgaHeader[18]; memset(tgaHeader, 0, 18); tgaHeader[2] = 2; tgaHeader[12] = width & 0xff; tgaHeader[13] = (width >> 8) & 0xff; tgaHeader[14] = height & 0xff; tgaHeader[15] = (height >> 8) & 0xff; tgaHeader[16] = 0x18; tgaHeader[17] = 0x20; fwrite(tgaHeader, 1, 18, f); for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { uint32_t color = buffer[x + y * memoryWidth]; fputc(color & 0xff, f); fputc((color >> 8) & 0xff, f); fputc((color >> 16) & 0xff, f); } } fclose(f); } } #ifndef M_PI #define M_PI 3.14159265 #endif /** Draw a gear wheel. You'll probably want to call this function when building a display list since we do a lot of trig here. Input: inner_radius - radius of hole at center outer_radius - radius at center of teeth width - width of gear teeth - number of teeth tooth_depth - depth of tooth **/ static void gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth) { GLint i; GLfloat r0, r1, r2; GLfloat angle, da; GLfloat u, v, len; r0 = inner_radius; r1 = outer_radius - tooth_depth / 2.0; r2 = outer_radius + tooth_depth / 2.0; da = 2.0 * M_PI / teeth / 4.0; glShadeModel(GL_FLAT); glNormal3f(0.0, 0.0, 1.0); /* draw front face */ glBegin(GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0 * M_PI / teeth; glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); } glEnd(); /* draw front sides of teeth */ glBegin(GL_QUADS); da = 2.0 * M_PI / teeth / 4.0; for (i = 0; i < teeth; i++) { angle = i * 2.0 * M_PI / teeth; glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); } glEnd(); glNormal3f(0.0, 0.0, -1.0); /* draw back face */ glBegin(GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0 * M_PI / teeth; glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); } glEnd(); /* draw back sides of teeth */ glBegin(GL_QUADS); da = 2.0 * M_PI / teeth / 4.0; for (i = 0; i < teeth; i++) { angle = i * 2.0 * M_PI / teeth; glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); } glEnd(); /* draw outward faces of teeth */ glBegin(GL_QUAD_STRIP); for (i = 0; i < teeth; i++) { angle = i * 2.0 * M_PI / teeth; glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); u = r2 * cos(angle + da) - r1 * cos(angle); v = r2 * sin(angle + da) - r1 * sin(angle); len = sqrt(u * u + v * v); u /= len; v /= len; glNormal3f(v, -u, 0.0); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); glNormal3f(cos(angle), sin(angle), 0.0); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5); glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5); u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da); v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da); glNormal3f(v, -u, 0.0); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); glNormal3f(cos(angle), sin(angle), 0.0); } glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5); glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5); glEnd(); glShadeModel(GL_SMOOTH); /* draw inside radius cylinder */ glBegin(GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0 * M_PI / teeth; glNormal3f(-cos(angle), -sin(angle), 0.0); glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); } glEnd(); } static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; static GLfloat angle = 0.0; static void draw(void) { static GLfloat pos[4] = {10.0, 10.0, 10.0, 0.0}; static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0}; static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0}; static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0}; angle += 1; view_rotx += 0.3; view_roty += 0.5; view_rotz += 0.7; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLfloat h = (GLfloat) height / (GLfloat) width; glViewport(0, 0, (GLint) width, (GLint) height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -40.0); glLightfv(GL_LIGHT0, GL_POSITION, pos); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glRotatef(view_rotx, 1.0, 0.0, 0.0); glRotatef(view_roty, 0.0, 1.0, 0.0); glRotatef(view_rotz, 0.0, 0.0, 1.0); glPushMatrix(); glTranslatef(-3.0, -2.0, 0.0); glRotatef(angle, 0.0, 0.0, 1.0); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); gear(1.0, 4.0, 1.0, 20, 0.7); glPopMatrix(); glPushMatrix(); glTranslatef(3.1, -2.0, 0.0); glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green); gear(0.5, 2.0, 2.0, 10, 0.7); glPopMatrix(); glPushMatrix(); glTranslatef(-3.1, 4.2, 0.0); glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); gear(1.3, 2.0, 0.5, 10, 0.7); glPopMatrix(); glEnable(GL_NORMALIZE); glFinish(); } void enableCursor(int enable) { int fd = open("/dev/console", O_NONBLOCK); if (fd >= 0) { ioctl(fd, KDSETMODE, enable ? KD_TEXT : KD_GRAPHICS); close(fd); } } int main(int argc, char *argv[]) { int fd; void *addr; int length; struct ps3fb_ioctl_res res; uint32_t frame = 0; struct timeval tv; uint32_t time; uint32_t timeGlobal; // switch to graphics mode (disable cursor) enableCursor(0); // access framebuffer fd = open("/dev/fb0", O_RDWR); ioctl(fd, PS3FB_IOCTL_SCREENINFO, (unsigned long)&res); printf("xres: %d, yres: %d, xoff: %d, yoff: %d, num_frames: %d\n", res.xres, res.yres, res.xoff, res.yoff, res.num_frames); length = res.xres * res.yres * 4 * res.num_frames; addr = mmap(NULL, length, PROT_WRITE, MAP_SHARED, fd, 0); // stop flipping in kernel thread with vsync ioctl(fd, PS3FB_IOCTL_ON, 0); // get dimension memoryWidth = res.xres; memoryHeight = res.yres; width = res.xres - 2 * res.xoff; height = res.yres - 2 * res.yoff; // init OSMesa ctx = OSMesaCreateContextExt( OSMESA_ARGB, 16, 0, 0, NULL ); if (!ctx) { printf("OSMesaCreateContext failed!\n"); return 0; } // start global timing gettimeofday(&tv, NULL); timeGlobal = tv.tv_sec * 1000000 + tv.tv_usec; // draw test count = 500; long sum = 0; int i; for (i = 0; i < count; i++) { // wait for vsync interrupt */ uint32_t crt = 0; uint32_t* fb = 0; ioctl(fd, FBIO_WAITFORVSYNC, (unsigned long)&crt); // start timing gettimeofday(&tv, NULL); time = tv.tv_sec * 1000000 + tv.tv_usec; // set current frame for OSMesa fb = addr + frame * memoryWidth * 4 * memoryHeight; if (!OSMesaMakeCurrent(ctx, fb, GL_UNSIGNED_BYTE, width, height)) { printf("OSMesaMakeCurrent failed!\n"); exit(0); } OSMesaPixelStore(OSMESA_ROW_LENGTH, memoryWidth); OSMesaPixelStore(OSMESA_Y_UP, 0); // draw frame draw(); // if (i == 0) writeTga("test.tga", fb); // end timing gettimeofday(&tv, NULL); time = tv.tv_sec * 1000000 + tv.tv_usec - time; sum += time; // blit and flip with vsync request */ ioctl(fd, PS3FB_IOCTL_FSEL, (unsigned long)&frame); // switch frame frame = 1 - frame; } // end global timing gettimeofday(&tv, NULL); timeGlobal = tv.tv_sec * 1000000 + tv.tv_usec - timeGlobal; printf("avg time per frame: %ld us\n", sum / count); printf("fps: %d\n", count * 1000000 / timeGlobal); // start flipping in kernel thread with vsync ioctl(fd, PS3FB_IOCTL_OFF, 0); munmap(NULL, length); // close device close(fd); // back to text mode enableCursor(1); return 0; }