/*thanks to tohtml.com for syntax highlighting*/
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <stdio.h>
SDL_Surface* screen;
SDL_Event input;
int loop = 1;
/*camera position*/
float camX = 2, camY = 2, camZ = 2;
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_EVERYTHING); /*initialize SDL*/
screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
/*print camera position*/
printf("camera position = (%g, %g, %g)\n", camX, camY, camZ);
/*set clear color to dark teal*/
glClearColor(0, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
/*create 3D camera*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 1.333, 1, 100);
glMatrixMode(GL_MODELVIEW); /*for transforming shapes*/
while (loop) {
glClear(GL_COLOR_BUFFER_BIT); /*clear screen*/
while (SDL_PollEvent(&input)) {
if (input.type == SDL_QUIT) loop = 0;
}
/*move camera away from shape, rotate to see shape*/
glLoadIdentity();
gluLookAt(camX, camY, camZ, 0, 0, 0, 0, 1, 0);
/*transformations which work in place of gluLookAt*/
/*glRotatef(35.26, 1, 0, 0); /*x-axis rotation*/
/*glRotatef(-45, 0, 1, 0); /*y-axis rotation*/
/*glTranslatef(-2, -2, -2); /*move to position*/
/*scale and translate the shape*/
glPushMatrix(); /*save current modelview*/
glTranslatef(-5, 0, 0);
glScalef(2, 1, 2);
/*draw quadrilateral*/
glBegin(GL_QUADS);
/*light teal square*/
glColor3ub(0, 255, 255);
glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, -1);
glEnd();
glPopMatrix(); /*restore previous modelview*/
/*scale the shape*/
glPushMatrix(); /*save current modelview*/
glScalef(2, 1, 1);
/*draw quadrilateral*/
glBegin(GL_QUADS);
/*multi-colored square*/
glColor3f(1, 1, 0); glVertex3f(-1, 0, -1);
glColor3f(1, 0, 0); glVertex3f(-1, 0, 1);
glColor3f(0, 1, 0); glVertex3f(1, 0, 1);
glColor3f(0, 0, 1); glVertex3f(1, 0, -1);
glEnd();
glPopMatrix(); /*restore previous modelview*/
SDL_GL_SwapBuffers();
SDL_Delay(20); /*wait 20ms*/
}
/*perform final commands then exit*/
SDL_Quit(); /*close SDL*/
fflush(stdout); /*update stdout*/
return 0;
}
|