All pastes #488993 Raw Edit

3d cruft

public cpp v1 · immutable
·published 2007-05-15 09:04 UTC
rendered paste body
// 3D graphics engine framework// by Ty Guenley#include "SDL/SDL.h"#include "SDL/SDL_opengl.h"#include <stdio.h>#include <cmath>//May need to be changed to whatever the math library is called on your machineconst double PI = 3.1415926535;const double mouseSensitivity = .1;struct coord3 {	double x;	double y;	double z;}coord3 camPosition;camPosition.x = 0;camPosition.y = 0;camPosition.z = 10;int mousex, mousey;double rotationAngle = 0, tiltAngle = 0;//Timing variablesdouble lastTime, elapsedTime = .01;int main(int argc, char *argv[]) {        SDL_Surface *screen;    float Rotate=0.0f; // A rotation value to be used to spin our polygon    glClearColor(0,0,0,0);        if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {        printf("Unable to initialize SDL: %s\n", SDL_GetError());        return 1;    }    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*    screen = SDL_SetVideoMode( 800, 600, 16, SDL_OPENGL ); // *changed*    if ( !screen ) {		printf("Unable to set video mode: %s\n", SDL_GetError());		return 1;	}	        // Setup our screen    //glViewport(0,0,800,600);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(45.0f,800.0f/600.0f,1.0f,500.0f);    glMatrixMode(GL_MODELVIEW);        // Ensure correct display of polygons    glEnable(GL_CULL_FACE);    glEnable(GL_DEPTH_TEST);    glDepthMask(GL_TRUE);    SDL_WarpMouse(400, 300);    lastTime = SDL_GetTicks();    int done = 0;        // SDL_ShowCursor(SDL_DISABLE); // disable cursor    // This loop will run until Esc is pressed    while(done == 0) {        SDL_Event event;                while ( SDL_PollEvent(&event) ) {            if ( event.type == SDL_QUIT ) {  done = 1;  }                        // key events            if ( event.type == SDL_KEYDOWN ) {                if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } // quit on escape            }        }	//Get mouse coordinates	SDL_GetMouseState(mousex, mousey);	//Adjust our camera view based on how the mouse moved	rotationAngle += (mousex - 400) * mouseSensitivity * elapsedTime;	tiltAngle -= (mousey - 400) * mouseSensitivity * elapsedTime;	//Limit camera tilt	if (tiltAngle > PI/2) tiltAngle = PI/2;	if (tiltAngle < -PI/2) tiltAngle = -PI/2;                SDL_WarpMouse(400,300); // set mouse to center of screen                Uint8 *keystate = SDL_GetKeyState(NULL);        if ( keystate[SDLK_s] ) {		camPosition.x += cos(rotationAngle) * 4 * elapsedTime;		camPosition.z += sin(rotationAngle) * 4 * elapsedTime;	}        if ( keystate[SDLK_w] ) {		camPosition.x -= cos(rotationAngle) * 4 * elapsedTime;		camPosition.z -= sin(rotationAngle) * 4 * elapsedTime;	}                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and z-buffer        glLoadIdentity(); // Reset current matrix (Modelview)                // set camera angle        gluLookAt (camPosition.x, camPosition.y, camPosition.z, camPosition.x + cos(rotationAngle), camPosition.y + sin(tiltAngle), camPosition.z + sin(rotationAngle), 0, 1, 0);         // Do our rotations        glTranslatef(0.0f,0.0f,-5.0f);        glRotatef(Rotate,0.0f,0.0f,1.0f);        glRotatef(Rotate,1.0f,0.6f,0.0f);            // Draw the top and bottom faces        glBegin(GL_QUADS);            glColor3f(1.0f,0.0f,0.0f); // Red            glVertex3f(-1.0f, 1.0f, 1.0f);            glVertex3f( 1.0f, 1.0f, 1.0f);            glVertex3f( 1.0f, 1.0f,-1.0f);            glVertex3f(-1.0f, 1.0f,-1.0f);                    glColor3f(0.0f,0.0f,1.0f); // Blue            glVertex3f(-1.0f,-1.0f, 1.0f);            glVertex3f(-1.0f,-1.0f,-1.0f);            glVertex3f( 1.0f,-1.0f,-1.0f);            glVertex3f( 1.0f,-1.0f, 1.0f);        glEnd();            // Draw the side faces        glBegin(GL_TRIANGLE_STRIP);            glColor3f(1.0f,1.0f,1.0f); // White            glVertex3f(-1.0f, 1.0f, 1.0f);            glVertex3f(-1.0f,-1.0f, 1.0f);            glVertex3f( 1.0f, 1.0f, 1.0f);            glColor3f(0.0f,1.0f,0.0f); // Green            glVertex3f( 1.0f,-1.0f, 1.0f);            glColor3f(1.0f,1.0f,0.0f); // Yellow            glVertex3f( 1.0f, 1.0f,-1.0f);            glColor3f(0.0f,1.0f,1.0f); // Aqua?            glVertex3f( 1.0f,-1.0f,-1.0f);            glColor3f(0.6f,0.6f,0.6f); // Gray            glVertex3f(-1.0f, 1.0f,-1.0f);            glColor3f(0.1f,0.1f,0.1f); // Dark Gray            glVertex3f(-1.0f,-1.0f,-1.0f);            glColor3f(0.0f,0.0f,1.0f); // Blue            glVertex3f(-1.0f, 1.0f, 1.0f);            glColor3f(1.0f,0.0f,1.0f); // Red            glVertex3f(-1.0f,-1.0f, 1.0f);        glEnd();            // Add to the rotation for next frame         Rotate+=0.05f;            // Show our cube        SDL_GL_SwapBuffers();	//Calculate elapsed time since last frame completed	elapsedTime = SDL_GetTicks() - lastTime;	lastTime = SDL_GetTicks();	    }}