00001 #include "glcontext.hh"
00002 #include <GL/gl.h>
00003 #include <GL/glu.h>
00004 #include <iostream>
00005
00006 namespace anoid {
00007 namespace context {
00008
00009 using namespace std;
00010 using namespace simple;
00011
00012 void GLContext::quit(int code) {
00013 cout << (frames * 1000.0 / (SDL_GetTicks() - then)) << " fps" << endl;
00014 SDL_Quit();
00015 exit(code);
00016 }
00017
00018 void GLContext::init(int width, int height, bool window) {
00019 const SDL_VideoInfo *info = NULL;
00020 int bpp = 0;
00021 int flags = 0;
00022
00023 info = SDL_GetVideoInfo();
00024
00025 if (!info) {
00026 cerr << "Video query failed: " << SDL_GetError() << endl;
00027 quit(1);
00028 }
00029
00030 bpp = info->vfmt->BitsPerPixel;
00031
00032 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
00033 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
00034 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
00035 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
00036 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00037
00038 if (window)
00039 flags = SDL_SWSURFACE | SDL_OPENGL;
00040 else
00041 flags = SDL_SWSURFACE | SDL_OPENGL | SDL_FULLSCREEN;
00042
00043 if (SDL_SetVideoMode(width, height, bpp, flags) == 0) {
00044 cerr << "Video mode set failed: " << SDL_GetError() << endl;
00045 quit(1);
00046 }
00047
00048 SDL_ShowCursor(SDL_DISABLE);
00049 SDL_ShowCursor(SDL_ENABLE);
00050
00051
00052 float ratio = (float) width / (float) height;
00053 GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 0.5f };
00054 GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
00055 GLfloat LightSpecular[]= { 1.0f, 1.0f, 1.0f, 1.0f };
00056 GLfloat LightPosition[]= { 0.0f, -1.0f, 2.0f, 1.0f };
00057
00058 glShadeModel(GL_SMOOTH);
00059
00060 glCullFace(GL_BACK);
00061 glFrontFace(GL_CCW);
00062 glEnable(GL_CULL_FACE);
00063
00064 glDepthFunc(GL_LEQUAL);
00065 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00066 glEnable(GL_DEPTH_TEST);
00067
00068 glClearColor(0, 0, 0, 1.0);
00069 glClearDepth(1.0f);
00070
00071 glViewport(0, 0, width, height);
00072
00073 glMatrixMode(GL_PROJECTION);
00074 glLoadIdentity();
00075
00076 gluPerspective(60.0, ratio, 1.0, 1024.0);
00077 glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
00078 glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
00079 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
00080 glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);
00081 glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
00082 glEnable(GL_LIGHT1);
00083 glEnable(GL_LIGHTING);
00084
00085
00086 then = SDL_GetTicks();
00087 }
00088
00089 void GLContext::initWindow(int width, int height) {
00090 init(width, height, true);
00091 }
00092
00093 void GLContext::initFullscreen(int width, int height) {
00094 init(width, height, false);
00095 }
00096
00097 void GLContext::setView() {
00098 }
00099
00100 void GLContext::loadIdentity() {
00101 glLoadIdentity();
00102 }
00103
00104 void GLContext::pushMatrix() {
00105 glPushMatrix();
00106 }
00107
00108 void GLContext::popMatrix() {
00109 glPopMatrix();
00110 }
00111
00112 void GLContext::rotate(Matrix &rotation) {
00113 }
00114
00115 void GLContext::scale(Vector &scale) {
00116 glScaled(scale.getX(), scale.getY(), scale.getZ());
00117 }
00118
00119 void GLContext::translate(Vector &translation) {
00120 glTranslated(translation.getX(), translation.getY(), translation.getZ());
00121 }
00122
00123 int GLContext::beginFigure() {
00124 GLint newobject = glGenLists(1);
00125 glNewList(newobject, GL_COMPILE);
00126 return newobject;
00127 }
00128
00129 void GLContext::endFigure() {
00130 endDrawing();
00131 glEndList();
00132 }
00133
00134 void GLContext::removeFigure(int id) {
00135 }
00136
00137 void GLContext::endDrawing() {
00138 if (drawingTriangles || drawingQuads)
00139 glEnd();
00140 drawingTriangles = drawingQuads = false;
00141 }
00142 void GLContext::drawTriangle(Vector &A, Vector &B, Vector &C, Vector &normal, int a) {
00143 if (!drawingTriangles) {
00144 endDrawing();
00145 glBegin(GL_TRIANGLES);
00146 drawingTriangles = true;
00147 }
00148
00149 glNormal3d(normal.getX(), normal.getY(), normal.getZ());
00150 glVertex3d(A.getX(), A.getY(), A.getZ());
00151 glVertex3d(B.getX(), B.getY(), B.getZ());
00152 glVertex3d(C.getX(), C.getY(), C.getZ());
00153 }
00154
00155 void GLContext::drawQuad(Vector &A, Vector &B, Vector &C, Vector &D, Vector &normal, int a) {
00156 if (!drawingQuads) {
00157 endDrawing();
00158 glBegin(GL_QUADS);
00159 drawingQuads = true;
00160 }
00161
00162 glNormal3d(normal.getX(), normal.getY(), normal.getZ());
00163 glVertex3d(A.getX(), A.getY(), A.getZ());
00164 glVertex3d(B.getX(), B.getY(), B.getZ());
00165 glVertex3d(C.getX(), C.getY(), C.getZ());
00166 glVertex3d(D.getX(), D.getY(), D.getZ());
00167 }
00168
00169 void GLContext::drawFigure(int id) {
00170 glCallList(id);
00171 }
00172
00173 Matrix GLContext::getMatrix() {
00174 }
00175
00176 void GLContext::setMatrix(Matrix &m) {
00177 }
00178
00179 void GLContext::swapBuffers() {
00180 frames++;
00181 SDL_GL_SwapBuffers();
00182 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00183 glMatrixMode(GL_MODELVIEW);
00184 glLoadIdentity();
00185 glTranslatef(0.0,0.0,-5.0);
00186 glScalef(0.001,0.001,0.001);
00187 }
00188
00189 GLContext::~GLContext() {
00190 quit(0);
00191 }
00192
00193 };
00194 };