void RenderScene() { static GLfloat vSunColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; static GLfloat vEarthColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; static GLfloat vMoonColor[] = { 1.0f, 1.0f, 0.0f, 1.0f }; static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); M3DVector4f vLightPos = { 0.0f, 10.0f, 5.0f, 1.0f }; M3DVector4f vLightEyePos; m3dTransformVector4(vLightEyePos, vLightPos, mCamera); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); modelViewMatrix.Translate(0.0f, 0.0f, -3.5f); modelViewMatrix.PushMatrix(); modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSunColor); sunSphereBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vEarthColor); earthSphereBatch.Draw(); modelViewMatrix.Rotate(yRot * -4.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.4f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vMoonColor); moonSphereBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
/////////////////////////////////////////////////////////////////////////////// // Draw the scene // void DrawWorld(GLfloat yRot) { M3DMatrix44f mCamera; modelViewMatrix.GetMatrix(mCamera); // Need light position relative to the Camera M3DVector4f vLightTransformed; m3dTransformVector4(vLightTransformed, vLightPos, mCamera); // Draw the light source as a small white unshaded sphere modelViewMatrix.PushMatrix(); modelViewMatrix.Translatev(vLightPos); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vWhite); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); // Draw stuff relative to the camera modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, 0.2f, -2.5f); modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeline.GetProjectionMatrix(), vLightTransformed, vGreen, 0); torusBatch.Draw(); modelViewMatrix.PopMatrix(); }
// Called to draw scene void RenderScene(void) { static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 0.0f, 1.0f); GLfloat vEyeLight[] = { -100.0f, 100.0f, 150.0f }; GLfloat vAmbientColor[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat vDiffuseColor[] = { 1.0f, 1.0f, 1.0f, 1.0f}; glUseProgram(normalMapShader); glUniform4fv(locAmbient, 1, vAmbientColor); glUniform4fv(locDiffuse, 1, vDiffuseColor); glUniform3fv(locLight, 1, vEyeLight); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix()); glUniform1i(locColorMap, 0); glUniform1i(locNormalMap, 1); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
// Called to draw scene void RenderScene(void) { // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Turn culling on if flag is set if(iCull) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); // Enable depth testing if flag is set if(iDepth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); modelViewMatix.PushMatrix(viewFrame); GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f }; //shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vRed); shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vRed); torusBatch.Draw(); modelViewMatix.PopMatrix(); glutSwapBuffers(); }
void RenderScene(void) { static CStopWatch rotTimer; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); { modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.f, 0.0f, 1.0f, 0.0f); glUseProgram(toonShader); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeLine.GetModelViewMatrix()); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeLine.GetModelViewProjectionMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeLine.GetNormalMatrix()); glUniform3fv(locLP, 1, vEyeLight); glUniform1i(locColorTable, 0); torusBatch.Draw(); } modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
// Called to draw scene void RenderScene(void) { static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 13.0f, 1.0f, 0.0f, 0.0f); GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f }; GLfloat vAmbientColor[] = { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat vDiffuseColor[] = { 0.1f, 1.0f, 0.1f, 1.0f }; GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f }; glUseProgram(explodeProgram); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix()); float push_out = sinf(rotTimer.GetElapsedSeconds() * 3.0f) * 0.1f + 0.2f; glUniform1f(locPushOut, push_out); torusBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. void SetupRC() { // Initialze Shader Manager shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); #ifndef OPENGL_ES glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); #else torusBatch.SetPrimitiveType(GL_LINE_LOOP); #endif glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // This makes a torus gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30); floorBatch.Begin(GL_LINES, 324); for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) { floorBatch.Vertex3f(x, -0.55f, 20.0f); floorBatch.Vertex3f(x, -0.55f, -20.0f); floorBatch.Vertex3f(20.0f, -0.55f, x); floorBatch.Vertex3f(-20.0f, -0.55f, x); } floorBatch.End(); }
// Called to draw scene static void RenderScene(void) { static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(40.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Rotate(20.0f, 1.0f, 0.0f, 0.0f); float f = (float)rotTimer.GetElapsedSeconds(); GLfloat vViewpoint[] = { sinf(f * 3.1f) * 30.0f, cosf(f * 2.4f) * 30.0f, sinf(f * 1.7f) * 30.0f }; glUseProgram(cullingShader); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix()); glUniform3fv(locViewpoint, 1, vViewpoint); torusBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
void drawSun() { //draw the sphere batch as a sun modelViewMatrix.PushMatrix(); modelViewMatrix.Translatev(vLightPosition); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),vWhite); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); }
////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. void SetupRC() { // Initialze Shader Manager shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); #ifndef OPENGL_ES glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); #else torusBatch.SetPrimitiveType(GL_LINE_LOOP); sphereBatch.SetPrimitiveType(GL_LINE_LOOP); #endif glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // This makes a torus gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30); // This make a sphere gltMakeSphere(sphereBatch, 0.1f, 26, 13); floorBatch.Begin(GL_LINES, 324); for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) { floorBatch.Vertex3f(x, -0.55f, 20.0f); floorBatch.Vertex3f(x, -0.55f, -20.0f); floorBatch.Vertex3f(20.0f, -0.55f, x); floorBatch.Vertex3f(-20.0f, -0.55f, x); } floorBatch.End(); // Randomly place the spheres for(int i = 0; i < NUM_SPHERES; i++) { GLfloat x = ((GLfloat)((rand() % 400) - 200) * 0.1f); GLfloat z = ((GLfloat)((rand() % 400) - 200) * 0.1f); spheres[i].SetOrigin(x, 0.0f, z); } }
void drawTorus(GLfloat yRot) { M3DMatrix44f mCamera; M3DVector4f vLightTransform; modelViewMatrix.GetMatrix(mCamera); m3dTransformVector4(vLightTransform,vLightPosition,mCamera); modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0,0.5,-1.0); modelViewMatrix.Rotate(yRot,0.0,1.0,0.0); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeline.GetProjectionMatrix(), vLightTransform,vGreen,0); torusBatch.Draw(); modelViewMatrix.PopMatrix(); }
/////////////////////////////////////////////////////////////////////////////// // Draw the scene // void DrawWorld(GLfloat yRot) { M3DMatrix44f mCamera; modelViewMatrix.GetMatrix(mCamera); // Need light position relative to the Camera M3DVector4f vLightTransformed; m3dTransformVector4(vLightTransformed, vLightPos, mCamera); // Draw the light source as a small white unshaded sphere modelViewMatrix.PushMatrix(); modelViewMatrix.Translatev(vLightPos); if(bUseFBO) UseProcessProgram(vLightPos, vWhite, -1); else shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vWhite); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); // Draw stuff relative to the camera modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, 0.2f, -2.5f); modelViewMatrix.PushMatrix(); modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.0, (GLfloat)-0.60, 0.0); modelViewMatrix.Scale((GLfloat)0.02, (GLfloat)0.006, (GLfloat)0.02); glBindTexture(GL_TEXTURE_2D, ninjaTex[0]); if(bUseFBO) { UseProcessProgram(vLightTransformed, vWhite, 0); } else { shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), 0); } ninja.Render(0,0); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); }
// Called to draw scene void RenderScene(void) { // Color values static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; // Time Based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix) modelViewMatrix.PushMatrix(); // Draw the ground shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); // Draw the spinning Torus modelViewMatrix.Translate(0.0f, 0.0f, -2.5f); modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor); torusBatch.Draw(); // Restore the previous modleview matrix (the idenity matrix) modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); }
// Called to draw scene void RenderScene(void) { // Clear the window glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); M3DMatrix44f mCamera; M3DMatrix44f mCameraRotOnly; M3DMatrix44f mInverseCamera; viewFrame.GetCameraMatrix(mCamera, false); viewFrame.GetCameraMatrix(mCameraRotOnly, true); m3dInvertMatrix44(mInverseCamera, mCameraRotOnly); modelViewMatrix.PushMatrix(); // Draw the sphere modelViewMatrix.MultMatrix(mCamera); glUseProgram(reflectionShader); glUniformMatrix4fv(locMVPReflect, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMVReflect, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNormalReflect, 1, GL_FALSE, transformPipeline.GetNormalMatrix()); glUniformMatrix4fv(locInvertedCamera, 1, GL_FALSE, mInverseCamera); glUniform1i(locCubeMap, 0); glUniform1i(locTarnishMap, 1); glEnable(GL_CULL_FACE); sphereBatch.Draw(); glDisable(GL_CULL_FACE); modelViewMatrix.PopMatrix(); modelViewMatrix.PushMatrix(); modelViewMatrix.MultMatrix(mCameraRotOnly); glUseProgram(skyBoxShader); glUniformMatrix4fv(locMVPSkyBox, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); cubeBatch.Draw(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); }
void RenderScence(void) { static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f;//每秒旋转60度? //Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); M3DMatrix44f mTranslate, mRotate, mModelView, mModelViewProjection; //生成一个平移矩阵,用于将花环移回视野中,有个问题,1.0f会是什么效果?1.0会看不见花托。有可能截头体是世界坐标1.0-1000.0f是指距离camera的。所以此矩阵是相对于摄像机的操作,移到摄像机面前2.5f深度距离处 //即摄像机所在的地方为原点(0,0,0) m3dTranslationMatrix44(mTranslate, 0.0f, 0.0f, -2.5f); //生成旋转矩阵 以y为轴,旋转时间t产生的旋转角 yRot m3dRotationMatrix44(mRotate, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f); //组合为模型变换矩阵,而原模型矩阵在最右边,即先旋转再平移 ? m3dMatrixMultiply44(mModelView, mTranslate, mRotate); //组合成模型投影矩阵,而原模型矩阵在最右边,先做模型变换再投影 ? m3dMatrixMultiply44(mModelViewProjection, viewFrustum.GetProjectionMatrix(), mModelView); GLfloat vBlack[] = { 1.0f, 1.0f, 1.0f, 1.0f }; //选择要配置的渲染操作类型,非smooth,每种模式对应的参数要熟悉才能正确使用 shaderManger.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vBlack);//黑色背景?不是,是设置当前画笔颜色 torusBatch.Draw(); glutSwapBuffers(); glutPostRedisplay(); //整个流程是:先旋转在平移,然后进行投影剪裁,最后光栅化。UseStockShader 和draw函数之前的变换管线还是不清晰 }
// Called to draw scene void RenderScene(void) { static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f); GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f }; GLfloat vAmbientColor[] = { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat vDiffuseColor[] = { 0.1f, 1.0f, 0.1f, 1.0f }; GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f }; glUseProgram(ADSDissloveShader); glUniform4fv(locAmbient, 1, vAmbientColor); glUniform4fv(locDiffuse, 1, vDiffuseColor); glUniform4fv(locSpecular, 1, vSpecularColor); glUniform3fv(locLight, 1, vEyeLight); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix()); glUniform1i(locTexture, 1); float fFactor = fmod(rotTimer.GetElapsedSeconds(), 10.0f); fFactor /= 10.0f; glUniform1f(locDissolveFactor, fFactor); torusBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
void RenderScene(void) { static CStopWatch rotTimer; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); mvMatrix.PushMatrix(viewFrame); mvMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f); GLfloat vAmbientColor[] = { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat vDiffuseColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f }; glUseProgram(ADSGouraudShader); glUniform4fv(locAC, 1, vAmbientColor); glUniform4fv(locDC, 1, vDiffuseColor); glUniform4fv(locSC, 1, vSpecularColor); glUniform3fv(locLP, 1, vEyeLight); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeLine.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeLine.GetModelViewMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeLine.GetNormalMatrix()); sphereBatch.Draw(); mvMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }
// Draw a cylinder. Much like gluCylinder void gltMakeCylinder(GLTriangleBatch& cylinderBatch, GLfloat baseRadius, GLfloat topRadius, GLfloat fLength, GLint numSlices, GLint numStacks) { float fRadiusStep = (topRadius - baseRadius) / float(numStacks); GLfloat fStepSizeSlice = (3.1415926536f * 2.0f) / float(numSlices); M3DVector3f vVertex[4]; M3DVector3f vNormal[4]; M3DVector2f vTexture[4]; cylinderBatch.BeginMesh(numSlices * numStacks * 6); GLfloat ds = 1.0f / float(numSlices); GLfloat dt = 1.0f / float(numStacks); GLfloat s; GLfloat t; for (int i = 0; i < numStacks; i++) { if(i == 0) t = 0.0f; else t = float(i) * dt; float tNext; if(i == (numStacks - 1)) tNext = 1.0f; else tNext = float(i+1) * dt; float fCurrentRadius = baseRadius + (fRadiusStep * float(i)); float fNextRadius = baseRadius + (fRadiusStep * float(i+1)); float theyta; float theytaNext; float fCurrentZ = float(i) * (fLength / float(numStacks)); float fNextZ = float(i+1) * (fLength / float(numStacks)); float zNormal = 0.0f; if(!m3dCloseEnough(baseRadius - topRadius, 0.0f, 0.00001f)) { // Rise over run... zNormal = (baseRadius - topRadius); } for (int j = 0; j < numSlices; j++) { if(j == 0) s = 0.0f; else s = float(j) * ds; float sNext; if(j == (numSlices -1)) sNext = 1.0f; else sNext = float(j+1) * ds; theyta = fStepSizeSlice * float(j); if(j == (numSlices - 1)) theytaNext = 0.0f; else theytaNext = fStepSizeSlice * (float(j+1)); // Inner First vVertex[1][0] = cos(theyta) * fCurrentRadius; // X vVertex[1][1] = sin(theyta) * fCurrentRadius; // Y vVertex[1][2] = fCurrentZ; // Z vNormal[1][0] = vVertex[1][0]; // Surface Normal, same for everybody vNormal[1][1] = vVertex[1][1]; vNormal[1][2] = zNormal; m3dNormalizeVector3(vNormal[1]); vTexture[1][0] = s; // Texture Coordinates, I have no idea... vTexture[1][1] = t; // Outer First vVertex[0][0] = cos(theyta) * fNextRadius; // X vVertex[0][1] = sin(theyta) * fNextRadius; // Y vVertex[0][2] = fNextZ; // Z if(!m3dCloseEnough(fNextRadius, 0.0f, 0.00001f)) { vNormal[0][0] = vVertex[0][0]; // Surface Normal, same for everybody vNormal[0][1] = vVertex[0][1]; // For cones, tip is tricky vNormal[0][2] = zNormal; m3dNormalizeVector3(vNormal[0]); } else memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f)); vTexture[0][0] = s; // Texture Coordinates, I have no idea... vTexture[0][1] = tNext; // Inner second vVertex[3][0] = cos(theytaNext) * fCurrentRadius; // X vVertex[3][1] = sin(theytaNext) * fCurrentRadius; // Y vVertex[3][2] = fCurrentZ; // Z vNormal[3][0] = vVertex[3][0]; // Surface Normal, same for everybody vNormal[3][1] = vVertex[3][1]; vNormal[3][2] = zNormal; m3dNormalizeVector3(vNormal[3]); vTexture[3][0] = sNext; // Texture Coordinates, I have no idea... vTexture[3][1] = t; // Outer second vVertex[2][0] = cos(theytaNext) * fNextRadius; // X vVertex[2][1] = sin(theytaNext) * fNextRadius; // Y vVertex[2][2] = fNextZ; // Z if(!m3dCloseEnough(fNextRadius, 0.0f, 0.00001f)) { vNormal[2][0] = vVertex[2][0]; // Surface Normal, same for everybody vNormal[2][1] = vVertex[2][1]; vNormal[2][2] = zNormal; m3dNormalizeVector3(vNormal[2]); } else memcpy(vNormal[2], vNormal[3], sizeof(M3DVector3f)); vTexture[2][0] = sNext; // Texture Coordinates, I have no idea... vTexture[2][1] = tNext; cylinderBatch.AddTriangle(vVertex, vNormal, vTexture); // Rearrange for next triangle memcpy(vVertex[0], vVertex[1], sizeof(M3DVector3f)); memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f)); memcpy(vTexture[0], vTexture[1], sizeof(M3DVector2f)); memcpy(vVertex[1], vVertex[3], sizeof(M3DVector3f)); memcpy(vNormal[1], vNormal[3], sizeof(M3DVector3f)); memcpy(vTexture[1], vTexture[3], sizeof(M3DVector2f)); cylinderBatch.AddTriangle(vVertex, vNormal, vTexture); } } cylinderBatch.End(); }
void gltMakeDisk(GLTriangleBatch& diskBatch, GLfloat innerRadius, GLfloat outerRadius, GLint nSlices, GLint nStacks) { // How much to step out each stack GLfloat fStepSizeRadial = outerRadius - innerRadius; if(fStepSizeRadial < 0.0f) // Dum dum... fStepSizeRadial *= -1.0f; fStepSizeRadial /= float(nStacks); GLfloat fStepSizeSlice = (3.1415926536f * 2.0f) / float(nSlices); diskBatch.BeginMesh(nSlices * nStacks * 6); M3DVector3f vVertex[4]; M3DVector3f vNormal[4]; M3DVector2f vTexture[4]; float fRadialScale = 1.0f / outerRadius; for(GLint i = 0; i < nStacks; i++) // Stacks { float theyta; float theytaNext; for(GLint j = 0; j < nSlices; j++) // Slices { float inner = innerRadius + (float(i)) * fStepSizeRadial; float outer = innerRadius + (float(i+1)) * fStepSizeRadial; theyta = fStepSizeSlice * float(j); if(j == (nSlices - 1)) theytaNext = 0.0f; else theytaNext = fStepSizeSlice * (float(j+1)); // Inner First vVertex[0][0] = cos(theyta) * inner; // X vVertex[0][1] = sin(theyta) * inner; // Y vVertex[0][2] = 0.0f; // Z vNormal[0][0] = 0.0f; // Surface Normal, same for everybody vNormal[0][1] = 0.0f; vNormal[0][2] = 1.0f; vTexture[0][0] = ((vVertex[0][0] * fRadialScale) + 1.0f) * 0.5f; vTexture[0][1] = ((vVertex[0][1] * fRadialScale) + 1.0f) * 0.5f; // Outer First vVertex[1][0] = cos(theyta) * outer; // X vVertex[1][1] = sin(theyta) * outer; // Y vVertex[1][2] = 0.0f; // Z vNormal[1][0] = 0.0f; // Surface Normal, same for everybody vNormal[1][1] = 0.0f; vNormal[1][2] = 1.0f; vTexture[1][0] = ((vVertex[1][0] * fRadialScale) + 1.0f) * 0.5f; vTexture[1][1] = ((vVertex[1][1] * fRadialScale) + 1.0f) * 0.5f; // Inner Second vVertex[2][0] = cos(theytaNext) * inner; // X vVertex[2][1] = sin(theytaNext) * inner; // Y vVertex[2][2] = 0.0f; // Z vNormal[2][0] = 0.0f; // Surface Normal, same for everybody vNormal[2][1] = 0.0f; vNormal[2][2] = 1.0f; vTexture[2][0] = ((vVertex[2][0] * fRadialScale) + 1.0f) * 0.5f; vTexture[2][1] = ((vVertex[2][1] * fRadialScale) + 1.0f) * 0.5f; // Outer Second vVertex[3][0] = cos(theytaNext) * outer; // X vVertex[3][1] = sin(theytaNext) * outer; // Y vVertex[3][2] = 0.0f; // Z vNormal[3][0] = 0.0f; // Surface Normal, same for everybody vNormal[3][1] = 0.0f; vNormal[3][2] = 1.0f; vTexture[3][0] = ((vVertex[3][0] * fRadialScale) + 1.0f) * 0.5f; vTexture[3][1] = ((vVertex[3][1] * fRadialScale) + 1.0f) * 0.5f; diskBatch.AddTriangle(vVertex, vNormal, vTexture); // Rearrange for next triangle memcpy(vVertex[0], vVertex[1], sizeof(M3DVector3f)); memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f)); memcpy(vTexture[0], vTexture[1], sizeof(M3DVector2f)); memcpy(vVertex[1], vVertex[3], sizeof(M3DVector3f)); memcpy(vNormal[1], vNormal[3], sizeof(M3DVector3f)); memcpy(vTexture[1], vTexture[3], sizeof(M3DVector2f)); diskBatch.AddTriangle(vVertex, vNormal, vTexture); } } diskBatch.End(); }
///////////////////////////////////////////////////////////////////////////////////////////////// // Make a sphere void gltMakeSphere(GLTriangleBatch& sphereBatch, GLfloat fRadius, GLint iSlices, GLint iStacks) { GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks; GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices; GLfloat ds = 1.0f / (GLfloat) iSlices; GLfloat dt = 1.0f / (GLfloat) iStacks; GLfloat t = 1.0f; GLfloat s = 0.0f; GLint i, j; // Looping variables sphereBatch.BeginMesh(iSlices * iStacks * 6); for (i = 0; i < iStacks; i++) { GLfloat rho = (GLfloat)i * drho; GLfloat srho = (GLfloat)(sin(rho)); GLfloat crho = (GLfloat)(cos(rho)); GLfloat srhodrho = (GLfloat)(sin(rho + drho)); GLfloat crhodrho = (GLfloat)(cos(rho + drho)); // Many sources of OpenGL sphere drawing code uses a triangle fan // for the caps of the sphere. This however introduces texturing // artifacts at the poles on some OpenGL implementations s = 0.0f; M3DVector3f vVertex[4]; M3DVector3f vNormal[4]; M3DVector2f vTexture[4]; for ( j = 0; j < iSlices; j++) { GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta; GLfloat stheta = (GLfloat)(-sin(theta)); GLfloat ctheta = (GLfloat)(cos(theta)); GLfloat x = stheta * srho; GLfloat y = ctheta * srho; GLfloat z = crho; vTexture[0][0] = s; vTexture[0][1] = t; vNormal[0][0] = x; vNormal[0][1] = y; vNormal[0][2] = z; vVertex[0][0] = x * fRadius; vVertex[0][1] = y * fRadius; vVertex[0][2] = z * fRadius; x = stheta * srhodrho; y = ctheta * srhodrho; z = crhodrho; vTexture[1][0] = s; vTexture[1][1] = t - dt; vNormal[1][0] = x; vNormal[1][1] = y; vNormal[1][2] = z; vVertex[1][0] = x * fRadius; vVertex[1][1] = y * fRadius; vVertex[1][2] = z * fRadius; theta = ((j+1) == iSlices) ? 0.0f : (j+1) * dtheta; stheta = (GLfloat)(-sin(theta)); ctheta = (GLfloat)(cos(theta)); x = stheta * srho; y = ctheta * srho; z = crho; s += ds; vTexture[2][0] = s; vTexture[2][1] = t; vNormal[2][0] = x; vNormal[2][1] = y; vNormal[2][2] = z; vVertex[2][0] = x * fRadius; vVertex[2][1] = y * fRadius; vVertex[2][2] = z * fRadius; x = stheta * srhodrho; y = ctheta * srhodrho; z = crhodrho; vTexture[3][0] = s; vTexture[3][1] = t - dt; vNormal[3][0] = x; vNormal[3][1] = y; vNormal[3][2] = z; vVertex[3][0] = x * fRadius; vVertex[3][1] = y * fRadius; vVertex[3][2] = z * fRadius; sphereBatch.AddTriangle(vVertex, vNormal, vTexture); // Rearrange for next triangle memcpy(vVertex[0], vVertex[1], sizeof(M3DVector3f)); memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f)); memcpy(vTexture[0], vTexture[1], sizeof(M3DVector2f)); memcpy(vVertex[1], vVertex[3], sizeof(M3DVector3f)); memcpy(vNormal[1], vNormal[3], sizeof(M3DVector3f)); memcpy(vTexture[1], vTexture[3], sizeof(M3DVector2f)); sphereBatch.AddTriangle(vVertex, vNormal, vTexture); } t -= dt; } sphereBatch.End(); }
// Draw a torus (doughnut) at z = fZVal... torus is in xy plane void gltMakeTorus(GLTriangleBatch& torusBatch, GLfloat majorRadius, GLfloat minorRadius, GLint numMajor, GLint numMinor) { double majorStep = 2.0f*M3D_PI / numMajor; double minorStep = 2.0f*M3D_PI / numMinor; int i, j; torusBatch.BeginMesh(numMajor * (numMinor+1) * 6); for (i=0; i<numMajor; ++i) { double a0 = i * majorStep; double a1 = a0 + majorStep; GLfloat x0 = (GLfloat) cos(a0); GLfloat y0 = (GLfloat) sin(a0); GLfloat x1 = (GLfloat) cos(a1); GLfloat y1 = (GLfloat) sin(a1); M3DVector3f vVertex[4]; M3DVector3f vNormal[4]; M3DVector2f vTexture[4]; for (j=0; j<=numMinor; ++j) { double b = j * minorStep; GLfloat c = (GLfloat) cos(b); GLfloat r = minorRadius * c + majorRadius; GLfloat z = minorRadius * (GLfloat) sin(b); // First point vTexture[0][0] = (float)(i)/(float)(numMajor); vTexture[0][1] = (float)(j)/(float)(numMinor); vNormal[0][0] = x0*c; vNormal[0][1] = y0*c; vNormal[0][2] = z/minorRadius; m3dNormalizeVector3(vNormal[0]); vVertex[0][0] = x0 * r; vVertex[0][1] = y0 * r; vVertex[0][2] = z; // Second point vTexture[1][0] = (float)(i+1)/(float)(numMajor); vTexture[1][1] = (float)(j)/(float)(numMinor); vNormal[1][0] = x1*c; vNormal[1][1] = y1*c; vNormal[1][2] = z/minorRadius; m3dNormalizeVector3(vNormal[1]); vVertex[1][0] = x1*r; vVertex[1][1] = y1*r; vVertex[1][2] = z; // Next one over b = (j+1) * minorStep; c = (GLfloat) cos(b); r = minorRadius * c + majorRadius; z = minorRadius * (GLfloat) sin(b); // Third (based on first) vTexture[2][0] = (float)(i)/(float)(numMajor); vTexture[2][1] = (float)(j+1)/(float)(numMinor); vNormal[2][0] = x0*c; vNormal[2][1] = y0*c; vNormal[2][2] = z/minorRadius; m3dNormalizeVector3(vNormal[2]); vVertex[2][0] = x0 * r; vVertex[2][1] = y0 * r; vVertex[2][2] = z; // Fourth (based on second) vTexture[3][0] = (float)(i+1)/(float)(numMajor); vTexture[3][1] = (float)(j+1)/(float)(numMinor); vNormal[3][0] = x1*c; vNormal[3][1] = y1*c; vNormal[3][2] = z/minorRadius; m3dNormalizeVector3(vNormal[3]); vVertex[3][0] = x1*r; vVertex[3][1] = y1*r; vVertex[3][2] = z; torusBatch.AddTriangle(vVertex, vNormal, vTexture); // Rearrange for next triangle memcpy(vVertex[0], vVertex[1], sizeof(M3DVector3f)); memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f)); memcpy(vTexture[0], vTexture[1], sizeof(M3DVector2f)); memcpy(vVertex[1], vVertex[3], sizeof(M3DVector3f)); memcpy(vNormal[1], vNormal[3], sizeof(M3DVector3f)); memcpy(vTexture[1], vTexture[3], sizeof(M3DVector2f)); torusBatch.AddTriangle(vVertex, vNormal, vTexture); } } torusBatch.End(); }
// called to draw dancing objects static void DrawSongAndDance(GLfloat yRot) { static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; static GLfloat vLightPos[] = { 0.0f, 3.0f, 0.0f, 1.0f }; // get the light position in eye space M3DMatrix44f mCamera; modelViewMatrix.GetMatrix(mCamera); M3DVector4f vLightTransformed; m3dTransformVector4(vLightTransformed, vLightPos, mCamera); // draw the light source modelViewMatrix.push(); modelViewMatrix.moveTo(vLightPos); shaderManager.useStockShader(GLT_SHADER_FLAT, transformPipeline.GetMVPMatrix(), vWhite); sphereBatch.draw(); modelViewMatrix.pop(); glBindTexture(GL_TEXTURE_2D, uiTextures[2]); for (int i = 0; i < NUM_SPHERES; ++i) { modelViewMatrix.push(); modelViewMatrix.MultMatrix(spheres[i]); shaderManager.useStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeline.GetProjectionMatrix(), vLightTransformed, vWhite, 0); sphereBatch.draw(); modelViewMatrix.pop(); } // song and dance modelViewMatrix.moveTo(0.0f, 0.2f, -2.5f); modelViewMatrix.push(); // save the translated origin modelViewMatrix.rotateTo(yRot, 0.0f, 1.0f, 0.0f); // draw stuff relative to the camera glBindTexture(GL_TEXTURE_2D, uiTextures[1]); shaderManager.useStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeline.GetProjectionMatrix(), vLightTransformed, vWhite, 0); torusBatch.draw(); modelViewMatrix.pop(); // erased the rotate modelViewMatrix.rotateTo(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.moveTo(0.8f, 0.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, uiTextures[2]); shaderManager.useStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeline.GetProjectionMatrix(), vLightTransformed, vWhite, 0); sphereBatch.draw(); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { static CStopWatch animationTimer; float yRot = animationTimer.GetElapsedSeconds() * 60.0f; M3DVector3f vCameraPos; M3DVector3f vCameraForward; M3DVector3f vMirrorPos; M3DVector3f vMirrorForward; cameraFrame.GetOrigin(vCameraPos); cameraFrame.GetForwardVector(vCameraForward); // Set position of mirror frame (camera) vMirrorPos[0] = 0.0; vMirrorPos[1] = 0.1f; vMirrorPos[2] = -6.0f; // view pos is actually behind mirror mirrorFrame.SetOrigin(vMirrorPos); // Calculate direction of mirror frame (camera) // Because the position of the mirror is known relative to the origin // find the direction vector by adding the mirror offset to the vector // of the viewer-origin vMirrorForward[0] = vCameraPos[0]; vMirrorForward[1] = vCameraPos[1]; vMirrorForward[2] = (vCameraPos[2] + 5); m3dNormalizeVector3(vMirrorForward); mirrorFrame.SetForwardVector(vMirrorForward); // first render from the mirrors perspective glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboName); glDrawBuffers(1, fboBuffs); glViewport(0, 0, mirrorTexWidth, mirrorTexHeight); // Draw scene from the perspective of the mirror camera modelViewMatrix.PushMatrix(); M3DMatrix44f mMirrorView; mirrorFrame.GetCameraMatrix(mMirrorView); modelViewMatrix.MultMatrix(mMirrorView); // Flip the mirror camera horizontally for the reflection modelViewMatrix.Scale(-1.0f, 1.0f, 1.0f); glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vWhite, 0); floorBatch.Draw(); DrawWorld(yRot); // Now draw a cylinder representing the viewer M3DVector4f vLightTransformed; modelViewMatrix.GetMatrix(mMirrorView); m3dTransformVector4(vLightTransformed, vLightPos, mMirrorView); modelViewMatrix.Translate(vCameraPos[0],vCameraPos[1]-0.8f,vCameraPos[2]-1.0f); modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeline.GetProjectionMatrix(), vLightTransformed, vBlue, 0); cylinderBatch.Draw(); modelViewMatrix.PopMatrix(); // Reset FBO. Draw world again from the real cameras perspective glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffers(1, windowBuff); glViewport(0, 0, screenWidth, screenHeight); modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vWhite, 0); floorBatch.Draw(); DrawWorld(yRot); // Now draw the mirror surfaces modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, -0.4f, -5.0f); if(vCameraPos[2] > -5.0) { glBindTexture(GL_TEXTURE_2D, mirrorTexture); // Reflection shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), 0); } else { // If the camera is behind the mirror, just draw black shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack); } mirrorBatch.Draw(); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vGrey); mirrorBorderBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); }
// Called to draw scene void RenderScene(void) { // Color values static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; static GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; // Time Based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix) modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); // Draw the ground shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); for(int i = 0; i < NUM_SPHERES; i++) { modelViewMatrix.PushMatrix(); modelViewMatrix.MultMatrix(spheres[i]); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); } // Draw the spinning Torus modelViewMatrix.Translate(0.0f, 0.0f, -2.5f); // Save the Translation modelViewMatrix.PushMatrix(); // Apply a rotation and draw the torus modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor); torusBatch.Draw(); modelViewMatrix.PopMatrix(); // "Erase" the Rotation from before // Apply another rotation, followed by a translation, then draw the sphere modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor); sphereBatch.Draw(); // Restore the previous modleview matrix (the identity matrix) modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); }
/** * @brief Called to draw scene */ void RenderScene() { // Color values static GLfloat vSunColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; static GLfloat vEarthColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; static GLfloat vMoonColor[] = { 1.0f, 1.0f, 0.0f, 1.0f }; static GLfloat vFloorColor[] = { 0.0f, 0.64f, 0.0f, 1.0f}; // Time Based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix) modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); // Transform the light position into eye coordinates M3DVector4f vLightPos = { 0.0f, 10.0f, 5.0f, 1.0f }; M3DVector4f vLightEyePos; m3dTransformVector4(vLightEyePos, vLightPos, mCamera); // Draw the floor shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); // Draw the spinning Sun modelViewMatrix.Translate(0.0f, 0.0f, -3.5f); // Save the Translation modelViewMatrix.PushMatrix(); // Apply a rotation and draw the Sun modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vSunColor); sunSphereBatch.Draw(); modelViewMatrix.PopMatrix(); // "Erase" the Rotation from before // Apply another rotation, followed by a translation, then draw the Earth modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vEarthColor); earthSphereBatch.Draw(); // Apply another rotation, followed by a translation, then draw the Moon modelViewMatrix.Rotate(yRot * -4.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.4f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vMoonColor); moonSphereBatch.Draw(); // Restore the previous modleview matrix (the identity matrix) modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); }
void DrawSongAndDance(GLfloat yRot) // Called to draw dancing objects { static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; static GLfloat vLightPos[] = { 0.0f, 3.0f, 0.0f, 1.0f }; // Get the light position in eye space M3DVector4f vLightTransformed; M3DMatrix44f mCamera; modelViewMatrix.GetMatrix(mCamera); m3dTransformVector4(vLightTransformed, vLightPos, mCamera); // Draw the light source modelViewMatrix.PushMatrix(); modelViewMatrix.Translatev(vLightPos); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeLine.GetModelViewProjectionMatrix(), vWhite); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); glBindTexture(GL_TEXTURE_2D, uiTextures[2]); for (int i = 0; i < NUM_SPHERES; i++) { modelViewMatrix.PushMatrix(); modelViewMatrix.MultMatrix(spheres[i]); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeLine.GetProjectionMatrix(), vLightTransformed, vWhite, 0); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); } // Song and dance modelViewMatrix.Translate(0.0f, 0.2f, -2.5f); modelViewMatrix.PushMatrix(); // Saves the translated origin modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); // Draw stuff relative to the camera glBindTexture(GL_TEXTURE_2D, uiTextures[1]); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeLine.GetProjectionMatrix(), vLightTransformed, vWhite, 0); torusBatch.Draw(); modelViewMatrix.PopMatrix(); // Erased the rotate modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, uiTextures[2]); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), transformPipeLine.GetProjectionMatrix(), vLightTransformed, vWhite, 0); sphereBatch.Draw(); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { // Bind the FBO with multisample buffers glBindFramebuffer(GL_DRAW_FRAMEBUFFER, msFBO); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // User selected order independant transparency if (mode == USER_OIT) { // Use OIT, setup sample masks glSampleMaski(0, 0x01); glEnable(GL_SAMPLE_MASK); // Prevent depth test from culling covered surfaces glDepthFunc(GL_ALWAYS); } modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, -0.4f, -4.0f); modelViewMatrix.Rotate(worldAngle, 0.0, 1.0, 0.0); // Draw the background and disk to the first sample modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, 3.0f, 0.0f); modelViewMatrix.Rotate(90.0, 1.0, 0.0, 0.0); modelViewMatrix.Rotate(90.0, 0.0, 0.0, 1.0); glBindTexture(GL_TEXTURE_2D, textures[1]); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), 0); bckgrndCylBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.Translate(0.0f, -0.3f, 0.0f); modelViewMatrix.PushMatrix(); modelViewMatrix.Rotate(90.0, 1.0, 0.0, 0.0); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vGrey); diskBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.Translate(0.0f, 0.1f, 0.0f); // User selected blending if (mode == USER_BLEND) { // Setup blend state glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); switch (blendMode) { case 1: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; case 2: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA); break; case 3: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); break; case 4: glBlendFunc(GL_SRC_ALPHA, GL_ONE); break; case 5: glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR); break; case 6: glBlendFuncSeparate(GL_SRC_ALPHA, GL_DST_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; case 7: glBlendFuncSeparate(GL_SRC_COLOR, GL_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; default: glDisable(GL_BLEND); } } // Now draw the glass pieces DrawWorld(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Clean up all state glDepthFunc(GL_LEQUAL); glDisable(GL_BLEND); glDisable(GL_SAMPLE_MASK); glSampleMaski(0, 0xffffffff); // Resolve multisample buffer projectionMatrix.PushMatrix(); projectionMatrix.LoadMatrix(orthoMatrix); modelViewMatrix.PushMatrix(); modelViewMatrix.LoadIdentity(); // Setup and Clear the default framebuffer glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glViewport(0, 0, screenWidth, screenHeight); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (mode == USER_OIT) SetupOITResolveProg(); else if (mode == USER_BLEND) SetupResolveProg(); // Draw a full-size quad to resolve the multisample surfaces screenQuad.Draw(); modelViewMatrix.PopMatrix(); projectionMatrix.PopMatrix(); // Reset texture state glEnable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); }