00001
00002
00003 #ifndef RENDERER_H
00004 #define RENDERER_H
00005
00006
00007 #include "glew.h"
00008
00009 #include "Configuration.h"
00010 #include "CoreEngine.h"
00011 #include "Texture.h"
00012 #include "Vector2f.h"
00013 #include "Vector3f.h"
00014 #include "Vector4f.h"
00015 #include "Vector2i.h"
00016
00017
00018 namespace pge {
00019
00020 namespace renderer {
00021
00022 static GLUquadric *g_quadric;
00023
00024
00025 static float g_nearPlane = 0.1f;
00026 static float g_farPlane = 1500.0f;
00027 static float g_fovAngle = 45.0f;
00028
00029
00030
00031
00032
00033 inline static void glInit() {
00034 glewInit();
00035 g_quadric = gluNewQuadric();
00036 }
00037
00038
00039
00040
00041
00042 inline static void glShutdown() {
00043 if(g_quadric) {
00044 gluDeleteQuadric(g_quadric);
00045 }
00046 }
00047
00049
00051
00052
00053
00054 inline static GLenum getError(bool printError) {
00055 GLenum errorCode = 0;
00056
00057 errorCode = glGetError();
00058 if (printError) {
00059 switch (errorCode) {
00060 case GL_NO_ERROR:
00061 printf("There was no error.");
00062 break;
00063 case GL_INVALID_ENUM:
00064 printf("Error: enum argument out of range.");
00065 break;
00066 case GL_INVALID_VALUE:
00067 printf("Error: numeric argument out of range.");
00068 break;
00069 case GL_INVALID_OPERATION:
00070 printf("Error: operation illegal in current state.");
00071 break;
00072 case GL_STACK_OVERFLOW:
00073 printf("Error: command would cause a stack overflow.");
00074 break;
00075 case GL_STACK_UNDERFLOW:
00076 printf("Error: command would cause a stack underflow.");
00077 break;
00078 case GL_OUT_OF_MEMORY:
00079 printf("Error: not enough memory left to execute command.");
00080 break;
00081 case GL_TABLE_TOO_LARGE:
00082 printf("Error: the specified table is too large.");
00083 break;
00084 default:
00085 break;
00086 }
00087 }
00088 return errorCode;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 inline static bool isAnyError() {
00098 if (glGetError() == GL_NO_ERROR) {
00099 return false;
00100 } else {
00101 return true;
00102 }
00103 }
00104
00105
00107
00109
00110
00111
00112 inline static void enter3DMode(float fovAngle, float aspect, float near, float far) {
00113 g_nearPlane = near;
00114 g_farPlane = far;
00115 g_fovAngle = fovAngle;
00116
00117 glMatrixMode(GL_PROJECTION);
00118 glLoadIdentity();
00119 gluPerspective(fovAngle, aspect, near, far);
00120 glMatrixMode(GL_MODELVIEW);
00121 glLoadIdentity();
00122 }
00123
00124
00125
00126
00127
00128 inline static void enter3DMode() {
00129 float aspect = (float)CoreEngine::getInstance()->getResolutionWidth() / (float)CoreEngine::getInstance()->getResolutionHeight();
00130
00131
00132 glMatrixMode(GL_PROJECTION);
00133 glLoadIdentity();
00134 gluPerspective(g_fovAngle, aspect, g_nearPlane, g_farPlane);
00135 glMatrixMode(GL_MODELVIEW);
00136 glLoadIdentity();
00137 }
00138
00139
00140
00141
00142
00143 inline static void enter2DMode(float left, float right, float bottom, float top, float near, float far) {
00144 glMatrixMode(GL_PROJECTION);
00145 glLoadIdentity();
00146 glOrtho(left, right, bottom, top, near, far);
00147 glMatrixMode(GL_MODELVIEW);
00148 glLoadIdentity();
00149 }
00150
00151
00153
00155
00156
00157
00158 inline static void normal3f(Vector3f normal) {
00159 glNormal3fv(normal.m_v);
00160 }
00161
00162
00163
00164
00165
00166 inline static void vertex3f(float x, float y, float z) {
00167 glVertex3f(x, y, z);
00168 }
00169
00170
00171
00172
00173
00174 inline static void vertex3f(float x, float y, float z, float u, float v) {
00175 glMultiTexCoord2f(GL_TEXTURE0, u, v);
00176 glVertex3f(x, y, z);
00177 }
00178
00179
00180
00181
00182
00183 inline static void vertex3f(Vector3f vertex) {
00184 glVertex3fv(vertex.m_v);
00185 }
00186
00187
00188
00189
00190
00191 inline static void vertex3f(Vector3f vertex, Vector2f texCoord) {
00192 glMultiTexCoord2fv(GL_TEXTURE0, texCoord.m_v);
00193 glVertex3fv(vertex.m_v);
00194 }
00195
00196
00197
00198
00199
00200 inline void vertex3f(Vector3f vertex, float texCoordU, float texCoordV) {
00201 glMultiTexCoord2f(GL_TEXTURE0, texCoordU, texCoordV);
00202 glVertex3fv(vertex.m_v);
00203 }
00204
00205
00206
00207
00208
00209
00210 inline static void vertex3f(Vector3f vertex, Vector2f texCoord0, Vector2f texCoord1) {
00211 glMultiTexCoord2fv(GL_TEXTURE0, texCoord0.m_v);
00212 glMultiTexCoord2fv(GL_TEXTURE1, texCoord1.m_v);
00213 glVertex3fv(vertex.m_v);
00214 }
00215
00216
00217
00218
00219
00220 inline static void vertex3f(Vector3f vertex, float texCoordU0, float texCoordV0, float texCoordU1,
00221 float texCoordV1) {
00222 glMultiTexCoord2f(GL_TEXTURE0, texCoordU0, texCoordV0);
00223 glMultiTexCoord2f(GL_TEXTURE1, texCoordU1, texCoordV1);
00224 glVertex3fv(vertex.m_v);
00225 }
00226
00227
00228
00229
00230
00231 inline static void vertex3f(Vector3f vertex, float texCoord[]) {
00232 glMultiTexCoord2fv(GL_TEXTURE0, texCoord);
00233 glVertex3fv(vertex.m_v);
00234 }
00235
00236
00237
00238
00239
00240 inline static void vertex3f(Vector3f vertex, Vector2f texCoord, int textureUnit) {
00241 glMultiTexCoord2fv(textureUnit, texCoord.m_v);
00242 glVertex3fv(vertex.m_v);
00243 }
00244
00245
00246
00247
00248
00249 inline static void vertex3f(Vector3f v, float texCoordU, float texCoordV, int textureUnit) {
00250 glMultiTexCoord2f(textureUnit, texCoordU, texCoordV);
00251 glVertex3fv(v.m_v);
00252 }
00253
00254
00255
00256
00257
00258 inline static void vertex2i(int x, int y) {
00259 glVertex2i(x, y);
00260 }
00261
00262
00263
00264
00265
00266 inline static void vertex2i(Vector2i v) {
00267 glVertex2iv(v.m_v);
00268 }
00269
00270
00271
00272
00273
00274 inline static void vertex2i(Vector2i vertex, float u, float v) {
00275 glMultiTexCoord2f(GL_TEXTURE0, u, v);
00276 glVertex2iv(vertex.m_v);
00277 }
00278
00279
00280
00281
00282
00283 inline static void vertex2i(Vector2i vertex, float texCoord[]) {
00284 glMultiTexCoord2fv(GL_TEXTURE0, texCoord);
00285 glVertex2iv(vertex.m_v);
00286 }
00287
00288
00289
00290
00291
00292 inline static void renderLine(Vector3f start, Vector3f end) {
00293 glBegin(GL_LINES);
00294 glVertex3fv(start.m_v);
00295 glVertex3fv(end.m_v);
00296 glEnd();
00297 }
00298
00299
00300
00301
00302
00303 inline static void renderLine(float startX, float startY, float startZ, float endX, float endY, float endZ) {
00304 glBegin(GL_LINES);
00305 glVertex3f(startX, startY, startZ);
00306 glVertex3f(endX, endY, endZ);
00307 glEnd();
00308 }
00309
00310
00311
00312
00313
00314 inline static void renderQuad(Vector3f q0, Vector3f q1, Vector3f q2, Vector3f q3) {
00315 glBegin(GL_QUADS);
00316 glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
00317 glVertex3fv(q0.m_v);
00318
00319 glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
00320 glVertex3fv(q1.m_v);
00321
00322 glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
00323 glVertex3fv(q2.m_v);
00324
00325 glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
00326 glVertex3fv(q3.m_v);
00327 glEnd();
00328 }
00329
00330
00331
00332
00333 inline static void setClearColor(Vector3f color) {
00334 glClearColor(color.m_v[0], color.m_v[1], color.m_v[2], 1.0f);
00335 }
00336
00337
00338
00339
00340
00341 inline static void color3f(float r, float g, float b) {
00342 glColor3f(r, g, b);
00343 }
00344
00345
00346
00347
00348
00349 inline static void color3f(Vector3f color) {
00350 glColor3fv(color.m_v);
00351 }
00352
00353
00354
00355
00356
00357 inline static void color4f(Vector4f color) {
00358 glColor4fv(color.m_v);
00359 }
00360
00361
00362
00363
00364
00365 inline static void color4f(float r, float g, float b, float a) {
00366 glColor4f(r, g, b, a);
00367 }
00368
00369
00371
00373
00374
00375
00376 inline static void translate(Vector3f t) {
00377 glTranslatef(t.m_v[0], t.m_v[1], t.m_v[2]);
00378 }
00379
00380
00381
00382
00383
00384 inline static void translate(float tx, float ty, float tz) {
00385 glTranslatef(tx, ty, tz);
00386 }
00387
00388
00389
00390
00391
00392 inline static void rotate(float angle, Vector3f axis) {
00393 glRotatef(angle, axis.m_v[0], axis.m_v[1], axis.m_v[2]);
00394 }
00395
00396
00397
00398
00399
00400 inline static void rotate(float angle, float x, float y, float z) {
00401 glRotatef(angle, x, y, z);
00402 }
00403
00404
00405
00406
00407
00408 inline static void pushMatrix() {
00409 glPushMatrix();
00410 }
00411
00412
00413
00414
00415
00416 inline static void popMatrix() {
00417 glPopMatrix();
00418 }
00419
00421
00423
00424
00425
00426 inline static void setDepthTestEnabled(bool enabled) {
00427 if (enabled) {
00428 glEnable(GL_DEPTH_TEST);
00429 } else {
00430 glDisable(GL_DEPTH_TEST);
00431 }
00432 }
00433
00434
00435
00436
00437
00438 inline static void setBlendingEnabled(bool enabled) {
00439 if (enabled) {
00440 glEnable(GL_BLEND);
00441 } else {
00442 glDisable(GL_BLEND);
00443 }
00444 }
00445
00446
00447
00448
00449
00450 inline static void setBlendFunc(int src, int dest) {
00451 glBlendFunc(src, dest);
00452 }
00453
00454
00455
00456
00457
00458 inline static void setAlphaTestEnabled(bool enabled) {
00459 if (enabled) {
00460 glEnable(GL_ALPHA_TEST);
00461 } else {
00462 glDisable(GL_ALPHA_TEST);
00463 }
00464 }
00465
00466
00467
00468
00469
00470 inline static void setAlphaFunc(unsigned int func, float ref) {
00471 glAlphaFunc(func, ref);
00472 }
00473
00474
00475
00476
00477
00478 inline static void setTextureAlphaBlendEnabled(bool enabled) {
00479 if (enabled) {
00480 glEnable(GL_BLEND);
00481 glEnable(GL_ALPHA_TEST);
00482 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00483 glAlphaFunc(GL_GREATER, 0);
00484 } else {
00485 glDisable(GL_BLEND);
00486 glDisable(GL_ALPHA_TEST);
00487 }
00488 }
00489
00490
00492
00494
00495
00496
00497 inline static void setTexture(Texture *texture, GLint envMode) {
00498 glEnable(GL_TEXTURE_2D);
00499 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode);
00500 glBindTexture(GL_TEXTURE_2D, texture->getDescriptor());
00501 }
00502
00503
00504
00505
00506
00507 inline static void setTexture(Texture *texture) {
00508 glEnable(GL_TEXTURE_2D);
00509 glBindTexture(GL_TEXTURE_2D, texture->getDescriptor());
00510 }
00511
00512
00513
00514
00515
00516 inline static void setTextureUnitEnabled(unsigned unit, bool enabled) {
00517 glActiveTexture(unit);
00518 if(enabled) {
00519 glEnable(GL_TEXTURE_2D);
00520 } else {
00521 glDisable(GL_TEXTURE_2D);
00522 }
00523 }
00524
00525
00527
00529
00530
00531
00532 inline static unsigned int createDisplayList(void) {
00533 GLuint displayListID = 0;
00534
00535
00536 displayListID = glGenLists(1);
00537
00538
00539 glNewList(displayListID, GL_COMPILE);
00540 return displayListID;
00541 }
00542
00543
00544
00545
00546
00547 inline static void endDisplayList(void) {
00548 glEndList();
00549 }
00550
00551
00552
00553
00554
00555 inline static void executeDisplayList(unsigned int displayListId) {
00556 glCallList(displayListId);
00557 }
00558
00559
00560
00561
00562
00563 inline static void deleteDisplayList(unsigned int displayListId) {
00564 glDeleteLists(displayListId, 1);
00565 }
00566
00567
00569
00571
00572
00573
00574 inline static void wireSphere(double radius, int slices, int stacks) {
00575 gluQuadricTexture(g_quadric, false);
00576 gluSphere(g_quadric, radius, slices, stacks);
00577 }
00578
00579
00580
00581
00582
00583 inline static void wireSphere(Vector3f position, double radius, int slices, int stacks) {
00584 glPushMatrix();
00585 glTranslatef(position.m_v[0], position.m_v[1], position.m_v[2]);
00586 gluQuadricTexture(g_quadric, false);
00587 gluSphere(g_quadric, radius, slices, stacks);
00588 glPopMatrix();
00589 }
00590
00591
00592
00593
00594
00595 inline static void wireSphere(Vector4f position, double radius, int slices, int stacks) {
00596 glPushMatrix();
00597 glTranslatef(position.m_v[0], position.m_v[1], position.m_v[2]);
00598 gluQuadricTexture(g_quadric, false);
00599 gluSphere(g_quadric, radius, slices, stacks);
00600 glPopMatrix();
00601 }
00602
00603
00604
00605
00606
00607 inline static void texturedSphere(double radius, int slices, int stacks) {
00608
00609 gluQuadricTexture(g_quadric, true);
00610 gluSphere(g_quadric, radius, slices, stacks);
00611 }
00612 };
00613 };
00614
00615 #endif