void mouseCameraFunc(int x, int y) { if (mouseActive) { GLfloat angle = 0.005f; if (x > W_WIDTH/2) { cameraFrame.RotateWorld(-angle*(x-W_WIDTH/2), 0, 0, 1); glutWarpPointer(W_WIDTH/2, W_HEIGHT/2); } else if (x < W_WIDTH/2) { cameraFrame.RotateWorld(angle*(W_WIDTH/2-x), 0, 0, 1); glutWarpPointer(W_WIDTH/2, W_HEIGHT/2); } if (y > W_HEIGHT/2 && pitch < 90) { cameraFrame.RotateLocalX(-angle*(W_HEIGHT/2-y)); glutWarpPointer(W_WIDTH/2, W_HEIGHT/2); pitch = pitch + (-angle*(W_HEIGHT/2-y)); } else if (y < W_HEIGHT/2 && pitch > -90) { cameraFrame.RotateLocalX(angle*(y-W_HEIGHT/2)); glutWarpPointer(W_WIDTH/2, W_HEIGHT/2); pitch = pitch + (angle*(y-W_HEIGHT/2)); } } }
void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_F1) frameCamera.MoveForward(1.0f); if(key == GLUT_KEY_F2) frameCamera.MoveForward(-1.0f); if(key == GLUT_KEY_DOWN) frameCamera.RotateLocalX(0.01f); if(key == GLUT_KEY_UP) frameCamera.RotateLocalX(-0.01f); if(key == GLUT_KEY_LEFT) frameCamera.RotateLocalY(0.01f); if(key == GLUT_KEY_RIGHT) frameCamera.RotateLocalY(-0.01f); /*if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(key > 356.0f) xRot = 0.0f; if(key < -1.0f) xRot = 355.0f; if(key > 356.0f) yRot = 0.0f; if(key < -1.0f) yRot = 355.0f;*/ // Refresh the Window glutPostRedisplay(); }
/////////////////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering context. // This is the first opportunity to do any OpenGL related tasks. void SetupRC() { GLbyte *pBytes; GLint nWidth, nHeight, nComponents; GLenum format; shaderManager.InitializeStockShaders(); // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); glLineWidth(2.5f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); cameraFrame.MoveForward(-15.0f); cameraFrame.MoveUp(6.0f); cameraFrame.RotateLocalX(float(m3dDegToRad(20.0f))); MakeCube(cubeBatch); MakeFloor(floorBatch); // Make top topBlock.Begin(GL_TRIANGLE_FAN, 4, 1); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 0.0f, 0.0f); topBlock.Vertex3f(-1.0f, 1.0f, 1.0f); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 1.0f, 0.0f); topBlock.Vertex3f(1.0f, 1.0f, 1.0f); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 1.0f, 1.0f); topBlock.Vertex3f(1.0f, 1.0f, -1.0f); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 0.0f, 1.0f); topBlock.Vertex3f(-1.0f, 1.0f, -1.0f); topBlock.End(); // Make Front frontBlock.Begin(GL_TRIANGLE_FAN, 4, 1); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 0.0f, 0.0f); frontBlock.Vertex3f(-1.0f, -1.0f, 1.0f); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 1.0f, 0.0f); frontBlock.Vertex3f(1.0f, -1.0f, 1.0f); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 1.0f, 1.0f); frontBlock.Vertex3f(1.0f, 1.0f, 1.0f); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 0.0f, 1.0f); frontBlock.Vertex3f(-1.0f, 1.0f, 1.0f); frontBlock.End(); // Make left leftBlock.Begin(GL_TRIANGLE_FAN, 4, 1); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 0.0f, 0.0f); leftBlock.Vertex3f(-1.0f, -1.0f, -1.0f); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 1.0f, 0.0f); leftBlock.Vertex3f(-1.0f, -1.0f, 1.0f); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 1.0f, 1.0f); leftBlock.Vertex3f(-1.0f, 1.0f, 1.0f); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 0.0f, 1.0f); leftBlock.Vertex3f(-1.0f, 1.0f, -1.0f); leftBlock.End(); // Create shadow projection matrix GLfloat floorPlane[] = { 0.0f, 1.0f, 0.0f, 1.0f}; m3dMakePlanarShadowMatrix(shadowMatrix, floorPlane, vLightPos); // Load up four textures glGenTextures(4, textures); // Wood floor pBytes = gltReadTGABits("floor.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // One of the block faces pBytes = gltReadTGABits("Block4.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // Another block face pBytes = gltReadTGABits("block5.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[2]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // Yet another block face pBytes = gltReadTGABits("block6.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[3]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); }