Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 30th, 2012  |  syntax: C++  |  size: 8.64 KB  |  views: 65  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Pyramid.cpp
  2. // OpenGL SuperBible, Chapter 5
  3. // Demonstrates Texture mapping a pyramid
  4. // Program by Richard S. Wright Jr.
  5.  
  6. #include <GLTools.h>    // OpenGL toolkit
  7. #include <GLMatrixStack.h>
  8. #include <GLFrame.h>
  9. #include <GLFrustum.h>
  10. #include <GLBatch.h>
  11. #include <GLGeometryTransform.h>
  12.  
  13. #include <math.h>
  14. #ifdef __APPLE__
  15. #include <glut/glut.h>
  16. #else
  17. #define FREEGLUT_STATIC
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////////
  22. // An assortment of needed classes
  23. GLShaderManager         shaderManager;
  24. GLMatrixStack           modelViewMatrix;
  25. GLMatrixStack           projectionMatrix;
  26. GLFrame                         cameraFrame;
  27. GLFrame             objectFrame;
  28. GLFrustum                       viewFrustum;
  29.  
  30. GLBatch             pyramidBatch;
  31.  
  32. GLuint              textureID;
  33.  
  34. GLGeometryTransform     transformPipeline;
  35. M3DMatrix44f            shadowMatrix;
  36.  
  37.  
  38. void MakePyramid(GLBatch& pyramidBatch)
  39.     {
  40.         pyramidBatch.Begin(GL_TRIANGLES, 18, 1);
  41.    
  42.         // Bottom of pyramid
  43.         pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
  44.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
  45.         pyramidBatch.Vertex3f(-1.0f, -1.0f, -1.0f);
  46.    
  47.         pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
  48.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
  49.         pyramidBatch.Vertex3f(1.0f, -1.0f, -1.0f);
  50.    
  51.         pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
  52.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
  53.         pyramidBatch.Vertex3f(1.0f, -1.0f, 1.0f);
  54.    
  55.         pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
  56.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
  57.         pyramidBatch.Vertex3f(-1.0f, -1.0f, 1.0f);
  58.    
  59.         pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
  60.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
  61.         pyramidBatch.Vertex3f(-1.0f, -1.0f, -1.0f);
  62.    
  63.         pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
  64.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
  65.         pyramidBatch.Vertex3f(1.0f, -1.0f, 1.0f);
  66.    
  67.        
  68.         M3DVector3f vApex = { 0.0f, 1.0f, 0.0f };
  69.         M3DVector3f vFrontLeft = { -1.0f, -1.0f, 1.0f };
  70.         M3DVector3f vFrontRight = { 1.0f, -1.0f, 1.0f };
  71.         M3DVector3f vBackLeft = { -1.0f, -1.0f, -1.0f };
  72.         M3DVector3f vBackRight = { 1.0f, -1.0f, -1.0f };
  73.         M3DVector3f n;
  74.        
  75.         // Front of Pyramid
  76.         m3dFindNormal(n, vApex, vFrontLeft, vFrontRight);
  77.         pyramidBatch.Normal3fv(n);
  78.         pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
  79.         pyramidBatch.Vertex3fv(vApex);          // Apex
  80.    
  81.         pyramidBatch.Normal3fv(n);
  82.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
  83.         pyramidBatch.Vertex3fv(vFrontLeft);             // Front left corner
  84.    
  85.         pyramidBatch.Normal3fv(n);
  86.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
  87.         pyramidBatch.Vertex3fv(vFrontRight);            // Front right corner
  88.    
  89.    
  90.         m3dFindNormal(n, vApex, vBackLeft, vFrontLeft);
  91.         pyramidBatch.Normal3fv(n);
  92.         pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
  93.         pyramidBatch.Vertex3fv(vApex);          // Apex
  94.    
  95.         pyramidBatch.Normal3fv(n);
  96.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
  97.         pyramidBatch.Vertex3fv(vBackLeft);              // Back left corner
  98.        
  99.         pyramidBatch.Normal3fv(n);
  100.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
  101.         pyramidBatch.Vertex3fv(vFrontLeft);             // Front left corner
  102.    
  103.         m3dFindNormal(n, vApex, vFrontRight, vBackRight);
  104.         pyramidBatch.Normal3fv(n);
  105.         pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
  106.         pyramidBatch.Vertex3fv(vApex);                          // Apex
  107.    
  108.         pyramidBatch.Normal3fv(n);
  109.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
  110.         pyramidBatch.Vertex3fv(vFrontRight);            // Front right corner
  111.    
  112.         pyramidBatch.Normal3fv(n);
  113.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
  114.         pyramidBatch.Vertex3fv(vBackRight);                     // Back right cornder
  115.    
  116.    
  117.         m3dFindNormal(n, vApex, vBackRight, vBackLeft);
  118.         pyramidBatch.Normal3fv(n);
  119.         pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
  120.         pyramidBatch.Vertex3fv(vApex);          // Apex
  121.    
  122.         pyramidBatch.Normal3fv(n);
  123.         pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
  124.         pyramidBatch.Vertex3fv(vBackRight);             // Back right cornder
  125.  
  126.         pyramidBatch.Normal3fv(n);
  127.         pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
  128.         pyramidBatch.Vertex3fv(vBackLeft);              // Back left corner
  129.  
  130.         pyramidBatch.End();
  131.         }
  132.  
  133. // Load a TGA as a 2D Texture. Completely initialize the state
  134. bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode)
  135. {
  136.         GLbyte *pBits;
  137.         int nWidth, nHeight, nComponents;
  138.         GLenum eFormat;
  139.        
  140.         // Read the texture bits
  141.         pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);
  142.         if(pBits == NULL)
  143.                 return false;
  144.        
  145.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
  146.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
  147.        
  148.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  149.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  150.    
  151.         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  152.         glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0,
  153.                                  eFormat, GL_UNSIGNED_BYTE, pBits);
  154.        
  155.     free(pBits);
  156.    
  157.     if(minFilter == GL_LINEAR_MIPMAP_LINEAR ||
  158.        minFilter == GL_LINEAR_MIPMAP_NEAREST ||
  159.        minFilter == GL_NEAREST_MIPMAP_LINEAR ||
  160.        minFilter == GL_NEAREST_MIPMAP_NEAREST)
  161.         glGenerateMipmap(GL_TEXTURE_2D);
  162.    
  163.         return true;
  164. }
  165.  
  166.  
  167. ///////////////////////////////////////////////////////////////////////////////
  168. // This function does any needed initialization on the rendering context.
  169. // This is the first opportunity to do any OpenGL related tasks.
  170. void SetupRC()
  171.         {
  172.     // Black background
  173.     glClearColor(0.7f, 0.7f, 0.7f, 1.0f );
  174.  
  175.         shaderManager.InitializeStockShaders();
  176.  
  177.         glEnable(GL_DEPTH_TEST);
  178.    
  179.     glGenTextures(1, &textureID);
  180.     glBindTexture(GL_TEXTURE_2D, textureID);
  181.     LoadTGATexture("stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
  182.    
  183.     MakePyramid(pyramidBatch);
  184.  
  185.         cameraFrame.MoveForward(-7.0f);
  186.     }
  187.  
  188. ///////////////////////////////////////////////////////////////////////////////
  189. // Cleanup... such as deleting texture objects
  190. void ShutdownRC(void)
  191.     {
  192.     glDeleteTextures(1, &textureID);
  193.     }
  194.  
  195. ///////////////////////////////////////////////////////////////////////////////
  196. // Called to draw scene
  197. void RenderScene(void)
  198.         {    
  199.     static GLfloat vLightPos [] = { 1.0f, 1.0f, 0.0f };
  200.     static GLfloat vWhite [] = { 1.0f, 1.0f, 1.0f, 1.0f };
  201.    
  202.         // Clear the window with current clearing color
  203.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  204.  
  205.         modelViewMatrix.PushMatrix();
  206.                 M3DMatrix44f mCamera;
  207.                 cameraFrame.GetCameraMatrix(mCamera);
  208.                 modelViewMatrix.MultMatrix(mCamera);
  209.  
  210.         M3DMatrix44f mObjectFrame;
  211.         objectFrame.GetMatrix(mObjectFrame);
  212.         modelViewMatrix.MultMatrix(mObjectFrame);
  213.  
  214.         glBindTexture(GL_TEXTURE_2D, textureID);
  215.         shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
  216.                                      transformPipeline.GetModelViewMatrix(),
  217.                                      transformPipeline.GetProjectionMatrix(),
  218.                                      vLightPos, vWhite, 0);
  219.  
  220.         pyramidBatch.Draw();
  221.  
  222.                
  223.         modelViewMatrix.PopMatrix();
  224.  
  225.         // Flush drawing commands
  226.         glutSwapBuffers();
  227.     }
  228.  
  229.  
  230. // Respond to arrow keys by moving the camera frame of reference
  231. void SpecialKeys(int key, int x, int y)
  232.     {
  233.         if(key == GLUT_KEY_UP)
  234.                 objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
  235.    
  236.         if(key == GLUT_KEY_DOWN)
  237.                 objectFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);
  238.        
  239.         if(key == GLUT_KEY_LEFT)
  240.                 objectFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
  241.    
  242.         if(key == GLUT_KEY_RIGHT)
  243.                 objectFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
  244.    
  245.         glutPostRedisplay();
  246.     }
  247.  
  248.  
  249.  
  250.  
  251. ///////////////////////////////////////////////////////////////////////////////
  252. // Window has changed size, or has just been created. In either case, we need
  253. // to use the window dimensions to set the viewport and the projection matrix.
  254. void ChangeSize(int w, int h)
  255.         {
  256.         glViewport(0, 0, w, h);
  257.         viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 500.0f);
  258.         projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
  259.     transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
  260.         }
  261.  
  262. ///////////////////////////////////////////////////////////////////////////////
  263. // Main entry point for GLUT based programs
  264. int main(int argc, char* argv[])
  265.         {
  266.         gltSetWorkingDirectory(argv[0]);
  267.        
  268.         glutInit(&argc, argv);
  269.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
  270.         glutInitWindowSize(800, 600);
  271.         glutCreateWindow("Pyramid");
  272.     glutReshapeFunc(ChangeSize);
  273.     glutSpecialFunc(SpecialKeys);
  274.     glutDisplayFunc(RenderScene);
  275.        
  276.         GLenum err = glewInit();
  277.         if (GLEW_OK != err) {
  278.                 fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
  279.                 return 1;
  280.                 }
  281.        
  282.  
  283.         SetupRC();
  284.  
  285.         glutMainLoop();
  286.    
  287.     ShutdownRC();
  288.    
  289.         return 0;
  290.         }