Created
September 27, 2011 22:01
-
-
Save davidwparker/1246381 to your computer and use it in GitHub Desktop.
OpenGL Screencast 11 part 2: Code Organization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| void initializeGlobals() | |
| { | |
| windowName = "OpenGL screenscasts 11: Code Organization"; | |
| windowWidth = 500; | |
| windowHeight = 450; | |
| toggleAxes = 0; | |
| toggleValues = 1; | |
| toggleMode = 0; | |
| dim = 5.0; | |
| th = 340; | |
| ph = 30; | |
| fov = 55; | |
| asp = 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void initializeGlobals(void); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* | |
| * windowKey() | |
| * ------ | |
| * GLUT calls this routine when a non-special key is pressed | |
| */ | |
| void windowKey(unsigned char key,int x,int y) | |
| { | |
| /* Exit on ESC */ | |
| if (key == 27) exit(0); | |
| else if (key == 'a' || key == 'A') toggleAxes = 1-toggleAxes; | |
| else if (key == 'v' || key == 'V') toggleValues = 1-toggleValues; | |
| else if (key == 'm' || key == 'M') toggleMode = 1-toggleMode; | |
| /* Change field of view angle */ | |
| else if (key == '-' && key>1) fov--; | |
| else if (key == '+' && key<179) fov++; | |
| /* Change dimensions */ | |
| else if (key == 'D') dim += 0.1; | |
| else if (key == 'd' && dim>1) dim -= 0.1; | |
| displayProject(); | |
| glutPostRedisplay(); | |
| } | |
| /* | |
| * windowMenu | |
| * ------ | |
| * Window menu is the same as the keyboard clicks | |
| */ | |
| void windowMenu(int value) | |
| { | |
| windowKey((unsigned char)value, 0, 0); | |
| } | |
| /* | |
| * windowSpecial() | |
| * ------ | |
| * GLUT calls this routine when an arrow key is pressed | |
| */ | |
| void windowSpecial(int key,int x,int y) | |
| { | |
| /* Right arrow key - increase azimuth by 5 degrees */ | |
| if (key == GLUT_KEY_RIGHT) th += 5; | |
| /* Left arrow key - decrease azimuth by 5 degrees */ | |
| else if (key == GLUT_KEY_LEFT) th -= 5; | |
| /* Up arrow key - increase elevation by 5 degrees */ | |
| else if (key == GLUT_KEY_UP) ph += 5; | |
| /* Down arrow key - decrease elevation by 5 degrees */ | |
| else if (key == GLUT_KEY_DOWN) ph -= 5; | |
| /* Keep angles to +/-360 degrees */ | |
| th %= 360; | |
| ph %= 360; | |
| displayProject(); | |
| glutPostRedisplay(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void windowKey(unsigned char key,int x,int y); | |
| void windowMenu(int value); | |
| void windowSpecial(int key,int x,int y); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* | |
| * spike | |
| * ------ | |
| * Draw a spike | |
| * at (x, y, z) | |
| * radius r, height h, with 360/deg sides | |
| * rotated ox around the x axis | |
| * rotated oy around the y axis | |
| * rotated oz around the z axis | |
| */ | |
| void spike(double x, double y, double z, | |
| double r,double h,int deg, | |
| double ox,double oy,double oz) | |
| { | |
| glPushMatrix(); | |
| glRotated(oz,0,0,1); | |
| glRotated(oy,0,1,0); | |
| glRotated(ox,1,0,0); | |
| cone(x,y,z, r,h,deg); | |
| glPopMatrix(); | |
| } | |
| /* | |
| * towers | |
| * ------ | |
| * Draw a tower | |
| * at (x,y,z) | |
| * dimensions (dx,dy,dz) | |
| * rotated th about the y axis | |
| */ | |
| void tower(double x,double y,double z, | |
| double dx,double dy,double dz, | |
| double th) | |
| { | |
| glPushMatrix(); | |
| /* Transformation */ | |
| glTranslated(x,y,z); | |
| glRotated(th,0,1,0); | |
| glScaled(dx,dy,dz); | |
| cube(0,1.5,0, 1,3,1, 0); | |
| cube(0,3.5,0, 2,1,2, 45); | |
| spike(0,1,-3.5, 0.5,1, 90, 90,0,0); | |
| spike(0,1,3.5, 0.5,1, 90, -90,0,0); | |
| spike(-3.5,1,0, 0.5,1, 90, 0,0,-90); | |
| spike(3.5,1,0, 0.5,1, 90, 0,0,90); | |
| glPopMatrix(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void spike(double x, double y, double z, | |
| double r,double h,int deg, | |
| double ox,double oy,double oz); | |
| void tower(double x,double y,double z, | |
| double dx,double dy,double dz, | |
| double th); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| #define LEN 8192 | |
| void printv(va_list args, const char* format) | |
| { | |
| char buf[LEN]; | |
| char* ch=buf; | |
| vsnprintf(buf,LEN,format,args); | |
| while (*ch) | |
| glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,*ch++); | |
| } | |
| void print(const char* format, ...) | |
| { | |
| va_list args; | |
| va_start(args,format); | |
| printv(args,format); | |
| va_end(args); | |
| } | |
| void printAt(int x, int y, const char* format, ...) | |
| { | |
| va_list args; | |
| glWindowPos2i(x,y); | |
| va_start(args,format); | |
| printv(args,format); | |
| va_end(args); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void printv(va_list args, const char* format); | |
| void print(const char* format, ...); | |
| void printAt(int x, int y, const char* format, ...); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef SCREENCASTS | |
| #define SCREENCASTS | |
| /* standard headers */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdarg.h> | |
| #include <string.h> | |
| #include <math.h> | |
| #include <time.h> | |
| /* OpenGL and friends */ | |
| #ifdef USEGLEW | |
| #include <GL/glew.h> | |
| #endif | |
| #define GL_GLEXT_PROTOTYPES | |
| #ifdef __APPLE__ | |
| #include <GLUT/glut.h> | |
| #else | |
| #include <GL/glut.h> | |
| #endif | |
| /* includes */ | |
| #include "common.h" /* common is just defines */ | |
| #include "print.h" /* printing functions */ | |
| #include "error.h" /* error convenience */ | |
| #include "shapes.h" /* basic shapes (cube, cone, etc) */ | |
| #include "models.h" /* complex objects */ | |
| #include "interaction.h" /* user interactions (keyboard, mouse, etc) */ | |
| #include "initialization.h" /* initialization */ | |
| #include "draw.h" /* draw -> draw whatever objects in the scene */ | |
| #include "display.h" /* display -> setup scene to draw */ | |
| /* GLOBALS (externs required here) */ | |
| /* Don't forget to initialize globals! */ | |
| /* window info */ | |
| extern char *windowName; | |
| extern int windowWidth; | |
| extern int windowHeight; | |
| /* toggle views */ | |
| extern int toggleAxes; /* toggle axes on and off */ | |
| extern int toggleValues; /* toggle values on and off */ | |
| extern int toggleMode; /* projection mode */ | |
| /* view */ | |
| extern double dim;/* dimension of orthogonal box */ | |
| extern int th; /* azimuth of view angle */ | |
| extern int ph; /* elevation of view angle */ | |
| extern int fov; /* field of view for perspective */ | |
| extern int asp; /* aspect ratio */ | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* | |
| * cube | |
| * ------ | |
| * Draw a cube | |
| * at (x,y,z) | |
| * dimensions (dx,dy,dz) | |
| * rotated th about the y axis | |
| */ | |
| void cube(double x,double y,double z, | |
| double dx,double dy,double dz, | |
| double th) | |
| { | |
| /* Cube vertices */ | |
| GLfloat vertA[3] = { 0.5, 0.5, 0.5}; | |
| GLfloat vertB[3] = {-0.5, 0.5, 0.5}; | |
| GLfloat vertC[3] = {-0.5,-0.5, 0.5}; | |
| GLfloat vertD[3] = { 0.5,-0.5, 0.5}; | |
| GLfloat vertE[3] = { 0.5, 0.5,-0.5}; | |
| GLfloat vertF[3] = {-0.5, 0.5,-0.5}; | |
| GLfloat vertG[3] = {-0.5,-0.5,-0.5}; | |
| GLfloat vertH[3] = { 0.5,-0.5,-0.5}; | |
| glPushMatrix(); | |
| /* Transform cube */ | |
| glTranslated(x,y,z); | |
| glRotated(th,0,1,0); | |
| glScaled(dx,dy,dz); | |
| /* Cube */ | |
| glBegin(GL_QUADS); | |
| /* front => ABCD yellow */ | |
| glColor3f(1.0,1.0,0.0); | |
| glVertex3fv(vertA); | |
| glVertex3fv(vertB); | |
| glVertex3fv(vertC); | |
| glVertex3fv(vertD); | |
| /* back => FEHG red */ | |
| glColor3f(1.0,0.0,0.0); | |
| glVertex3fv(vertF); | |
| glVertex3fv(vertE); | |
| glVertex3fv(vertH); | |
| glVertex3fv(vertG); | |
| /* right => EADH green */ | |
| glColor3f(0.0,1.0,0.0); | |
| glVertex3fv(vertE); | |
| glVertex3fv(vertA); | |
| glVertex3fv(vertD); | |
| glVertex3fv(vertH); | |
| /* left => BFGC blue */ | |
| glColor3f(0.0,0.0,1.0); | |
| glVertex3fv(vertB); | |
| glVertex3fv(vertF); | |
| glVertex3fv(vertG); | |
| glVertex3fv(vertC); | |
| /* top => EFBA turquoise */ | |
| glColor3f(0.0,1.0,1.0); | |
| glVertex3fv(vertE); | |
| glVertex3fv(vertF); | |
| glVertex3fv(vertB); | |
| glVertex3fv(vertA); | |
| /* bottom => DCGH pink */ | |
| glColor3f(1.0,0.0,1.0); | |
| glVertex3fv(vertD); | |
| glVertex3fv(vertC); | |
| glVertex3fv(vertG); | |
| glVertex3fv(vertH); | |
| glEnd(); | |
| glPopMatrix(); | |
| } | |
| /* | |
| * cone | |
| * ------ | |
| * Draws a cone | |
| * at (x,y,z) | |
| * with radius r and height h | |
| * with 360/deg sides | |
| */ | |
| void cone(double x,double y,double z, | |
| double r,double h,int deg) | |
| { | |
| int k; | |
| glPushMatrix(); | |
| /* Transformation */ | |
| glTranslated(x,y,z); | |
| glScaled(r,h,r); | |
| glRotated(-90,1,0,0); | |
| /* sides */ | |
| glBegin(GL_TRIANGLES); | |
| for (k=0;k<=360;k+=deg){ | |
| glColor3f(0.0,0.0,1.0); | |
| glVertex3f(0,0,1); | |
| glColor3f(0.0,1.0,1.0); | |
| glVertex3f(Cos(k),Sin(k),0); | |
| glColor3f(1.0,0.0,1.0); | |
| glVertex3f(Cos(k+deg),Sin(k+deg),0); | |
| } | |
| glEnd(); | |
| /* bottom circle */ | |
| /* rotate back */ | |
| glRotated(90,1,0,0); | |
| glBegin(GL_TRIANGLES); | |
| glColor3f(1.0,1.0,0.0); | |
| for (k=0;k<=360;k+=deg) { | |
| glVertex3f(0,0,0); | |
| glVertex3f(Cos(k),0,Sin(k)); | |
| glVertex3f(Cos(k+deg),0,Sin(k+deg)); | |
| } | |
| glEnd(); | |
| glPopMatrix(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment