/////////////////////////////////////////////////////////////////////////////// // Creates a 4x4 rotation matrix, takes radians NOT degrees void m3dRotationMatrix44(M3DMatrix44d m, double angle, double x, double y, double z) { double mag, s, c; double xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c; s = sin(angle); c = cos(angle); mag = sqrt( x*x + y*y + z*z ); // Identity matrix if (mag == 0.0) { m3dLoadIdentity44(m); return; } // Rotation matrix is normalized x /= mag; y /= mag; z /= mag; #define M(row,col) m[col*4+row] xx = x * x; yy = y * y; zz = z * z; xy = x * y; yz = y * z; zx = z * x; xs = x * s; ys = y * s; zs = z * s; one_c = 1.0f - c; M(0,0) = (one_c * xx) + c; M(0,1) = (one_c * xy) - zs; M(0,2) = (one_c * zx) + ys; M(0,3) = 0.0; M(1,0) = (one_c * xy) + zs; M(1,1) = (one_c * yy) + c; M(1,2) = (one_c * yz) - xs; M(1,3) = 0.0; M(2,0) = (one_c * zx) - ys; M(2,1) = (one_c * yz) + xs; M(2,2) = (one_c * zz) + c; M(2,3) = 0.0; M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0; #undef M }
/////////////////////////////////////////////////////////////////////////////// // Make a orthographic projection matrix void m3dMakeOrthographicMatrix(M3DMatrix44f mProjection, float xMin, float xMax, float yMin, float yMax, float zMin, float zMax) { m3dLoadIdentity44(mProjection); mProjection[0] = 2.0f / (xMax - xMin); mProjection[5] = 2.0f / (yMax - yMin); mProjection[10] = -2.0f / (zMax - zMin); mProjection[12] = -((xMax + xMin)/(xMax - xMin)); mProjection[13] = -((yMax + yMin)/(yMax - yMin)); mProjection[14] = -((zMax + zMin)/(zMax - zMin)); mProjection[15] = 1.0f; }
// Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); M3DMatrix44f identityMatrix; m3dLoadIdentity44(identityMatrix); shaderManager.UseStockShader(GLT_SHADER_SHADED, identityMatrix); triangleBatch.Draw(); // Perform the buffer swap to display back buffer glutSwapBuffers(); //glutPostRedisplay(); }
//////////////////////////////////////////////////////////////////////////////////////////// // Create a projection matrix // Similiar to the old gluPerspective... fov is in radians btw... void m3dMakePerspectiveMatrix(M3DMatrix44f mProjection, float fFov, float fAspect, float zMin, float zMax) { m3dLoadIdentity44(mProjection); // Fastest way to get most valid values already in place float yMax = zMin * tanf(fFov * 0.5f); float yMin = -yMax; float xMin = yMin * fAspect; float xMax = -xMin; mProjection[0] = (2.0f * zMin) / (xMax - xMin); mProjection[5] = (2.0f * zMin) / (yMax - yMin); mProjection[8] = (xMax + xMin) / (xMax - xMin); mProjection[9] = (yMax + yMin) / (yMax - yMin); mProjection[10] = -((zMax + zMin) / (zMax - zMin)); mProjection[11] = -1.0f; mProjection[14] = -((2.0f * (zMax*zMin))/(zMax - zMin)); mProjection[15] = 0.0f; }
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(); } }
void SetupRC() { glClearColor(0, 0, 0, 1); shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); gltMakeSphere(sphereBatch, 0.4, 20, 20); gltMakeCube(cubeBatch, 0.3); // Load up a triangle GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f }; GLfloat vColors [] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f }; triangleBatch.Begin(GL_TRIANGLES, 3); triangleBatch.CopyVertexData3f(vVerts); triangleBatch.CopyColorData4f(vColors); triangleBatch.End(); mWcgCube.SetupCube(); /*transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);*/ cameraFrame.MoveForward(-3.0f); m3dLoadIdentity44(mRotation); myShader = gltLoadShaderPairWithAttributes("shader.vp", "shader.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_COLOR, "vColor"); locMVP = glGetUniformLocation(myShader, "mvpMatrix"); }
// 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); }
/** \brief Transform Node is initialized with a Name and the identity matrix * \param Name of the Node */ Transform::Transform(const string& Name) : CompositeNode(Name) { m3dLoadIdentity44( _matrix ); }
void Transform::Reset() { m3dLoadIdentity44( _matrix ); }
/** \brief Transform Node is initialized with the Identity Matrix */ Transform::Transform() : CompositeNode() { m3dLoadIdentity44( _matrix ); }