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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "SDL.lib")
#pragma
#ifdef _WIN32
#include <windows.h>
#endif
#include <iostream>
#include <vector>
#include <string>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
using std::vector;
void error(const std::string& s) {
#ifdef _WIN32
MessageBox(NULL, s.c_str(), "An error occurred", MB_ICONINFORMATION | MB_OK);
#else
std::cerr << s << std::endl;
#endif
}
class SimpleApp
{
public:
static const int WINDOW_WIDTH = 1024;
static const int WINDOW_HEIGHT = 768;
SimpleApp();
bool initialize();
void resize(int x, int y);
void prepare();
void render();
};
SimpleApp::SimpleApp()
{
}
bool SimpleApp::initialize()
{
//Enable depth testing
glEnable(GL_DEPTH_TEST);
//Set up the projection matrix
resize(WINDOW_WIDTH, WINDOW_HEIGHT);
return true;
}
void SimpleApp::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 1.0, 6.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
//Send the vertices and colors for the triangle
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(2.0, 2.5, -1.0);
glColor4f(0.0, 1.0, 0.0, 1.0);
glVertex3f(-3.5, -2.5, -1.0);
glColor4f(0.0, 0.0, 1.0, 1.0);
glVertex3f(2.0, -4.0, -1.0);
glEnd();
glBegin(GL_TRIANGLE_FAN);
//Send the vertices and colors for the pentagon
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex3f(-1.0, 2.0, 0.0);
glColor4f(1.0, 1.0, 0.0, 1.0);
glVertex3f(-3.0, -0.5, 0.0);
glColor4f(0.0, 1.0, 1.0, 1.0);
glVertex3f(-1.5, -3.0, 0.0);
glColor4f(0.0, 0.0, 0.0, 1.0);
glVertex3f(1.0, -2.0, 0.0);
glColor4f(1.0, 0.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glEnd();
}
void SimpleApp::resize(int w, int h)
{
//Prevent a divide by zero error
if (h <= 0)
{
h = 1;
}
//When we resize the window, we tell OpenGL about the new viewport size
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION); //deprecated
glLoadIdentity();
//Then we set up our projection matrix with the correct aspect ratio
gluPerspective(60.0f, float(w) / float(h), 1.0f, 100.0f); //deprecated
glMatrixMode(GL_MODELVIEW); //deprecated
glLoadIdentity(); //deprecated
}
int main(int argc, char** argv)
{
SimpleApp myApp;
if ( SDL_Init( SDL_INIT_VIDEO ) != 0 )
{
std::cerr << "Could not initialize SDL" << std::endl;
return false;
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
std::string title = "BOGLGP Chapter 1";
SDL_WM_SetCaption(title.c_str(), title.c_str());
// Create the window
if (!SDL_SetVideoMode(SimpleApp::WINDOW_WIDTH, SimpleApp::WINDOW_HEIGHT, 0, SDL_OPENGL | SDL_RESIZABLE))
{
std::cerr << "Could not create the window" << std::endl;
return false;
}
if (!myApp.initialize())
{
SDL_Quit();
return 1;
}
bool done = false;
while (!done)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch(event.type) {
case SDL_QUIT:
done = true;
break;
case SDL_VIDEORESIZE:
myApp.resize(event.resize.w, event.resize.h);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
done = true;
}
break;
default:
break;
}
}
myApp.render();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
|