/* pyramid.c * * Kamil Kaminski * kkaminsk.com * * Textured Pyramid, * running on a homebrew framework * * */ #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" /* 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(struct platform *); 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(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); 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(); /* 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, 3); return 0; } static inline void process_events(void) { sdl_process_events(); } static void render(struct platform *p) { /* 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, 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); /* draw the pyramid */ glCallList(p->c->triangle_list); glPopMatrix(); 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(); glPushMatrix(); /* draw figures */ glTranslatef(0.0f, 4.0f, 0.0f); glCallList(p->c->figures_list); glPopMatrix(); /* restore the matrix state */ glPopMatrix(); /* buffer swap */ SDL_GL_SwapBuffers(); } int main(int argc, char **argv) { glutInit(&argc, argv); struct platform p; memset(&p, 0, sizeof(p)); /* setup client platform */ struct client c; memset(&c, 0, sizeof(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(&p); sdlFrameControl(startclock); } platform_destroy(&p); puts("bye!"); return 0; }