diff options
-rw-r--r-- | sdl/GL_todo.txt | 1 | ||||
-rw-r--r-- | sdl/gltools.c | 33 | ||||
-rw-r--r-- | sdl/gltools.h | 3 | ||||
-rw-r--r-- | sdl/pyramid.c | 64 |
4 files changed, 53 insertions, 48 deletions
diff --git a/sdl/GL_todo.txt b/sdl/GL_todo.txt index 0a38253..2543f5e 100644 --- a/sdl/GL_todo.txt +++ b/sdl/GL_todo.txt @@ -12,4 +12,5 @@ todo: - split SDL - reflection, build on blending, needs stencil magic? - draw inhabitants, that would accept a texture id +- fps counter in seperate file, could draw to SDL surface diff --git a/sdl/gltools.c b/sdl/gltools.c index d6c518f..756112b 100644 --- a/sdl/gltools.c +++ b/sdl/gltools.c @@ -2,6 +2,8 @@ * * OpenGL Tools * + * notes: some function modify current color, just fyi + * */ #include "math3d.h" @@ -235,6 +237,25 @@ void gltDrawTriangle(void) glEnd(); } +void gltDrawGround(void) +{ + GLfloat fExtent = 20.0f; + GLfloat fStep = 0.5f; + GLfloat y = -0.4f; + GLfloat iLine; + + glLineWidth(1.0f); + glBegin(GL_LINES); + for (iLine = -fExtent; iLine <= fExtent; iLine += fStep) + { + glVertex3f(iLine, y, fExtent); + glVertex3f(iLine, y, -fExtent); + glVertex3f(fExtent, y, iLine); + glVertex3f(-fExtent, y, iLine); + } + glEnd(); +} + void gltDrawUnitAxes(void) { GLUquadricObj *pObj; /* temporary, used for quadrics */ @@ -345,20 +366,10 @@ void gltDrawTorus(GLfloat majorRadius, GLfloat minorRadius, GLint numMajor, } } -/* this function just specifically draws the jet */ -/* FIXME: needs to accepts parameters of location and lightning */ -void gltDrawJet(int nShadow) +void gltDrawJet(void) { M3DVector3f vNormal; - /* nose cone, set material color, note we only have to set to black - * for the shadow once - */ - if (nShadow == 0) - glColor3ub(128, 128, 128); - else - glColor3ub(0, 0, 0); - /* nose cone, points straight down, set material color */ /* follow few lines use manual approach */ glBegin(GL_TRIANGLES); diff --git a/sdl/gltools.h b/sdl/gltools.h index 9d5b819..61001d2 100644 --- a/sdl/gltools.h +++ b/sdl/gltools.h @@ -25,9 +25,10 @@ typedef struct GLint gltWriteTGA(const char *); GLbyte *gltLoadTGA(const char *, GLint *, GLint *, GLint *, GLenum *); void gltDrawTriangle(void); +void gltDrawGround(void); void gltDrawUnitAxes(void); void gltDrawTorus(GLfloat, GLfloat, GLint, GLint); -void gltDrawJet(int); +void gltDrawJet(void); #endif diff --git a/sdl/pyramid.c b/sdl/pyramid.c index c8595b2..5959e02 100644 --- a/sdl/pyramid.c +++ b/sdl/pyramid.c @@ -9,6 +9,9 @@ * pass we would draw with black color and multiply by squished matrix, we should * get to this later with better approach * + * so far we have been using immediate mode rendering, we should begin using + * display lists / batch processing to reduce overhead, aka compiling commands + * */ #include <SDL/SDL.h> @@ -20,6 +23,8 @@ #include <sys/time.h> #define FRAMES_PER_SECOND 60 +/* function prototypes */ + /* global */ int program_running = 1; GLFrame camera; @@ -55,7 +60,7 @@ static void resize(int w, int h) SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_RESIZABLE); } -static void SetupRC() +static void setup_opengl(void) { GLbyte *pBytes; GLint iWidth, iHeight, iComponents; @@ -71,11 +76,11 @@ static void SetupRC() glFogfv(GL_FOG_COLOR, fLowLight); /* set fog color to match background */ glFogf(GL_FOG_START, 4.0f); glFogf(GL_FOG_END, 20.0f); - glFogi(GL_FOG_MODE, GL_LINEAR); /* fog equation */ + glFogi(GL_FOG_MODE, GL_LINEAR); /* fog equation */ - glEnable(GL_DEPTH_TEST); /* hidden surface removal */ - glFrontFace(GL_CCW); /* counter clock-wise polygons face out */ - glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */ + glEnable(GL_DEPTH_TEST);/* hidden surface removal */ + glFrontFace(GL_CCW); /* counter clock-wise polygons face out */ + glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */ /* enable lighting */ glEnable(GL_LIGHTING); @@ -87,7 +92,7 @@ static void SetupRC() glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight); glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight); glLightfv(GL_LIGHT0, GL_POSITION, lightPos); - glEnable(GL_LIGHTING); + glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); /* enable color tracking */ @@ -96,7 +101,7 @@ static void SetupRC() /* set Material properties to follow glColor values */ glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight); - glMateriali(GL_FRONT, GL_SHININESS, 128); + glMateriali(GL_FRONT, GL_SHININESS, 128); /* turn on anti aliasing for points, lines, and polygons */ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -106,7 +111,7 @@ static void SetupRC() glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); glEnable(GL_POLYGON_SMOOTH); - glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); + glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); /* gray background */ glClearColor(0.5f, 0.5f, 0.5f, 1.0f); @@ -160,7 +165,7 @@ static void keys(SDL_keysym *keysym, unsigned int *keys_held, int flag) case SDLK_UP: move_forward(&camera, 0.5f); break; case SDLK_DOWN: move_forward(&camera, -0.5f); break; case SDLK_LEFT: rotate_local_y(&camera, 0.1f); break; - case SDLK_RIGHT: rotate_local_y(&camera, -0.1f); break; + case SDLK_RIGHT: rotate_local_y(&camera, -0.1f); break; default: break; } } @@ -182,7 +187,7 @@ static void keys(SDL_keysym *keysym, unsigned int *keys_held, int flag) if (keys_held[SDLK_LEFT]) rotate_local_y(&camera, 0.02f); if (keys_held[SDLK_RIGHT]) - rotate_local_y(&camera, -0.02f); + rotate_local_y(&camera, -0.02f); } xRot = (GLfloat) ((const int) xRot % 360); @@ -190,7 +195,7 @@ static void keys(SDL_keysym *keysym, unsigned int *keys_held, int flag) } /* process SDL events */ -void processEvents() +static void process_events() { SDL_Event event; unsigned static int keys_held[323]; @@ -222,8 +227,7 @@ void processEvents() } } - /* XXX below code has to be placed here */ - /* check for keys that are being constantly held */ + /* below code has to be placed here, check for keys that are being constantly held */ if (keys_held[SDLK_w] || keys_held[SDLK_s] || keys_held[SDLK_a] || keys_held[SDLK_d] || keys_held[SDLK_UP] || keys_held[SDLK_DOWN] || keys_held[SDLK_LEFT] || keys_held[SDLK_RIGHT]) { @@ -231,26 +235,7 @@ void processEvents() keys(NULL, keys_held, flag); } else - flag = !flag; -} - -static void draw_ground(void) -{ - GLfloat fExtent = 20.0f; - GLfloat fStep = 0.5f; - GLfloat y = -0.4f; - GLfloat iLine; - - glLineWidth(1.0f); - glBegin(GL_LINES); - for (iLine = -fExtent; iLine <= fExtent; iLine += fStep) - { - glVertex3f(iLine, y, fExtent); - glVertex3f(iLine, y, -fExtent); - glVertex3f(fExtent, y, iLine); - glVertex3f(-fExtent, y, iLine); - } - glEnd(); + flag = !flag; } static void render(void) @@ -263,7 +248,7 @@ static void render(void) /* apply camera transform, and draw the ground */ apply_camera_transform(&camera); glColor3ub(255, 0, 255); - draw_ground(); + gltDrawGround(); /* move object back and do in place rotation */ glTranslatef(0.0f, 0.2f, -3.5f); @@ -319,7 +304,14 @@ int main(int argc, char **argv) else fprintf(stdout, "status: using GLEW %s\n", glewGetString(GLEW_VERSION)); - SetupRC(); + /* display OpenGL version */ + GLint major; + GLint minor; + glGetIntegerv(GL_MAJOR_VERSION, &major); + glGetIntegerv(GL_MINOR_VERSION, &minor); + fprintf(stdout, "version: OpenGL %d.%d\n", major, minor); + + setup_opengl(); #ifdef STAT_FPS /* fps counter */ @@ -335,7 +327,7 @@ int main(int argc, char **argv) #ifdef STAT_FPS startclock = SDL_GetTicks(); #endif - processEvents(); + process_events(); render(); gettimeofday(&lcurrent, 0); |