void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glEnable( GL_DEPTH_TEST ) ; glUseProgram(shader); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); float angle = timer.GetElapsedSeconds(); M3DVector3f eye = {cos(angle)*4.0f,sin(angle)*4.0,sin(angle)*4.0f}; M3DVector3f at = {0,0,0}; M3DVector3f up = {0.0f,0.0f,1.0f}; GLFrame cameraFrame; LookAt(cameraFrame, eye, at, up); cameraFrame.GetCameraMatrix(cameraMatrix); m3dMatrixMultiply44(matrix, viewFrustum.GetProjectionMatrix(), cameraMatrix); M3DMatrix44f tMatrix; m3dTranslationMatrix44(tMatrix, 0, 0, 0); m3dMatrixMultiply44(matrix, tMatrix, matrix); if((sin(angle) < 0 && sin(angle-0.003) > 0) || (sin(angle) > 0 && sin(angle-0.003) < 0)) { debugMatrix44(matrix); } glUniformMatrix4fv(MVPMatrixLocation,1,GL_FALSE,matrix); // drawGrid(0.1); drawPyramid(); glutSwapBuffers(); glutPostRedisplay(); }
void GetViewProjectionMatrix(float *viewProjectionMatrix) { M3DMatrix44f viewMatrix; cameraFrame.GetCameraMatrix(viewMatrix); m3dMatrixMultiply44(viewProjectionMatrix, viewFrustum.GetProjectionMatrix(), viewMatrix); }
void Transform::Scale( float xScale, float yScale, float zScale ) { M3DMatrix44f _scmatrix; m3dScaleMatrix44( _scmatrix, xScale, yScale, zScale ); M3DMatrix44f _source; m3dCopyMatrix44(_source, _matrix); m3dMatrixMultiply44(_matrix, _source, _scmatrix); }
void Transform::Rotate( float angle, float x, float y, float z ) { M3DMatrix44f _rotmatrix; m3dRotationMatrix44( _rotmatrix, angle, x, y, z ); M3DMatrix44f _source; m3dCopyMatrix44(_source, _matrix); m3dMatrixMultiply44(_matrix, _source, _rotmatrix); }
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函数之前的变换管线还是不清晰 }
void RenderScene(void) { M3DMatrix44f CameraMatrix; M3DMatrix44f ViewProjectionMatrix; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glUseProgram(shader); cameraFrame.GetCameraMatrix(CameraMatrix); frame.GetMatrix(ProjectionMatrix); m3dMatrixMultiply44(ViewProjectionMatrix,ProjectionMatrix,CameraMatrix); glUniformMatrix4fv(MVPMatrixLocation,1,GL_FALSE,ViewProjectionMatrix); glBegin(GL_QUADS); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 0.5, 0.5, 0.5); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glEnd(); glBegin(GL_TRIANGLES); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 1.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 2.0); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 1.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 2.0); glVertex3f(-1.0, -1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 2.0); glVertex3f(1.0, -1.0, 0.0); glVertex3f(1.0, 1.0, 0.0); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 2.0); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, 1.0, 0.0); glEnd(); glutSwapBuffers(); //cameraFrame.RotateLocalZ(0.01); //Obrót kamery glutPostRedisplay(); }
void renderScene(void) { float angle = timer.GetElapsedSeconds() * 3.14f; location[0] = 6.0f * cos(angle); location[1] = 6.0f * sin(angle); location[2] = 5.0f; lookAt(cameraFrame, location, target, upDir); cameraFrame.GetCameraMatrix(cameraMatrix); m3dMatrixMultiply44(MVPMatrix, viewFrustum.GetProjectionMatrix(), cameraMatrix); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glUseProgram(shader); glUniformMatrix4fv(MVPMatrixLocation, 1, GL_FALSE, MVPMatrix); glBegin(GL_QUADS); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glEnd(); glBegin(GL_TRIANGLES); glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 1.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 2.0f); //-- glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 1.0f, 1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 2.0f); //-- glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 0.0f, 1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 2.0f); glVertex3f(-1.0f, 1.0f, 0.0f); //-- glVertexAttrib3f(GLT_ATTRIBUTE_COLOR, 0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 2.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glEnd(); glutSwapBuffers(); glutPostRedisplay(); }
void MouseMoveEvent(int x, int y) { if (isStartTrackBall) { GLfloat theta; M3DVector3f p1, p2, n; int width = glutGet(GLUT_WINDOW_WIDTH); int height = glutGet(GLUT_WINDOW_HEIGHT); MousePtToSphereVec(p1, mMouseX, mMouseY, width, height); MousePtToSphereVec(p2, x, y, width, height); mMouseX = x; mMouseY = y; m3dNormalizeVector3(p1); m3dNormalizeVector3(p2); theta = acos(m3dDotProduct3(p1, p2)) ; //theta = m3dGetAngleBetweenVectors3(p1, p2); m3dCrossProduct3(n, p1, p2); m3dNormalizeVector3(n); M3DMatrix44f tempRotation; m3dLoadIdentity44(tempRotation); GLfloat dis = m3dGetDistance3(p1, p2); if (dis != 0.0f) { m3dRotationMatrix44(tempRotation, theta, m3dGetVectorX(n), m3dGetVectorY(n), m3dGetVectorZ(n)); } m3dMatrixMultiply44(mRotation, tempRotation, mRotation); glutPostRedisplay(); } }
// Called to regenerate the shadow map void RegenerateShadowMap(void) { GLfloat lightToSceneDistance, nearPlane, fieldOfView; GLfloat lightModelview[16], lightProjection[16]; GLfloat sceneBoundingRadius = 95.0f; // based on objects in scene // Save the depth precision for where it's useful lightToSceneDistance = sqrt(lightPos[0] * lightPos[0] + lightPos[1] * lightPos[1] + lightPos[2] * lightPos[2]); nearPlane = lightToSceneDistance - sceneBoundingRadius; // Keep the scene filling the depth texture fieldOfView = (GLfloat)m3dRadToDeg(2.0f * atan(sceneBoundingRadius / lightToSceneDistance)); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(fieldOfView, 1.0f, nearPlane, nearPlane + (2.0f * sceneBoundingRadius)); glGetFloatv(GL_PROJECTION_MATRIX, lightProjection); // Switch to light's point of view glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(lightPos[0], lightPos[1], lightPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glGetFloatv(GL_MODELVIEW_MATRIX, lightModelview); glViewport(0, 0, shadowWidth, shadowHeight); // Clear the depth buffer only glClear(GL_DEPTH_BUFFER_BIT); // All we care about here is resulting depth values glShadeModel(GL_FLAT); glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); glDisable(GL_NORMALIZE); glColorMask(0, 0, 0, 0); // Overcome imprecision glEnable(GL_POLYGON_OFFSET_FILL); // Draw objects in the scene except base plane // which never shadows anything DrawModels(GL_FALSE); // Copy depth values into depth texture glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, shadowWidth, shadowHeight, 0); // Restore normal drawing state glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glEnable(GL_NORMALIZE); glColorMask(1, 1, 1, 1); glDisable(GL_POLYGON_OFFSET_FILL); // Set up texture matrix for shadow map projection, // which will be rolled into the eye linear // texture coordinate generation plane equations M3DMatrix44f tempMatrix; m3dLoadIdentity44(tempMatrix); m3dTranslateMatrix44(tempMatrix, 0.5f, 0.5f, 0.5f); m3dScaleMatrix44(tempMatrix, 0.5f, 0.5f, 0.5f); m3dMatrixMultiply44(textureMatrix, tempMatrix, lightProjection); m3dMatrixMultiply44(tempMatrix, textureMatrix, lightModelview); // transpose to get the s, t, r, and q rows for plane equations m3dTransposeMatrix44(textureMatrix, tempMatrix); }