diff options
author | Kyle K <kylek389@gmail.com> | 2011-07-19 14:57:55 -0500 |
---|---|---|
committer | Kamil Kaminski <kamilkss@gmail.com> | 2011-07-19 14:57:55 -0500 |
commit | 260b4bdaf2d5ac9e432a5fc5b6c555499b36d3e8 (patch) | |
tree | 6ac7c02d614653a0eb44b7b674fdf0e6b6148d46 /sdl | |
parent | f7d6c6be3413438afef1c3319db3df5c184ead12 (diff) | |
download | GLPyramid-260b4bdaf2d5ac9e432a5fc5b6c555499b36d3e8.tar.gz GLPyramid-260b4bdaf2d5ac9e432a5fc5b6c555499b36d3e8.tar.bz2 GLPyramid-260b4bdaf2d5ac9e432a5fc5b6c555499b36d3e8.zip |
sdl: tidy up the comments, format the code
Diffstat (limited to 'sdl')
-rw-r--r-- | sdl/GL_notes.txt | 9 | ||||
-rw-r--r-- | sdl/client.h | 1 | ||||
-rw-r--r-- | sdl/event.c | 55 | ||||
-rw-r--r-- | sdl/gldraw.c | 68 | ||||
-rw-r--r-- | sdl/platform.c | 13 | ||||
-rw-r--r-- | sdl/platform.h | 2 | ||||
-rw-r--r-- | sdl/pyramid.c | 66 |
7 files changed, 137 insertions, 77 deletions
diff --git a/sdl/GL_notes.txt b/sdl/GL_notes.txt index ac51035..2c77ed9 100644 --- a/sdl/GL_notes.txt +++ b/sdl/GL_notes.txt @@ -35,6 +35,15 @@ Buffers - Front Buffer - Back Buffer +Lighting +- when lighting is enabled, we need to specify normals for each polygon face so + OpenGL can calculate e.g. how light reflects + +Fake Shadow +- drawing a shadow for the pyramid would require drawing things twice, so on 2nd + pass we would draw with black color and multiply by squished matrix, we should + get to this later with better approach + Terms - Dithering: simulate displaying wider range of colors on on systems with small amount of colors diff --git a/sdl/client.h b/sdl/client.h index b576a83..3116604 100644 --- a/sdl/client.h +++ b/sdl/client.h @@ -9,6 +9,7 @@ struct client /* display lists identifiers */ GLuint ground_list; GLuint triangle_list; + GLuint figures_list; /* pyramid texture handle */ GLuint textures[2]; diff --git a/sdl/event.c b/sdl/event.c index a760635..32c2866 100644 --- a/sdl/event.c +++ b/sdl/event.c @@ -117,6 +117,9 @@ void event_keydown_gen(SDL_keysym *keysym) case SDLK_ESCAPE: event_handler.funcs.quit(); break; + case SDLK_r: + glframe_reset(&(*event_handler.p).camera); + break; default: break; } @@ -133,7 +136,7 @@ void event_keydown(SDL_keysym *keysym) const unsigned int *keys_held = event_handler.keys_held; struct platform *p = event_handler.p; - /* a flag is toggled on if the we are interested in keys that are being held */ + /* a flag is toggled on if the keys that we are interested in are being held */ if (event_handler.key_held) { if (keys_held[SDLK_w]) @@ -163,18 +166,44 @@ void event_keydown(SDL_keysym *keysym) { switch (keysym->sym) { - case SDLK_ESCAPE: program_running = 0; break; - case SDLK_w: p->c->xrot -= 5.0f; break; - case SDLK_s: p->c->xrot += 5.0f; break; - case SDLK_a: p->c->yrot -= 5.0f; break; - case SDLK_d: p->c->yrot += 5.0f; break; - case SDLK_UP: glframe_move_forward(&p->camera, 0.5f); break; - case SDLK_DOWN: glframe_move_forward(&p->camera, -0.5f); break; - case SDLK_LEFT: glframe_rotate_local_y(&p->camera, 0.1f); break; - case SDLK_RIGHT: glframe_rotate_local_y(&p->camera, -0.1f); break; - case SDLK_n: glframe_rotate_local_x(&p->camera, 0.1f); break; - case SDLK_m: glframe_rotate_local_x(&p->camera, -0.1f); break; - default: break; + case SDLK_ESCAPE: + program_running = 0; + break; + case SDLK_r: + glframe_reset(&(*event_handler.p).camera); + break; + case SDLK_w: + p->c->xrot -= 5.0f; + break; + case SDLK_s: + p->c->xrot += 5.0f; + break; + case SDLK_a: + p->c->yrot -= 5.0f; + break; + case SDLK_d: + p->c->yrot += 5.0f; + break; + case SDLK_UP: + glframe_move_forward(&p->camera, 0.5f); + break; + case SDLK_DOWN: + glframe_move_forward(&p->camera, -0.5f); + break; + case SDLK_LEFT: + glframe_rotate_local_y(&p->camera, 0.1f); + break; + case SDLK_RIGHT: + glframe_rotate_local_y(&p->camera, -0.1f); + break; + case SDLK_n: + glframe_rotate_local_x(&p->camera, 0.1f); + break; + case SDLK_m: + glframe_rotate_local_x(&p->camera, -0.1f); + break; + default: + break; } } diff --git a/sdl/gldraw.c b/sdl/gldraw.c index b4e3be7..ef01fac 100644 --- a/sdl/gldraw.c +++ b/sdl/gldraw.c @@ -69,49 +69,43 @@ void glDrawSnowman(void) /* this screws up the rendering, possibly becasue it's glut? */ void glDrawFigures(void) { - glDisable(GL_TEXTURE_2D); - glDisable(GL_LIGHTING); - glPushMatrix(); - /* scale the figures */ - glScalef(0.02f, 0.02f, 0.02f); + /* scale the figures */ + glScalef(0.02f, 0.02f, 0.02f); - /* draw red cube */ - glColor3f(1.0f, 0.0f, 0.0f); - glPushMatrix(); - glutSolidCube(48.0f); - glPopMatrix(); + /* draw red cube */ + glColor3f(1.0f, 0.0f, 0.0f); + glPushMatrix(); + glutSolidCube(48.0f); + glPopMatrix(); - /* draw green sphere */ - glColor3f(0.0f, 1.0f, 0.0f); - glPushMatrix(); - glTranslatef(-60.0f, 0.0f, 0.0f); - glutSolidSphere(25.0f, 50, 50); - glPopMatrix(); + /* draw green sphere */ + glColor3f(0.0f, 1.0f, 0.0f); + glPushMatrix(); + glTranslatef(-60.0f, 0.0f, 0.0f); + glutSolidSphere(25.0f, 50, 50); + glPopMatrix(); - /* draw magenta torus */ - glColor3f(1.0f, 0.0f, 1.0f); - glPushMatrix(); - glTranslatef(0.0f, 0.0f, 60.0f); - glutSolidTorus(8.0f, 16.0f, 50, 50); - glPopMatrix(); + /* draw magenta torus */ + glColor3f(1.0f, 0.0f, 1.0f); + glPushMatrix(); + glTranslatef(0.0f, 0.0f, 60.0f); + glutSolidTorus(8.0f, 16.0f, 50, 50); + glPopMatrix(); - /* draw yellow cone */ - glColor3f(1.0f, 1.0f, 0.0f); - glPushMatrix(); - glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); - glTranslatef(60.0f, 0.0f, -24.0f); - glutSolidCone(25.0f, 50.0f, 50, 50); - glPopMatrix(); + /* draw yellow cone */ + glColor3f(1.0f, 1.0f, 0.0f); + glPushMatrix(); + glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); + glTranslatef(60.0f, 0.0f, -24.0f); + glutSolidCone(25.0f, 50.0f, 50, 50); + glPopMatrix(); - /* draw cyan teapot */ - glColor3f(0.0f, 1.0f, 1.0f); - glPushMatrix(); - glTranslatef(0.0f, 0.0f, -60.0f); - glutSolidTeapot(25.0f); - glPopMatrix(); + /* draw cyan teapot */ + glColor3f(0.0f, 1.0f, 1.0f); + glPushMatrix(); + glTranslatef(0.0f, 0.0f, -60.0f); + glutSolidTeapot(25.0f); glPopMatrix(); - glEnable(GL_LIGHTING); - glEnable(GL_TEXTURE_2D); } void glDrawTriangle(void) diff --git a/sdl/platform.c b/sdl/platform.c index 1fd9129..c4e11ca 100644 --- a/sdl/platform.c +++ b/sdl/platform.c @@ -42,6 +42,12 @@ extern unsigned int maxfps_w; void platform_init(struct platform *p) { + if (!p) + { + fprintf(stderr, "platform: failed to initialize, parameter = NULL\n"); + exit(-1); + } + int ret; /* Lua */ @@ -93,8 +99,11 @@ void platform_destroy(struct platform *p) } /* load Lua config file from the disk */ -void load_config(struct platform *p) +int load_config(struct platform *p) { + if (!p) + return -1; + if (luaLoadConfig(p->L, "config.lua") || luaFillTablePlatform(p->L, &p->config_table) != -1) { @@ -123,6 +132,8 @@ void load_config(struct platform *p) if (p->config_table.maxfps) maxfps_w = p->config_table.maxfps; } + + return 0; } void setup_opengl(void) diff --git a/sdl/platform.h b/sdl/platform.h index d546749..f9f2a48 100644 --- a/sdl/platform.h +++ b/sdl/platform.h @@ -39,7 +39,7 @@ extern const GLfloat lightPos[]; /* function prototypes */ void platform_init(struct platform *); void platform_destroy(struct platform *); -void load_config(struct platform *); +int load_config(struct platform *); void setup_opengl(void); SDL_Surface *setup_sdl_video(int, int, int, unsigned int); SDL_Surface *setup_sdl(void); diff --git a/sdl/pyramid.c b/sdl/pyramid.c index ff8a03f..316abe5 100644 --- a/sdl/pyramid.c +++ b/sdl/pyramid.c @@ -1,18 +1,11 @@ /* pyramid.c * - * @2011 Kamil Kaminski + * Kamil Kaminski + * kkaminsk.com * - * notes: since lighting is enabled, we need to specify normals for - * each polygon face so the OpenGL can calculate e.g. how light reflects + * Textured Pyramid, + * running on a homebrew framework * - * drawing a shadow for the pyramid would require drawing things twice, so on 2nd - * 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 - * - * it might be time to split the code, and make a shader version... * */ @@ -54,8 +47,9 @@ static int client_init(struct client *p) p->textures[1] = gltLoadTGATexture("grass.tga"); /* display list, precompile commands */ - p->ground_list = glGenLists(2); + p->ground_list = glGenLists(3); p->triangle_list = p->ground_list + 1; + p->figures_list = p->ground_list + 2; /* a triangle with a texture */ glNewList(p->triangle_list, GL_COMPILE); @@ -71,13 +65,22 @@ static int client_init(struct client *p) glDrawGround(); glEndList(); + /* figures */ + glNewList(p->figures_list, GL_COMPILE); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDrawFigures(); + glEnable(GL_LIGHTING); + glEnable(GL_TEXTURE_2D); + glEndList(); + return 0; } static int client_destroy(struct client *p) { glDeleteTextures(2, p->textures); - glDeleteLists(p->ground_list, 2); + glDeleteLists(p->ground_list, 3); return 0; } @@ -95,25 +98,36 @@ static void render(struct platform *p) /* save the matrix state and do the rotations */ glPushMatrix(); /* apply camera transform, and draw the ground */ - glframe_apply_camera_transform(&p->camera, 1); + glframe_apply_camera_transform(&p->camera, 0); glCallList(p->c->ground_list); glPushMatrix(); - /* move object back and do in place rotation */ - glTranslatef(0.0f, 0.2f, -3.5f); - glRotatef(p->c->xrot, 1.0f, 0.0f, 0.0f); - glRotatef(p->c->yrot, 0.0f, 1.0f, 0.0f); + /* move object back and do in place rotation */ + glTranslatef(0.0f, 0.2f, -3.5f); + glRotatef(p->c->xrot, 1.0f, 0.0f, 0.0f); + glRotatef(p->c->yrot, 0.0f, 1.0f, 0.0f); - /* draw the pyramid */ - glCallList(p->c->triangle_list); + /* draw the pyramid */ + glCallList(p->c->triangle_list); glPopMatrix(); - /* draw a snowman */ - glTranslatef(0.0f, 0.0f, -7.0f); + glPushMatrix(); + /* draw a snowman */ + glDisable(GL_TEXTURE_2D); + int i; + for (i = 0; i < 3; i++) + { + glTranslatef(0.0f, 0.0f, -6.0f); + glDrawSnowman(); + } + glEnable(GL_TEXTURE_2D); + glPopMatrix(); - glDisable(GL_TEXTURE_2D); - glDrawSnowman(); - glEnable(GL_TEXTURE_2D); + glPushMatrix(); + /* draw figures */ + glTranslatef(0.0f, 4.0f, 0.0f); + glCallList(p->c->figures_list); + glPopMatrix(); /* restore the matrix state */ glPopMatrix(); @@ -124,6 +138,8 @@ static void render(struct platform *p) int main(int argc, char **argv) { + glutInit(&argc, argv); + struct platform p; memset(&p, 0, sizeof(p)); |