#include <SDL/SDL.h> #include <SDL/SDL_opengl.h> #include <stdio.h>
SDL_Surface* screen; SDL_Event input; int loop = 1;
/*z positions*/ float redZ = -3; float greenZ = -5; float blueZ = -4;
int main(int argc, char** argv) { SDL_Init(SDL_INIT_EVERYTHING); /*initialize SDL*/ screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
/*print depths of drawn shapes*/ printf("depth of red square = %g\n", redZ); printf("depth of green square = %g\n", greenZ); printf("depth of blue square = %g\n", blueZ);
/*set clear color to golden yellow*/ glClearColor(0.8, 0.8, 0, 1.0); glClear(GL_COLOR_BUFFER_BIT);
/*setup depth buffering*/ glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); /*greater depth*/ glClearDepth(0); /*clear to minimum depth*/
/*create 3D camera*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70, 1.333, 1, 100);
/*camera set at origin*/ glMatrixMode(GL_MODELVIEW); glLoadIdentity();
while (loop) { /*clear the color and depth of each pixel*/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
while (SDL_PollEvent(&input)) { /*check exit button*/ if (input.type == SDL_QUIT) loop = 0; }
/*draw quadrilaterals*/ glBegin(GL_QUADS);
/*red square*/ glColor3ub(255, 0, 0); glVertex3f(-2, 1, redZ); glVertex3f(-2, -1, redZ); glVertex3f(0, -1, redZ); glVertex3f(0, 1, redZ);
/*green square*/ glColor3ub(0, 255, 0); glVertex3f(-1, 1, greenZ); glVertex3f(-1, -1, greenZ); glVertex3f(1, -1, greenZ); glVertex3f(1, 1, greenZ);
/*blue square*/ glColor3ub(0, 0, 255); glVertex3f(-1, 0, blueZ); glVertex3f(-1, -2, blueZ); glVertex3f(1, -2, blueZ); glVertex3f(1, 0, blueZ);
glEnd();
SDL_GL_SwapBuffers(); SDL_Delay(20); /*wait 20ms*/ }
/*perform final commands then exit*/ SDL_Quit(); /*close SDL*/ fflush(stdout); /*update stdout*/ return 0; }
|