void Render(float alpha, float elapsedtime) { static float time = 0; float world[16]; float view[16]; float proj[16]; float viewproj[16]; float viewinv[16]; float viewprojinv[16]; float eye[3]; float clip[4] = { 0, 0, 0, 0 }; orbitcamera.Animate(alpha); orbitcamera.GetViewMatrix(view); orbitcamera.GetProjectionMatrix(proj); orbitcamera.GetEyePosition(eye); clip[0] = orbitcamera.GetNearPlane(); clip[1] = orbitcamera.GetFarPlane(); GLMatrixInverse(viewinv, view); GLMatrixMultiply(viewproj, view, proj); GLMatrixInverse(viewprojinv, viewproj); glViewport(0, 0, screenwidth, screenheight); if( userefgtao ) { // using the reference spheres with GTAO refgbuffereffect->SetMatrix("matView", view); refgbuffereffect->SetMatrix("matViewInv", viewinv); refgbuffereffect->SetMatrix("matViewProjInv", viewprojinv); refgbuffereffect->SetVector("eyePos", eye); refgbuffereffect->SetVector("clipPlanes", clip); RenderReferenceWithGTAO(proj, clip, eye); } else { // using the reference spheres with path tracing RenderReferenceWithPathTracing(viewprojinv, eye, time); } if( drawtext ) { // render text glViewport(5, screenheight - 517, 512, 512); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); float xzplane[4] = { 0, 1, 0, -0.5f }; GLMatrixReflect(world, xzplane); presenteffect->SetMatrix("matTexture", world); presenteffect->Begin(); { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, text1); screenquad->Draw(); } presenteffect->End(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); GLMatrixIdentity(world); presenteffect->SetMatrix("matTexture", world); } time += elapsedtime; #ifdef _DEBUG GLenum err = glGetError(); if( err != GL_NO_ERROR ) std::cout << "Error\n"; #endif SwapBuffers(hdc); }
void Render(float alpha, float elapsedtime) { float world[16]; float view[16]; float proj[16]; float eye[3]; basiccamera.Animate(alpha); basiccamera.GetViewMatrix(view); basiccamera.GetProjectionMatrix(proj); basiccamera.GetEyePosition(eye); GLVec4Set(uniformDTO.vsuniforms.lightPos, 6, 3, 10, 1); GLVec4Set(uniformDTO.vsuniforms.eyePos, eye[0], eye[1], eye[2], 1); GLMatrixMultiply(uniformDTO.vsuniforms.matViewProj, view, proj); GLMatrixScaling(uniformDTO.vsuniforms.matWorld, OBJECT_SCALE, OBJECT_SCALE, OBJECT_SCALE); switch( rendermethod ) { case 1: GLVec4Set(uniformDTO.fsuniforms.color, 1, 0, 0, 1); break; case 2: GLVec4Set(uniformDTO.fsuniforms.color, 1, 0.5f, 0, 1); break; case 3: GLVec4Set(uniformDTO.fsuniforms.color, 1, 1, 0, 1); break; case 4: GLVec4Set(uniformDTO.fsuniforms.color, 0, 0.75f, 0, 1); break; case 5: GLVec4Set(uniformDTO.fsuniforms.color, 0, 1, 0, 1); break; default: break; } // render pass OpenGLEffect* effect = ((rendermethod > 1) ? effect2 : effect1); GLsync sync = 0; if( rendermethod == 1 ) { effect1->SetMatrix("matViewProj", uniformDTO.vsuniforms.matViewProj); effect1->SetVector("lightPos", uniformDTO.vsuniforms.lightPos); effect1->SetVector("eyePos", uniformDTO.vsuniforms.eyePos); effect1->SetVector("color", uniformDTO.fsuniforms.color); } framebuffer->Set(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); effect->Begin(); { for( int i = 0; i < GRID_SIZE; ++i ) { for( int j = 0; j < GRID_SIZE; ++j ) { for( int k = 0; k < GRID_SIZE; ++k ) { if( currentcopy >= UNIFORM_COPIES ) { if( rendermethod == 4 || rendermethod == 2 ) { sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); } else if( rendermethod == 5 ) { glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer3); glUnmapBuffer(GL_UNIFORM_BUFFER); persistentdata = (EffectUniformBlock*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, UNIFORM_COPIES * sizeof(EffectUniformBlock), GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_BUFFER_BIT|GL_MAP_PERSISTENT_BIT|GL_MAP_COHERENT_BIT); assert(persistentdata != 0); glBindBuffer(GL_UNIFORM_BUFFER, 0); currentcopy = 0; } } uniformDTO.vsuniforms.matWorld[12] = GRID_SIZE * -0.5f + i; uniformDTO.vsuniforms.matWorld[13] = GRID_SIZE * -0.5f + j; uniformDTO.vsuniforms.matWorld[14] = GRID_SIZE * -0.5f + k; if( rendermethod == 1 ) { effect1->SetMatrix("matWorld", uniformDTO.vsuniforms.matWorld); effect1->CommitChanges(); } else { if( rendermethod == 2 ) { if( sync != 0 ) { glWaitSync(sync, 0, GL_TIMEOUT_IGNORED); glDeleteSync(sync); sync = 0; } glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer1); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(EffectUniformBlock), &uniformDTO); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer1, 0, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer1, offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); } else if( rendermethod == 3 ) { glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer1); void* data = glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(EffectUniformBlock), GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_RANGE_BIT); { memcpy(data, &uniformDTO, sizeof(EffectUniformBlock)); } glUnmapBuffer(GL_UNIFORM_BUFFER); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer1, 0, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer1, offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); } else if( rendermethod == 4 ) { GLintptr baseoffset = currentcopy * sizeof(EffectUniformBlock); GLbitfield flags = GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_RANGE_BIT|GL_MAP_UNSYNCHRONIZED_BIT; if( sync != 0 ) { GLenum result = 0; GLbitfield waitflags = GL_SYNC_FLUSH_COMMANDS_BIT; do { result = glClientWaitSync(sync, waitflags, 500000); waitflags = 0; if( result == GL_WAIT_FAILED ) { std::cout << "glClientWaitSync() failed!\n"; break; } } while( result == GL_TIMEOUT_EXPIRED ); glDeleteSync(sync); sync = 0; currentcopy = 0; baseoffset = 0; } glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer2); void* data = glMapBufferRange(GL_UNIFORM_BUFFER, baseoffset, sizeof(EffectUniformBlock), flags); assert(data != 0); { memcpy(data, &uniformDTO, sizeof(EffectUniformBlock)); } glUnmapBuffer(GL_UNIFORM_BUFFER); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer2, baseoffset, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer2, baseoffset + offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); ++currentcopy; } else if( rendermethod == 5 ) { GLintptr baseoffset = currentcopy * sizeof(EffectUniformBlock); memcpy(persistentdata + currentcopy, &uniformDTO, sizeof(EffectUniformBlock)); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer3, baseoffset, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer3, baseoffset + offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); ++currentcopy; } } mesh->DrawSubset(0); } } } } effect->End(); framebuffer->Unset(); // present GLMatrixIdentity(world); glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); basic2D->SetMatrix("matTexture", world); basic2D->Begin(); { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, framebuffer->GetColorAttachment(0)); screenquad->Draw(); } basic2D->End(); // render text glViewport(5, screenheight - 517, 512, 512); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); float xzplane[4] = { 0, 1, 0, -0.5f }; GLMatrixReflect(world, xzplane); basic2D->SetMatrix("matTexture", world); basic2D->Begin(); { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, text1); screenquad->Draw(); } basic2D->End(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); glViewport(0, 0, screenwidth, screenheight); // check errors GLenum err = glGetError(); if( err != GL_NO_ERROR ) std::cout << "Error\n"; SwapBuffers(hdc); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { OpenGLColor grcolor(0xffdddddd); OpenGLColor cvcolor(0xff7470ff); OpenGLColor splinecolor(0xff000000); OpenGLColor outsidecolor(0.75f, 0.75f, 0.8f, 1); OpenGLColor insidecolor(1, 0.66f, 0.066f, 1); float world[16]; float view[16]; float proj[16]; float viewproj[16]; float pointsize[2] = { 10.0f / screenwidth, 10.0f / screenheight }; float grthickness[2] = { 1.5f / screenwidth, 1.5f / screenheight }; float cvthickness[2] = { 2.0f / screenwidth, 2.0f / screenheight }; float spthickness[2] = { 3.0f / screenwidth, 3.0f / screenheight }; float spviewport[] = { 0, 0, (float)screenwidth, (float)screenheight }; float eye[3] = { 5, 4, 15 }; float look[3] = { 5, 4, 5 }; float up[3] = { 0, 1, 0 }; float lightdir[4] = { 0, 1, 0, 0 }; float fwd[3]; float orient[2]; // play with ortho matrix instead of viewport (line thickness remains constant) ConvertToSplineViewport(spviewport[0], spviewport[1]); ConvertToSplineViewport(spviewport[2], spviewport[3]); GLMatrixIdentity(world); GLMatrixOrthoRH(proj, spviewport[0], spviewport[2], spviewport[1], spviewport[3], -1, 1); glViewport(0, 0, screenwidth, screenheight); glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST); renderlines->SetMatrix("matViewProj", proj); renderlines->SetMatrix("matWorld", world); renderlines->SetVector("color", &grcolor.r); renderlines->SetVector("lineThickness", grthickness); renderlines->Begin(); { supportlines->DrawSubset(0); renderlines->SetVector("color", &cvcolor.r); renderlines->SetVector("lineThickness", cvthickness); renderlines->CommitChanges(); supportlines->DrawSubset(1); if( hascompute ) { renderlines->SetVector("lineThickness", spthickness); renderlines->SetVector("color", &splinecolor.r); renderlines->CommitChanges(); curve->DrawSubset(0); } } renderlines->End(); renderpoints->SetMatrix("matViewProj", proj); renderpoints->SetMatrix("matWorld", world); renderpoints->SetVector("color", &cvcolor.r); renderpoints->SetVector("pointSize", pointsize); renderpoints->Begin(); { glBindVertexArray(supportlines->GetVertexLayout()); glDrawArrays(GL_POINTS, 44, numcontrolvertices); } renderpoints->End(); // render surface in a smaller viewport if( !fullscreen ) { glEnable(GL_SCISSOR_TEST); glScissor(screenwidth - surfvpwidth - 10, screenheight - surfvpheight - 10, surfvpwidth, surfvpheight); } glClearColor(0.0f, 0.125f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); glEnable(GL_DEPTH_TEST); if( fullscreen ) glViewport(0, 0, screenwidth, screenheight); else glViewport(screenwidth - surfvpwidth - 10, screenheight - surfvpheight - 10, surfvpwidth, surfvpheight); cameraangle.smooth(orient, alpha); GLVec3Subtract(fwd, look, eye); GLMatrixRotationYawPitchRoll(view, orient[0], orient[1], 0); GLVec3Transform(fwd, fwd, view); GLVec3Subtract(eye, look, fwd); GLMatrixPerspectiveRH(proj, M_PI / 3, 4.0f / 3.0f, 0.1f, 50.0f); GLMatrixLookAtRH(view, eye, look, up); GLMatrixMultiply(viewproj, view, proj); if( wireframe ) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_CULL_FACE); if( hascompute ) { rendersurface->SetMatrix("matViewProj", viewproj); rendersurface->SetMatrix("matWorld", world); rendersurface->SetMatrix("matWorldInv", world); // its id rendersurface->SetVector("lightDir", lightdir); rendersurface->SetVector("eyePos", eye); rendersurface->SetVector("outsideColor", &outsidecolor.r); rendersurface->SetVector("insideColor", &insidecolor.r); rendersurface->SetInt("isWireMode", wireframe); rendersurface->Begin(); { surface->DrawSubset(0); } rendersurface->End(); } glEnable(GL_CULL_FACE); if( wireframe ) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // render text if( !fullscreen ) { glViewport(3, 0, 800, 130); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); float xzplane[4] = { 0, 1, 0, -0.5f }; GLMatrixReflect(world, xzplane); basic2D->SetMatrix("matTexture", world); basic2D->SetInt("sampler0", 0); basic2D->Begin(); { glBindTexture(GL_TEXTURE_2D, text1); screenquad->Draw(); } basic2D->End(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); } #ifdef _DEBUG // check errors GLenum err = glGetError(); if( err != GL_NO_ERROR ) std::cout << "Error\n"; #endif SwapBuffers(hdc); mousedx = mousedy = 0; }