1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <SDL.h>
#include <SDL_opengl.h>
#include <gl/GL.h>
#include <gl/GLU.h>
Uint32 start;
int Triangle;
bool Running;
char* Title;
SDL_Surface* Screen = NULL;
SDL_Event event;
float angle;
int Init();
void Render();
void DrawCube(float size);
int main(int argc, char* argv[])
{
if(Init() == 1) {std::cout << "Failure to set up SDL &/or OpenGL.\n"; system("PAUSE"); SDL_Quit(); return 1;}
while(Running)
{
start = SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
Running = false;
break;
}
}
Render();
angle += 0.5;
if(angle < 360) {angle -= 360;}
if(1000/30 > SDL_GetTicks() - start)
{
SDL_Delay(1000/30 - (SDL_GetTicks() - start));
}
}
SDL_Quit();
return 0;
}
int Init()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("OpenGL Tut", "OpenGL Tutorial");
Screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_OPENGL);
if(Screen != NULL) {Running = true;}
else {return 1;}
start = SDL_GetTicks();
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640.0/480.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
return 0;
}
void Render()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -0.5);
glRotatef(angle, 1.0, 1.0, 1.0);
DrawCube(1.0);
/*glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, -5.0); glColor3f(0.0, 1.0, 0.0);
glVertex3f(-2.0, -2.0, -5.0); glColor3f(0.0, 0.0, 1.0);
glVertex3f(2.0, -2.0, -5.0);
glEnd();*/
}
void DrawCube(float size)
{
glBegin(GL_QUADS);
// front face
glColor3f(1.0,0.0,0.0);
glVertex3f(size/2,size/2,size/2);
glVertex3f(-size/2,size/2,size/2);
glVertex3f(-size/2,-size/2,size/2);
glVertex3f(size/2,-size/2,size/2);
// left face
glColor3f(0.0,1.0,0.0);
glVertex3f(-size/2,size/2,size/2);
glVertex3f(-size/2,-size/2,size/2);
glVertex3f(-size/2,-size/2,-size/2);
glVertex3f(-size/2,size/2,-size/2);
// back face
glColor3f(0.0,0.0,1.0);
glVertex3f(size/2,size/2,-size/2);
glVertex3f(-size/2,size/2,-size/2);
glVertex3f(-size/2,-size/2,-size/2);
glVertex3f(size/2,-size/2,-size/2);
// right face
glColor3f(1.0,1.0,0.0);
glVertex3f(size/2,size/2,size/2);
glVertex3f(size/2,-size/2,size/2);
glVertex3f(size/2,-size/2,-size/2);
glVertex3f(size/2,size/2,-size/2);
// top face
glColor3f(1.0,0.0,1.0);
glVertex3f(size/2,size/2,size/2);
glVertex3f(-size/2,size/2,size/2);
glVertex3f(-size/2,size/2,-size/2);
glVertex3f(size/2,size/2,-size/2);
// bottom face
glColor3f(0.0,1.0,1.0);
glVertex3f(size/2,-size/2,size/2);
glVertex3f(-size/2,-size/2,size/2);
glVertex3f(-size/2,-size/2,-size/2);
glVertex3f(size/2,-size/2,-size/2);
glEnd();
}
|