/* pyramid.c * * @2011 Kamil Kaminski * * notes: since lighting is enabled, we need to specify normals for * each polygon face so the OpenGL can calculate e.g. how light reflects * * 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... * * ToDo: on linux, update the makefile dependencies, and check out lua-config * */ #include #include #include #include #include #include "client.h" #include "event.h" #include "gldraw.h" #include "glframe.h" #include "gltools.h" #include "math3d.h" #include "platform.h" #include "sdltools.h" #include "window.h" /* globals */ struct platform p; /* sane defaults, these are overwritable by config.lua */ int program_running = 1; unsigned int xres_w = 640; unsigned int yres_w = 480; unsigned int bpp_w = 32; unsigned int af_w = 8; const char *window_caption = "Textured Pyramid"; const char *window_icon_path = "tux.png"; const unsigned int sdl_video_flags = SDL_OPENGL | SDL_RESIZABLE; unsigned int maxfps_w = 60; /* function prototypes */ static int client_init(struct client *); static int client_destroy(struct client *); static inline void process_events(void); static void render(void); static int client_init(struct client *p) { p->textures[0] = gltLoadTGATexture("stone.tga"); p->textures[1] = gltLoadTGATexture("grass.tga"); /* display list, precompile commands */ p->ground_list = glGenLists(2); p->triangle_list = p->ground_list + 1; /* a triangle with a texture */ glNewList(p->triangle_list, GL_COMPILE); glBindTexture(GL_TEXTURE_2D, p->textures[0]); /* glBindTexture(GL_TEXTURE_2D, p->textures[1]); */ glColor3f(1.0f, 1.0f, 1.0f); glDrawTriangle(); glEndList(); /* a ground drawn using magneta lines */ glNewList(p->ground_list, GL_COMPILE); glColor3ub(255, 0, 255); glDrawGround(); glEndList(); return 0; } static int client_destroy(struct client *p) { glDeleteTextures(2, p->textures); glDeleteLists(p->ground_list, 2); return 0; } static inline void process_events(void) { sdl_process_events(); } static void render(void) { /* clear the window with current clearing color */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* save the matrix state and do the rotations */ glPushMatrix(); /* apply camera transform, and draw the ground */ glframe_apply_camera_transform(&p.camera, 1); 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); /* draw the pyramid */ glCallList(p.c->triangle_list); glPopMatrix(); /* draw a snowman */ glTranslatef(0.0f, 0.0f, -7.0f); glDisable(GL_TEXTURE_2D); glDrawSnowman(); glEnable(GL_TEXTURE_2D); /* restore the matrix state */ glPopMatrix(); /* buffer swap */ SDL_GL_SwapBuffers(); } int main(int argc, char **argv) { memset(&p, 0, sizeof(p)); /* setup client platform */ struct client c; p.c = &c; p.client_init = &client_init; p.client_destroy = &client_destroy; /* initialize host platform */ platform_init(&p); gltErrorCheck(); gltOpenGLInfo(); unsigned int startclock; while (program_running) { startclock = SDL_GetTicks(); process_events(); render(); sdlFrameControl(startclock); } platform_destroy(&p); puts("bye!"); return 0; }