• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/render/src/Render.cpp

Go to the documentation of this file.
00001 #include "../pch.h"
00002 
00003 typedef void (__stdcall * PFNGLDRAWELEMENTSBASEVERTEXPROC) (GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex);
00004 PFNGLDRAWELEMENTSBASEVERTEXPROC glDrawElementsBaseVertex;
00005 
00006 Render::Render(void)
00007 {
00008         int i;
00009         m_dRenderCount = 0;
00010         m_dRenderColorCount = 0;
00011         m_numGuiObjects = 0;
00012         m_guiShown = false;
00013         m_enableBlending = true;
00014         m_enableCulling = true;
00015         m_enableWireframe = false;
00016         m_skyBox = 0;
00017         m_drawList = 0;
00018         m_drawColorList = 0;
00019         m_guiDrawList = 0;
00020 
00021         parallax.lightColorArray[0] = float4(1.f, .5f, .2f, 0.0001f);
00022         parallax.lightColorArray[1] = float4(.3f, 1.f, .3f, 0.0001f);
00023         parallax.lightColorArray[2] = float4(.2f, .5f, 1.f, 0.0001f);
00024         parallax.lightColorArray[3] = float4(.4f, .4f, .4f, 0.0001f);
00025         parallax.lightPositionArray[0] = float4( 20.f,-20.f,  20.f, 1.f);
00026         parallax.lightPositionArray[1] = float4(-50.f,  0.f,   0.f, 1.f);
00027         parallax.lightPositionArray[2] = float4(  0.f,  0.f,  50.f, 1.f);
00028         parallax.lightPositionArray[3] = float4(  0.f, 50.f,   0.f, 1.f);
00029 
00030         memset(defaultRectVertices, 0, sizeof(Vertex)*4);
00031         i = 0;
00032         defaultRectVertices[i++].position = float4(-1.f, 1.f, 0.0f, 1.f);
00033         defaultRectVertices[i++].position = float4(-1.f,-1.f, 0.0f, 1.f);
00034         defaultRectVertices[i++].position = float4( 1.f, 1.f, 0.0f, 1.f);
00035         defaultRectVertices[i++].position = float4( 1.f,-1.f, 0.0f, 1.f);
00036         i = 0;
00037         defaultRectVertices[i++].uv = float4( 0.f, 1.f, 0.f, 0.f);
00038         defaultRectVertices[i++].uv = float4( 0.f, 0.f, 0.f, 0.f);
00039         defaultRectVertices[i++].uv = float4( 1.f, 1.f, 0.f, 0.f);
00040         defaultRectVertices[i++].uv = float4( 1.f, 0.f, 0.f, 0.f);
00041 
00042         i = 0;
00043         defaultRectIndices[i++] = 0;
00044         defaultRectIndices[i++] = 1;
00045         defaultRectIndices[i++] = 2;
00046         defaultRectIndices[i++] = 2;
00047         defaultRectIndices[i++] = 1;
00048         defaultRectIndices[i++] = 3;
00049 
00050         memset(defaultCubeVertices, 0, sizeof(Vertex)*4);
00051         i=0;
00052         float d = 400.f;
00053         defaultCubeVertices[i++].position = float4( d, d, d, 1.f);
00054         defaultCubeVertices[i++].position = float4( d, d,-d, 1.f);
00055         defaultCubeVertices[i++].position = float4(-d, d, d, 1.f);
00056         defaultCubeVertices[i++].position = float4(-d, d,-d, 1.f);
00057         defaultCubeVertices[i++].position = float4( d,-d, d, 1.f);
00058         defaultCubeVertices[i++].position = float4( d,-d,-d, 1.f);
00059         defaultCubeVertices[i++].position = float4(-d,-d, d, 1.f);
00060         defaultCubeVertices[i++].position = float4(-d,-d,-d, 1.f);
00061 
00062         i=0;
00063         // upper
00064         defaultCubeIndices[i++] = 0; defaultCubeIndices[i++] = 2; defaultCubeIndices[i++] = 1;
00065         defaultCubeIndices[i++] = 2; defaultCubeIndices[i++] = 3; defaultCubeIndices[i++] = 1;
00066         // lower
00067         defaultCubeIndices[i++] = 4; defaultCubeIndices[i++] = 5; defaultCubeIndices[i++] = 6;
00068         defaultCubeIndices[i++] = 5; defaultCubeIndices[i++] = 7; defaultCubeIndices[i++] = 6;
00069         // front
00070         defaultCubeIndices[i++] = 4; defaultCubeIndices[i++] = 6; defaultCubeIndices[i++] = 0;
00071         defaultCubeIndices[i++] = 6; defaultCubeIndices[i++] = 2; defaultCubeIndices[i++] = 0;
00072         // back
00073         defaultCubeIndices[i++] = 5; defaultCubeIndices[i++] = 1; defaultCubeIndices[i++] = 7;
00074         defaultCubeIndices[i++] = 1; defaultCubeIndices[i++] = 3; defaultCubeIndices[i++] = 7;
00075         // left
00076         defaultCubeIndices[i++] = 6; defaultCubeIndices[i++] = 7; defaultCubeIndices[i++] = 2;
00077         defaultCubeIndices[i++] = 7; defaultCubeIndices[i++] = 3; defaultCubeIndices[i++] = 2;
00078         // right
00079         defaultCubeIndices[i++] = 5; defaultCubeIndices[i++] = 4; defaultCubeIndices[i++] = 1;
00080         defaultCubeIndices[i++] = 4; defaultCubeIndices[i++] = 0; defaultCubeIndices[i++] = 1;
00081 
00082         m_keepGeometryVertices = 4+8;
00083         m_keepGeometryIndices = 6+36;
00084 
00085         m_vertexCount = 4+8;
00086         m_indexCount = 6+36;
00087 }
00088 
00089 Render::~Render(void)
00090 {
00091         uninit();
00092 }
00093 
00094 bool Render::init(HWND hwnd, int width, int height, RenderDesc& desc)
00095 {
00096         m_hwnd = hwnd;
00097         m_hdc = GetDC(hwnd);
00098         
00099         PIXELFORMATDESCRIPTOR pfd;
00100         ZeroMemory(&pfd, sizeof(pfd));
00101         pfd.nSize = sizeof(pfd);
00102         pfd.nVersion = 1;
00103         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
00104         pfd.iPixelType = PFD_TYPE_RGBA;
00105         pfd.cColorBits = 32;
00106         pfd.cDepthBits = 16;
00107         pfd.cRedBits = 8;
00108         pfd.cGreenBits = 8;
00109         pfd.cBlueBits = 8;
00110         pfd.cAlphaBits = 8;
00111         pfd.iLayerType = PFD_MAIN_PLANE;
00112         int format = ChoosePixelFormat(m_hdc, &pfd);
00113         SetPixelFormat(m_hdc, format, &pfd);
00114 
00115         int attribs[] = {
00116                 WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
00117                 WGL_CONTEXT_MINOR_VERSION_ARB, 2,
00118                 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
00119                 0
00120         };
00121         
00122         HGLRC tempContext = wglCreateContext(m_hdc);
00123         wglMakeCurrent(m_hdc, tempContext);
00124 
00125         CHECK_GL_ERROR
00126 
00127         initGLEW();
00128 
00129         if(wglewIsSupported("WGL_ARB_create_context") == 1) {
00130                 m_RC = wglCreateContextAttribsARB(m_hdc, 0, attribs);
00131                 wglMakeCurrent(NULL, NULL);
00132                 wglDeleteContext(tempContext);
00133                 wglMakeCurrent(m_hdc, m_RC);    
00134         } else {
00135                 printf("Not possible to create a OpenGL 3.x context");
00136                 return false;
00137         }
00138 
00139         CHECK_GL_ERROR
00140 
00141         glDrawElementsBaseVertex = (PFNGLDRAWELEMENTSBASEVERTEXPROC)wglGetProcAddress("glDrawElementsBaseVertex");
00142         if(glDrawElementsBaseVertex == 0){
00143                 printf("%s","glDrawElementsBaseVertex not available.");
00144                 return false;
00145         }
00146 
00147         CHECK_GL_ERROR
00148         
00149         glEnable(GL_DEPTH_TEST);
00150         glEnable(GL_CULL_FACE);
00151         glDepthFunc(GL_LEQUAL);
00152         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00153         glClearColor(0.125f, 0.0f, 0.7f, 0.0f);
00154         glViewport(0,0,width,height);
00155 
00156         CHECK_GL_ERROR
00157 
00158         // reserve all neccessary
00159         // memory on the device
00160         reserveDeviceMemory(width, height, desc);
00161 
00162         if(!gui.shader.init(TEXT("../bin/shaders/uiShader.vert"), TEXT("../bin/shaders/uiShader.frag"))) {
00163                 printf("Gui Shader fuckup!");
00164                 return false;
00165         }
00166 
00167         gui.shader.use();
00168 
00169         gui.gui = gui.shader.getUniform("guiMat");
00170         gui.guiTexIdx = gui.shader.getUniform("guiTexIdx");
00171         gui.guiColor = gui.shader.getUniform("guiColorArray");
00172         gui.guiUVIndex = gui.shader.getUniform("guiUVIndex");
00173 
00174         gui.guiColor.set1i(0);
00175         float4x4 mat = getGuiMatrix(width, height);
00176         gui.gui.set4x4fv(&mat,1);
00177         
00178         CHECK_GL_ERROR
00179 
00180         if(!depthOnly.shader.init(TEXT("../bin/shaders/depthOnly.vert"), TEXT("../bin/shaders/depthOnly.frag"))) {
00181                 printf("Depth only shader could not be initialized!");
00182                 return false;
00183         }
00184 
00185         depthOnly.shader.use();
00186         depthOnly.modelViewMat = depthOnly.shader.getUniform("modelViewMat");
00187         depthOnly.projMat = depthOnly.shader.getUniform("projMat");
00188         
00189         CHECK_GL_ERROR
00190 
00191         if(!blur.shader.init(TEXT("../bin/shaders/blur.vert"), TEXT("../bin/shaders/blur.frag"))) {
00192                 printf("Blur shader could not be initialized!");
00193                 return false;
00194         }
00195 
00196         blur.shader.use();
00197         //blur.radius = blur.shader.getUniform("radius");
00198         blur.texture = blur.shader.getUniform("color");
00199         // set the texture level of the texture arrays
00200         //blur.radius.set1f(0.0198f);
00201         
00202         CHECK_GL_ERROR
00203 
00204         if(!depthOfField.shader.init(TEXT("../bin/shaders/blur.vert"), TEXT("../bin/shaders/depthOfField.frag"))) {
00205                 printf("Depth of field shader could not be initialized!");
00206                 return false;
00207         }
00208 
00209         depthOfField.shader.use();
00210         //depthOfField.dof = depthOfField.shader.getUniform("dof");
00211         depthOfField.sharp = depthOfField.shader.getUniform("sharp");
00212         depthOfField.blured = depthOfField.shader.getUniform("blured");
00213         // set the texture level of the texture arrays
00214         //depthOfField.dof.set2f(3.4f, 0.8f);
00215         depthOfField.sharp.set1i(2);
00216         depthOfField.blured.set1i(3);
00217         
00218         CHECK_GL_ERROR
00219 
00220         if(!skybox.shader.init(TEXT("../bin/shaders/skybox.vert"), TEXT("../bin/shaders/skybox.frag"))) {
00221                 printf("Skybox shader could not be initialized!");
00222                 return false;
00223         }
00224 
00225         skybox.shader.use();
00226         skybox.modelViewMat = skybox.shader.getUniform("modelViewMat");
00227         skybox.projMat = skybox.shader.getUniform("projMat");
00228         skybox.camPosition = skybox.shader.getUniform("camPosition");
00229         skybox.skybox = skybox.shader.getUniform("skybox");
00230         // set the texture level of the texture arrays
00231         skybox.skybox.set1i(4);
00232         
00233         CHECK_GL_ERROR
00234 
00235         if(!parallax.shader.init(TEXT("../bin/shaders/parallax.vert"), TEXT("../bin/shaders/parallax.frag"))) {
00236                 printf("Parallax shader could not be initialized!");
00237                 return false;
00238         }
00239         
00240         parallax.shader.use();
00241         // get uniforms from shader
00242         parallax.viewMat = parallax.shader.getUniform("viewMat");
00243         parallax.modelViewMat = parallax.shader.getUniform("modelViewMat");
00244         parallax.normalMat = parallax.shader.getUniform("normalMat");
00245         parallax.projMat = parallax.shader.getUniform("projMat");
00246         parallax.texIdx = parallax.shader.getUniform("texIdx");
00247         parallax.lightPositions = parallax.shader.getUniform("lightPositions");
00248         parallax.lightColors = parallax.shader.getUniform("lightColors");
00249         parallax.colorArray = parallax.shader.getUniform("colorArray");
00250         parallax.detailArray = parallax.shader.getUniform("detailArray");
00251         // set the texture level of the texture arrays
00252         parallax.colorArray.set1i(0);
00253         parallax.detailArray.set1i(1);
00254 
00255         CHECK_GL_ERROR
00256 
00257         if(!glass.shader.init(TEXT("../bin/shaders/glass.vert"), TEXT("../bin/shaders/glass.frag"))) {
00258                 printf("Glass shader could not be initialized!");
00259                 return false;
00260         }
00261 
00262         glass.shader.use();
00263         glass.modelViewMat = glass.shader.getUniform("modelViewMat");
00264         glass.normalMat = glass.shader.getUniform("normalMat");
00265         glass.projMat = glass.shader.getUniform("projMat");
00266         glass.camPosition = glass.shader.getUniform("camPosition");
00267         glass.environment = glass.shader.getUniform("environment");
00268         // set the texture level of the texture arrays
00269         glass.environment.set1i(4);
00270         
00271         CHECK_GL_ERROR
00272 
00273         m_drawList = new DrawCall[2024];
00274         m_drawColorList = new DrawCall[2024];
00275         m_guiDrawList = new pIControl[1024];
00276 
00277         return true;
00278 }
00279 
00280 void Render::keepCurrentGeometry(void){
00281         m_keepGeometryVertices = m_vertexCount;
00282         m_keepGeometryIndices = m_indexCount;
00283         m_keepRenderCount = m_dRenderCount;
00284         m_keepColorRenderCount = m_dRenderColorCount;
00285         m_keepGuiCount = m_numGuiObjects;
00286 }
00287 
00288 void Render::resetCurrentGeometry(void){
00289         m_vertexCount = 4+8;
00290         m_indexCount = 6+36;
00291 }
00292 
00293 const float4x4 Render::getGuiMatrix(int width, int height)
00294 {
00295         //float4x4* mat = (float4x4*)malloc(sizeof(float4x4));
00296         float4x4 mat;
00297 
00298         mat(0,0) = 2.0f / (float)width;
00299         mat(0,1) = 0.0f;
00300         mat(0,2) = 0.0f;
00301         mat(0,3) = -1.0f;
00302 
00303         mat(1,0) = 0.0f;
00304         mat(1,1) = -2.0f / (float)height;
00305         mat(1,2) = 0.0f;
00306         mat(1,3) = 1.0f;
00307 
00308         mat(2,0) = 0.0f;
00309         mat(2,1) = 0.0f;
00310         mat(2,2) = 0.0f;
00311         mat(2,3) = -1.0f;
00312 
00313         mat(3,0) = 0.0f;
00314         mat(3,1) = 0.0f;
00315         mat(3,2) = 0.0f;
00316         mat(3,3) = 1.0f;
00317 
00318         return mat;
00319 }
00320 
00321 void Render::reserveDeviceMemory(int width, int height, RenderDesc& desc)
00322 {
00323         char* offset = 0;
00324         m_vertexSize = int(sizeof(Vertex));
00325         m_vertexCount = 0;
00326         m_indexCount = 0;
00327         m_textureCount = 0;
00328         renderDesc = &desc;
00329 
00330         // reserve vertex buffer data
00331         glGenBuffers(1, &m_vertexBuffer);
00332         glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
00333         glBufferData(GL_ARRAY_BUFFER, m_vertexSize*desc.maxVertices, 0, GL_STATIC_DRAW);
00334 
00335         CHECK_GL_ERROR
00336 
00337         // create vertex array
00338         // NOTE: A vertex array can only be created
00339         // while a vertex buffer is bound.
00340         glGenVertexArrays(1, &m_vertexArray);
00341         glBindVertexArray(m_vertexArray);
00342 
00343         for(GLuint i=0; i<ATTRIBUTE_COUNT; i++){
00344                 glEnableVertexAttribArray(i);
00345                 glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, m_vertexSize, offset);
00346                 offset += sizeof(float)*4;
00347         }
00348 
00349         CHECK_GL_ERROR
00350 
00351         // reserve index buffer data
00352         glGenBuffers(1, &m_indexBuffer);
00353         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
00354         glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*desc.maxIndices, 0, GL_STATIC_DRAW);
00355 
00356         CHECK_GL_ERROR
00357 
00358         // create texture handle
00359         glGenTextures(1, &m_colorTexture);
00360         glActiveTexture(GL_TEXTURE0);
00361         glBindTexture(GL_TEXTURE_2D_ARRAY, m_colorTexture);
00362 
00363         // set default parameters
00364         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00365         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00366         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_R, GL_REPEAT);
00367         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
00368         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
00369 
00370         // reserve mipmaps
00371         for(int m=0, w=desc.colorWidth, h=desc.colorHeight; w>0 || h>0; w/=2, h/=2, m++)
00372                 glTexImage3D(GL_TEXTURE_2D_ARRAY, m, GL_RGBA, w>0?w:1, h>0?h:1, desc.maxTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
00373 
00374         CHECK_GL_ERROR
00375 
00376         // create texture handle
00377         glGenTextures(1, &m_detailTexture);
00378         glActiveTexture(GL_TEXTURE1);
00379         glBindTexture(GL_TEXTURE_2D_ARRAY, m_detailTexture);
00380         // set default parameters
00381         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00382         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00383         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_R, GL_REPEAT);
00384         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
00385         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
00386         glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, desc.detailWidth, desc.detailWidth, desc.maxTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
00387 
00388         CHECK_GL_ERROR
00389 
00390         // create texture handle
00391         glGenTextures(1, &m_sharpTexture);
00392         glActiveTexture(GL_TEXTURE2);
00393         glBindTexture(GL_TEXTURE_2D, m_sharpTexture);
00394         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00395         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00396         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
00397         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
00398         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
00399         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
00400 
00401         // create texture handle
00402         glGenTextures(1, &m_blurTexture);
00403         glActiveTexture(GL_TEXTURE3);
00404         glBindTexture(GL_TEXTURE_2D, m_blurTexture);
00405         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00406         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00407         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
00408         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
00409         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
00410         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
00411         
00412         glGenRenderbuffers(1, &renderbufferDepth);
00413         glBindRenderbuffer(GL_RENDERBUFFER, renderbufferDepth);
00414         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
00415         
00416         glGenFramebuffers(1, &framebufferSharp);
00417         glBindFramebuffer(GL_FRAMEBUFFER, framebufferSharp);
00418         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_sharpTexture, 0);
00419         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbufferDepth);
00420         GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
00421         if(status != GL_FRAMEBUFFER_COMPLETE)
00422                 printf("%s\n", "The framebuffer object is incomplete.");
00423 
00424         glGenFramebuffers(1, &framebufferBlur);
00425         glBindFramebuffer(GL_FRAMEBUFFER, framebufferBlur);
00426         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_blurTexture, 0);
00427         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbufferDepth);
00428         status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
00429         if(status != GL_FRAMEBUFFER_COMPLETE)
00430                 printf("%s\n", "The framebuffer object is incomplete.");
00431 
00432         glBindFramebuffer(GL_FRAMEBUFFER, 0);
00433 
00434         CHECK_GL_ERROR
00435 
00436         // reserve standard rectangle for DOF
00437         MeshData meshData;
00438 
00439         meshData.vertexSize = sizeof(Vertex);
00440         meshData.vertexCount = 4;
00441         meshData.vertices = defaultRectVertices;
00442         meshData.indexCount = 6;
00443         meshData.indices = defaultRectIndices;
00444         rectGeom = (pGeometry)createGeometry(meshData);
00445 
00446         meshData.vertexCount = 8;
00447         meshData.vertices = defaultCubeVertices;
00448         meshData.indexCount = 36;
00449         meshData.indices = defaultCubeIndices;
00450         cubeGeom = (pGeometry)createGeometry(meshData);
00451 
00452         meshData.vertices = 0;
00453         meshData.indices = 0;
00454 }
00455 
00456 void Render::update(Camera& camera)
00457 {
00458         if(m_dRenderCount == 0)
00459                 return;
00460         float4 camPosition = float4(camera.getPosition(), 1.0f);
00461         Frustum<float> frustum(camera.getViewProjMatrix());
00462 
00463         if(m_enableCulling){
00464                 for(int i=0; i<m_dRenderCount; i++)
00465                         m_drawList[i].draw = m_drawList[i].visible && frustum.intersect(m_drawList[i].body->getPosition(), m_drawList[i].body->getRadius());
00466                 for(int i=0; i<m_dRenderColorCount; i++)
00467                         m_drawColorList[i].draw = m_drawColorList[i].visible && frustum.intersect(m_drawColorList[i].body->getPosition(), m_drawColorList[i].body->getRadius());
00468         }else{
00469                 for(int i=0; i<m_dRenderCount; i++)
00470                         m_drawList[i].draw = m_drawList[i].visible;
00471                 for(int i=0; i<m_dRenderColorCount; i++)
00472                         m_drawColorList[i].draw = m_drawColorList[i].visible;
00473         }
00474 
00475         // PASS 0: depth only draw
00476         glBindFramebuffer(GL_FRAMEBUFFER, framebufferSharp);
00477         glClear(GL_DEPTH_BUFFER_BIT);
00478         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
00479         depthOnly.shader.use();
00480         depthOnly.projMat.set4x4fv(&camera.getProjMatrix());
00481 
00482         for(int i=0; i<m_dRenderCount; i++) {
00483                 if(m_drawList[i].draw){
00484                         const pGeometry& geometry = m_drawList[i].geometry;
00485                         const pIBody& body = m_drawList[i].body;
00486                         const float4x4 modelViewMat = camera.getViewMatrix() * body->getTransformation();
00487                         depthOnly.modelViewMat.set4x4fv(&modelViewMat);
00488                         glDrawElementsBaseVertex(GL_TRIANGLES, geometry->getIndexCount(), GL_UNSIGNED_SHORT, geometry->getBaseIndex(), geometry->getBaseVertex());
00489                 }
00490         }
00491         CHECK_GL_ERROR
00492 
00493         // PASS: parallax shader into DOF render target
00494         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
00495         if(m_enableWireframe){
00496                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00497                 glClear(GL_COLOR_BUFFER_BIT);
00498         }
00499 
00500         /*if(m_enableLinearTexture) {
00501                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00502         } else {
00503                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00504         }
00505 
00506         if(m_enableMipmap) {
00507                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00508         } else {
00509                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00510         }*/
00511 
00512 
00513         parallax.shader.use();
00514         parallax.viewMat.set4x4fv(&camera.getViewMatrix());
00515         parallax.projMat.set4x4fv(&camera.getProjMatrix());
00516         parallax.lightPositions.set4fv(parallax.lightPositionArray, MAX_ACTIVE_LIGHTS);
00517         parallax.lightColors.set4fv(parallax.lightColorArray, MAX_ACTIVE_LIGHTS);
00518 
00519         for(int i=0; i<m_dRenderCount; i++) {
00520                 if(m_drawList[i].draw){
00521                         const pGeometry& geometry = m_drawList[i].geometry;
00522                         const pTexture& texture = m_drawList[i].texture;
00523                         const pIBody& body = m_drawList[i].body;
00524                         const float4x4 modelViewMat = camera.getViewMatrix() * body->getTransformation();
00525                         const float3x3 normalMat = transpose(inverse(modelViewMat).as3x3());
00526                         parallax.modelViewMat.set4x4fv(&modelViewMat);
00527                         parallax.normalMat.set3x3fv(&normalMat);
00528                         parallax.texIdx.set2f(float(texture->getTextureIndex()), 0.025f);
00529                         glDrawElementsBaseVertex(GL_TRIANGLES, geometry->getIndexCount(), GL_UNSIGNED_SHORT, geometry->getBaseIndex(), geometry->getBaseVertex());
00530                 }
00531         }
00532         CHECK_GL_ERROR
00533 
00534         // PASS: draw sky box
00535         if(m_skyBox){
00536                 skybox.shader.use();
00537                 skybox.modelViewMat.set4x4fv(&camera.getViewMatrix());
00538                 skybox.projMat.set4x4fv(&camera.getProjMatrix());
00539                 skybox.camPosition.set4fv(&camPosition);
00540                 glDrawElementsBaseVertex(GL_TRIANGLES, cubeGeom->getIndexCount(), GL_UNSIGNED_SHORT, cubeGeom->getBaseIndex(), cubeGeom->getBaseVertex());
00541                 CHECK_GL_ERROR
00542         }
00543 
00544         // PASS: glass shader into DOF render target
00545         if(m_enableBlending)
00546                 glEnable(GL_BLEND);
00547         glass.shader.use();
00548         glass.projMat.set4x4fv(&camera.getProjMatrix());
00549         glass.camPosition.set4fv(&camPosition);
00550 
00551         for(int i=0; i<m_dRenderColorCount; i++) {
00552                 if(m_drawColorList[i].draw){
00553                         const pGeometry& geometry = m_drawColorList[i].geometry;
00554                         const pIBody& body = m_drawColorList[i].body;
00555                         const float4x4 modelViewMat = camera.getViewMatrix() * body->getTransformation();
00556                         const float3x3 normalMat = transpose(inverse(modelViewMat).as3x3());
00557                         glass.modelViewMat.set4x4fv(&modelViewMat);
00558                         glass.normalMat.set3x3fv(&normalMat);
00559                         glDrawElementsBaseVertex(GL_TRIANGLES, geometry->getIndexCount(), GL_UNSIGNED_SHORT, geometry->getBaseIndex(), geometry->getBaseVertex());
00560                 }
00561         }
00562         if(m_enableBlending)
00563                 glDisable(GL_BLEND);
00564         CHECK_GL_ERROR
00565 
00566         // PASS: blur DOF render target
00567         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00568         glDisable(GL_DEPTH_TEST);
00569         glBindFramebuffer(GL_FRAMEBUFFER, framebufferBlur);
00570         glClear(GL_COLOR_BUFFER_BIT);
00571         blur.shader.use();
00572         blur.texture.set1i(2);
00573         glDrawElementsBaseVertex(GL_TRIANGLES, rectGeom->getIndexCount(), GL_UNSIGNED_SHORT, rectGeom->getBaseIndex(), rectGeom->getBaseVertex());
00574         blur.texture.set1i(3);
00575         glDrawElementsBaseVertex(GL_TRIANGLES, rectGeom->getIndexCount(), GL_UNSIGNED_SHORT, rectGeom->getBaseIndex(), rectGeom->getBaseVertex());
00576         CHECK_GL_ERROR
00577 
00578         // PASS: combine DOF render targets
00579         glBindFramebuffer(GL_FRAMEBUFFER, 0);
00580         depthOfField.shader.use();
00581         depthOfField.sharp.set1i(2);
00582         depthOfField.blured.set1i(3);
00583         glDrawElementsBaseVertex(GL_TRIANGLES, rectGeom->getIndexCount(), GL_UNSIGNED_SHORT, rectGeom->getBaseIndex(), rectGeom->getBaseVertex());
00584         CHECK_GL_ERROR
00585         
00586         // PASS: draw UI
00587         gui.shader.use();
00588         glEnable(GL_BLEND);
00589         if(m_guiShown) {
00590                 for(int i = 0; i < m_numGuiObjects; i++) {
00591                         const pIControl& control = m_guiDrawList[i];
00592                         if(control->getVisible()) {
00593                                 const pGeometry& geometry = (pGeometry)control->getGeometry();
00594                                 const pTexture& texture = (pTexture)control->getTexture();
00595                                 gui.guiUVIndex.set1i(control->getIndex());
00596                                 gui.guiTexIdx.set1f(float(texture->getTextureIndex()));
00597                                 glDrawElementsBaseVertex(GL_TRIANGLES, geometry->getIndexCount(), GL_UNSIGNED_SHORT, geometry->getBaseIndex(), geometry->getBaseVertex());
00598                         }
00599                 }
00600         } else {
00601                 for(int i = 0; i < m_numGuiObjects; i++) {
00602                         const pIControl& control = m_guiDrawList[i];
00603                         if(control->getVisible()) {
00604                                 const pGeometry& geometry = (pGeometry)control->getGeometry();
00605                                 const pTexture& texture = (pTexture)control->getTexture();
00606                                 if(wcscmp(control->getName(), TEXT("HUD")) == 0) {
00607                                         gui.guiUVIndex.set1i(control->getIndex());
00608                                         gui.guiTexIdx.set1f(float(texture->getTextureIndex()));
00609                                         glDrawElementsBaseVertex(GL_TRIANGLES, geometry->getIndexCount(), GL_UNSIGNED_SHORT, geometry->getBaseIndex(), geometry->getBaseVertex());
00610                                 }
00611                         }
00612                 }
00613         }
00614         glDisable(GL_BLEND);
00615         glEnable(GL_DEPTH_TEST);
00616 
00617         SwapBuffers(m_hdc);
00618 
00619         CHECK_GL_ERROR
00620 }
00621 
00622 pIGeometry Render::createGeometry(MeshData& mesh)
00623 {
00624         if(m_vertexCount + mesh.vertexCount > renderDesc->maxVertices || m_indexCount + mesh.indexCount > renderDesc->maxIndices){
00625                 printf("No more vertex or index memory available on the device.\n    Vertices (%i/%i)\n    Indices (%i/%i)\n",
00626                         m_vertexCount + mesh.vertexCount, renderDesc->maxVertices, m_indexCount + mesh.indexCount, renderDesc->maxIndices);
00627                 return 0;
00628         }
00629 
00630         if(mesh.vertexSize != m_vertexSize){
00631                 printf("Vertex size does not match (intern %i / file %i).\n", m_vertexSize, mesh.vertexSize);
00632                 return 0;
00633         }
00634                 
00635         pIGeometry geometry = new Geometry(m_vertexCount, mesh.vertexCount, (void*)(sizeof(ushort)*m_indexCount), mesh.indexCount);
00636 
00637 //#ifdef _DEBUG
00638 //      printf("%s\n", "Vertices");
00639 //      for(int i=0; i<mesh.vertexCount; i++)
00640 //              printf("    pos (%f %f %f)\n", mesh.vertices[i].position[0], mesh.vertices[i].position[1], mesh.vertices[i].position[2]);
00641 //      printf("\n%s\n", "Indices");
00642 //      for(int i=0; i<mesh.indexCount; i+=3)
00643 //              printf("    (%i %i %i)\n", int(mesh.indices[i]), int(mesh.indices[i+1]), int(mesh.indices[i+2]));
00644 //#endif
00645 
00646         glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
00647         glBufferSubData(GL_ARRAY_BUFFER, GLintptr(m_vertexSize*m_vertexCount), GLsizeiptr(m_vertexSize*mesh.vertexCount), mesh.vertices);
00648 
00649         CHECK_GL_ERROR
00650 
00651         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
00652         glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, GLintptr(sizeof(ushort)*m_indexCount), GLsizeiptr(sizeof(ushort)*mesh.indexCount), mesh.indices);
00653 
00654         CHECK_GL_ERROR
00655 
00656         m_vertexCount += mesh.vertexCount;
00657         m_indexCount += mesh.indexCount;
00658 
00659         return geometry;
00660 }
00661 
00662 pITexture Render::createTexture(TextureData& textureData)
00663 {
00664         // upload color data
00665         glActiveTexture(GL_TEXTURE0);
00666         glBindTexture(GL_TEXTURE_2D_ARRAY, m_colorTexture);
00667 
00668         CHECK_GL_ERROR
00669 
00670         for(int m=0, mData=0, w=renderDesc->colorWidth, h=renderDesc->colorHeight; w>0 || h>0; w/=2, h/=2, m++)
00671         {
00672                 for(;mData < textureData.mipmapsColor && (textureData.color[mData].width > w || textureData.color[mData].height > h); mData++);
00673                 if(mData == textureData.mipmapsColor)
00674                         break;
00675 
00676                 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, m, 0, 0, m_textureCount,
00677                         textureData.color[mData].width, textureData.color[mData].height,
00678                         1, GL_RGBA, GL_UNSIGNED_BYTE, textureData.color[mData].data);
00679         }
00680 
00681         CHECK_GL_ERROR
00682         
00683         // upload normal and height (=detail) data
00684         glActiveTexture(GL_TEXTURE1);
00685         glBindTexture(GL_TEXTURE_2D_ARRAY, m_detailTexture);
00686 
00687         CHECK_GL_ERROR
00688 
00689         for(int m=0, mData=0, w=renderDesc->detailWidth, h=renderDesc->detailHeight; w>0 || h>0; w/=2, h/=2, m++)
00690         {
00691                 for(;mData < textureData.mipmapsDetail && (textureData.detail[mData].width > w || textureData.detail[mData].height > h); mData++);
00692                 if(mData == textureData.mipmapsDetail)
00693                         break;
00694 
00695                 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, m, 0, 0, m_textureCount,
00696                         textureData.detail[mData].width, textureData.detail[mData].height,
00697                         1, GL_RGBA, GL_UNSIGNED_BYTE, textureData.detail[mData].data);
00698 
00699                 // no mipmapping
00700                 break;
00701         }
00702 
00703         CHECK_GL_ERROR
00704 
00705         return new Texture(GLint(m_textureCount++));
00706 }
00707 
00708 void Render::createSkyBox(TextureData& top, TextureData& bottom, TextureData& left, TextureData& right, TextureData& front, TextureData& back)
00709 {
00710         if(m_skyBox != 0){
00711                 glDeleteTextures(1, &m_skyBox);
00712                 m_skyBox = 0;
00713         }
00714         glGenTextures(1, &m_skyBox);
00715         glActiveTexture(GL_TEXTURE4);
00716         glBindTexture(GL_TEXTURE_CUBE_MAP, m_skyBox);
00717         glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00718         glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00719         glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT);
00720         glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
00721         glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
00722         glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, right.color[0].width,  right.color[0].height,  0, GL_RGBA, GL_UNSIGNED_BYTE, right.color[0].data);
00723         glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, left.color[0].width,   left.color[0].height,   0, GL_RGBA, GL_UNSIGNED_BYTE, left.color[0].data);
00724         glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, top.color[0].width,    top.color[0].height,    0, GL_RGBA, GL_UNSIGNED_BYTE, top.color[0].data);
00725         glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, bottom.color[0].width, bottom.color[0].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bottom.color[0].data);
00726         glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, front.color[0].width,  front.color[0].height,  0, GL_RGBA, GL_UNSIGNED_BYTE, front.color[0].data);
00727         glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, back.color[0].width,   back.color[0].height,   0, GL_RGBA, GL_UNSIGNED_BYTE, back.color[0].data);
00728 
00729         CHECK_GL_ERROR
00730 }
00731 
00732 void Render::setLight(int index, LightData& lightData)
00733 {
00734         if(0 <= index && index < 4){
00735                 parallax.lightColorArray[index] = lightData.color;
00736                 parallax.lightPositionArray[index] = lightData.position;
00737         }
00738 }
00739 
00740 pIControl Render::createControl(pIGeometry geometry, pITexture texture)
00741 {
00742         return new Control(geometry, texture);
00743 }
00744 
00745 pIControl Render::createButton(pIGeometry geometry, pITexture texture, void callback(pIControl))
00746 {
00747         return new Button(geometry, texture, callback);
00748 }
00749 
00750 void Render::freeTexture(pITexture& texture)
00751 {
00752         if(texture)
00753         {
00754                 delete texture;
00755                 texture = 0;
00756         }
00757 }
00758 
00759 void Render::freeGeometry(pIGeometry& geometry)
00760 {
00761         if(geometry)
00762         {
00763                 delete geometry;
00764                 geometry = 0;
00765         }
00766 }
00767 
00768 pInstance Render::createDrawCall(pITexture texture, pIGeometry geometry, pIBody body)
00769 {
00770         DrawCall& drawcall = m_drawList[m_dRenderCount];
00771         drawcall.texture = (pTexture)texture;
00772         drawcall.geometry = (pGeometry)geometry;
00773         drawcall.body = body;
00774         drawcall.visible = true;
00775         m_dRenderCount++;
00776         return &drawcall;
00777 }
00778 
00779 pInstance Render::createColorDrawCall(pIGeometry geometry, pIBody body)
00780 {
00781         DrawCall& drawcall = m_drawColorList[m_dRenderColorCount];
00782         drawcall.texture = 0;
00783         drawcall.geometry = (pGeometry)geometry;
00784         drawcall.body = body;
00785         drawcall.visible = true;
00786         m_dRenderColorCount++;
00787         return &drawcall;
00788 }
00789 
00790 void Render::createGuiDrawCall(pIControl control)
00791 {
00792         m_guiDrawList[m_numGuiObjects++] = control;
00793 }
00794 
00795 void Render::updateScreenSize(int width, int height)
00796 {
00797         glViewport(0,0,width,height);
00798         float4x4 mat = getGuiMatrix(width, height);
00799         gui.gui.set4x4fv(&mat,1);
00800 }
00801 
00802 void Render::clear(void)
00803 {
00804         //if(m_dRenderCount > m_keepRenderCount) {
00805         //      m_drawList.erase(m_drawList.begin() + m_keepRenderCount + 1, m_drawList.end());
00806         //}
00807         m_dRenderCount = m_keepRenderCount;
00808         
00809         //if(m_dRenderColorCount > m_keepColorRenderCount) {
00810         //      m_drawColorList.erase(m_drawColorList.begin() + m_keepColorRenderCount + 1, m_drawColorList.end());
00811         //}
00812         m_dRenderColorCount = m_keepColorRenderCount;
00813         
00814         //if(m_numGuiObjects > m_keepGuiCount) {
00815         //      m_guiDrawList.erase(m_guiDrawList.begin() + m_keepGuiCount + 1, m_guiDrawList.end());
00816         //}
00817         m_numGuiObjects = m_keepGuiCount;
00818         
00819         m_vertexCount = m_keepGeometryVertices;
00820         m_indexCount = m_keepGeometryIndices;
00821 }
00822 
00823 void Render::uninit(void)
00824 {
00825         SAVEDELETE(m_drawList);
00826         SAVEDELETE(m_drawColorList);
00827         SAVEDELETE(m_guiDrawList);
00828         freeGeometry((pIGeometry&)rectGeom);
00829         freeGeometry((pIGeometry&)cubeGeom);
00830         glass.shader.uninit();
00831         depthOnly.shader.uninit();
00832         blur.shader.uninit();
00833         depthOfField.shader.uninit();
00834         parallax.shader.uninit();
00835         gui.shader.uninit();
00836         wglMakeCurrent(0, 0);
00837         if(m_RC)
00838                 wglDeleteContext(m_RC);
00839         if(m_hwnd && m_hdc)
00840                 ReleaseDC(m_hwnd, m_hdc);
00841         m_RC = 0;
00842         m_hdc = 0;
00843         m_hwnd = 0;
00844 }
00845 
00846 void Render::initGLEW(void)
00847 {
00848         GLenum err = glewInit();
00849 
00850         if(err != GLEW_OK) {
00851                 printf("Error: %s\n", glewGetErrorString(err));
00852                 exit(1);
00853         }
00854 }
00855 
00856 void Render::toggleGui()
00857 {
00858         m_guiShown = !m_guiShown;
00859 }
00860 
00861 void Render::setEnableMipmap(bool b)
00862 {
00863         m_enableMipmap = b;
00864 
00865         glActiveTexture(GL_TEXTURE0);
00866         glBindTexture(GL_TEXTURE_2D_ARRAY, m_colorTexture);
00867         
00868         if(m_enableMipmap)
00869                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00870         else
00871                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00872 }
00873 
00874 void Render::setEnableLinearTexture(bool b)
00875 {
00876         m_enableLinearTexture = b;
00877 
00878         glActiveTexture(GL_TEXTURE0);
00879         glBindTexture(GL_TEXTURE_2D_ARRAY, m_colorTexture);
00880         
00881         if(m_enableLinearTexture)
00882                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00883         else
00884                 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00885 }

Generated on Fri Jun 18 2010 17:48:39 for Cannonball by  doxygen 1.7.0