Advertisement
Guest User

test3d.cpp

a guest
Aug 3rd, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 KB | None | 0 0
  1. // Include common headers
  2. #include "common_header.h"
  3.  
  4. // Include shader class
  5. #include "shaders.h"
  6.  
  7. // Include GLEW
  8. #include <GL/glew.h>
  9.  
  10. // Include GLFW
  11. #include <GL/glfw.h>
  12.  
  13. // Include GLM
  14. #include <glm/glm.hpp>
  15. #include <glm/gtc/matrix_transform.hpp>
  16.  
  17. //include glew
  18. #include <gl/glew.h>
  19.  
  20. using namespace glm;
  21. using namespace std;
  22.  
  23. //shaders
  24. CShader simpleFrag, simpleVert;
  25. CShaderProgram SSP;
  26.  
  27. bool LoadShaders(void)
  28. {
  29.     simpleVert.loadShader("simple.vert", GL_VERTEX_SHADER);
  30.     simpleFrag.loadShader("simple.frag", GL_FRAGMENT_SHADER);
  31.    
  32.     SSP.createProgram();
  33.     SSP.addShaderToProgram(&simpleVert);
  34.     SSP.addShaderToProgram(&simpleFrag);
  35.  
  36.     SSP.linkProgram();
  37.     SSP.useProgram();
  38.  
  39.     return true; //~_^
  40. }
  41.  
  42.  
  43. //matrices
  44. GLuint ModelToWorld;
  45. GLuint WorldToCamera;
  46. GLuint CameraToClip;
  47.  
  48. //camera matrix setup
  49. static vec3 g_camTarget(0.0f, 0.4f, 0.0f);
  50. static vec3 g_sphereCamRelPos(67.5f, -46.0f, 150.0f);   //In spherical coordinates.
  51.  
  52. float DegToRad(float fAngDeg)
  53. {
  54.     const float fDegToRad = 3.14159f * 2.0f / 360.0f;
  55.     return fAngDeg * fDegToRad;
  56. }
  57.  
  58. vec3 ResolveCamPosition()
  59. {
  60.     float phi = DegToRad(g_sphereCamRelPos.x);
  61.     float theta = DegToRad(g_sphereCamRelPos.y + 90.0f);
  62.  
  63.     float fSinTheta = sinf(theta);
  64.     float fCosTheta = cosf(theta);
  65.     float fCosPhi = cosf(phi);
  66.     float fSinPhi = sinf(phi);
  67.  
  68.     vec3 dirToCamera(fSinTheta * fCosPhi, fCosTheta, fSinTheta * fSinPhi);
  69.     return (dirToCamera * g_sphereCamRelPos.z) + g_camTarget;
  70. }
  71.  
  72. mat4 CalcLookAtMatrix(const vec3 &cameraPt, const vec3 &lookPt, const vec3 &upPt)
  73. {
  74.     vec3 lookDir = normalize(lookPt - cameraPt);
  75.     vec3 upDir = normalize(upPt);
  76.  
  77.     vec3 rightDir = normalize(cross(lookDir, upDir));
  78.     vec3 perpUpDir = cross(rightDir, lookDir);
  79.  
  80.     mat4 rotMat(1.0f);
  81.     rotMat[0] = vec4(rightDir, 0.0f);
  82.     rotMat[1] = vec4(perpUpDir, 0.0f);
  83.     rotMat[2] = vec4(-lookDir, 0.0f);
  84.  
  85.     rotMat = transpose(rotMat);
  86.  
  87.     mat4 transMat(1.0f);
  88.     transMat[3] = vec4(-cameraPt, 1.0f);
  89.  
  90.     return rotMat * transMat;
  91. }
  92.  
  93. int main( void )
  94. {
  95.     // Initialise GLFW
  96.     if( !glfwInit() )
  97.     {
  98.         fprintf( stderr, "Failed to initialize GLFW\n" );
  99.         return -1;
  100.     }
  101.  
  102.     glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
  103.     glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
  104.     glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
  105.     glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  106.  
  107.     // Open a window and create its OpenGL context
  108.     if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
  109.     {
  110.         fprintf( stderr, "Failed to open GLFW window.\n" );
  111.         glfwTerminate();
  112.         return -1;
  113.     }
  114.  
  115.     // Initialize GLEW
  116.     if (glewInit() != GLEW_OK) {
  117.         fprintf(stderr, "Failed to initialize GLEW\n");
  118.         return -1;
  119.     }
  120.  
  121.     glfwSetWindowTitle( "Test 3D" );
  122.  
  123.     //initialize objectspace matrix
  124.     mat4 mObjSpace = mat4(
  125.                 vec4(1.0f, 0.0f, 0.0f, 0.0f),
  126.                 vec4(0.0f, 1.0f, 0.0f, 0.0f),
  127.                 vec4(0.0f, 0.0f, 1.0f, 0.0f),
  128.                 vec4(0.0f, 0.0f, 0.0f, 1.0f)
  129.             );  // this should be no transformation
  130.  
  131.  
  132.     //first, use shaders
  133.     LoadShaders();
  134.  
  135.     //uniform for Object to World translation matrix
  136.     GLuint uniformOtW = glGetUniformLocation(SSP.getProgramID(), "OtW");
  137.     GLuint uniformWtC = glGetUniformLocation(SSP.getProgramID(), "WtC");
  138.     GLuint uniformCtC = glGetUniformLocation(SSP.getProgramID(), "CtC");
  139.  
  140.     //we need a VAO
  141.     GLuint VAOID;
  142.     glGenVertexArrays(1, &VAOID);
  143.     glBindVertexArray(VAOID);
  144.  
  145.     //data for buffer
  146.     static const GLfloat vertices[9] = {
  147.         -1.0f, -1.0f, 0.0f,
  148.         1.0f, -1.0f, 0.0f,
  149.         0.0f, 1.0f, 0.0f
  150.     };
  151.  
  152.     //setup buffer and fill
  153.     GLuint verticesBuffer;
  154.     glGenBuffers(1, &verticesBuffer);
  155.     glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
  156.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  157.  
  158.     // Ensure we can capture the escape key being pressed below
  159.     glfwEnable( GLFW_STICKY_KEYS );
  160.  
  161.     // Dark blue background
  162.     glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
  163.     glClearDepth(1.0f);
  164.  
  165.     do{
  166.         //clear
  167.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  168.  
  169.         const vec3 &camPos = ResolveCamPosition();
  170.  
  171.         //matrices... oh joi
  172.         //object to world transformations
  173.         //mat4 OtW = translate(mObjSpace, vec3(0.5f, 0.0f, 0.0f)); //move 0.5 (1/4 "screen" right)
  174.         //mat4 OtW = translate(mObjSpace, vec3(0.0f, 0.0f, 0.0f)); //no transform
  175.         mat4 OtW = scale(mObjSpace, vec3(100.0f, 1.0f, 100.0f));//make it really big
  176.         mat4 WtC = CalcLookAtMatrix(camPos, g_camTarget, vec3(0.0f, 1.0f, 0.0f));
  177.         //cam2clip matrix
  178.         float FoV = 45 - (5 * glfwGetMouseWheel());
  179.         mat4 CtC = perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f); //FoV adjusted by mousewheel, 4/3 aspect ratio, 0.1 - 100.0 clip on z axis
  180.  
  181.         SSP.useProgram();
  182.  
  183.         //set uniforms
  184.         glUniformMatrix4fv(uniformOtW, 1, GL_FALSE, &OtW[0][0]);
  185.         glUniformMatrix4fv(uniformWtC, 1, GL_FALSE, &WtC[0][0]);
  186.         glUniformMatrix4fv(uniformCtC, 1, GL_FALSE, &CtC[0][0]);
  187.  
  188.         //send down the pipe
  189.         glEnableVertexAttribArray(0);
  190.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  191.  
  192.         //draw active VertexArray
  193.         glDrawArrays(GL_TRIANGLES, 0, 3);
  194.  
  195.         // Swap buffers
  196.         glfwSwapBuffers();
  197.  
  198.         //handle keypresses
  199.         int temp = glfwGetKey('w');
  200.         if (glfwGetKey ('W') == GLFW_PRESS) g_camTarget.z -= 4.0f;
  201.         if (glfwGetKey ('s') == GLFW_PRESS) g_camTarget.z += 4.0f;  
  202.         if (glfwGetKey ('d') == GLFW_PRESS) g_camTarget.x += 4.0f;
  203.         if (glfwGetKey ('a') == GLFW_PRESS) g_camTarget.x -= 4.0f;
  204.         if (glfwGetKey ('e') == GLFW_PRESS) g_camTarget.y -= 4.0f;
  205.         if (glfwGetKey ('q') == GLFW_PRESS) g_camTarget.y += 4.0f;
  206.         if (glfwGetKey ('W') == GLFW_PRESS) g_camTarget.z -= 0.4f;
  207.         if (glfwGetKey ('S') == GLFW_PRESS) g_camTarget.z += 0.4f;
  208.         if (glfwGetKey ('D') == GLFW_PRESS) g_camTarget.x += 0.4f;
  209.         if (glfwGetKey ('A') == GLFW_PRESS) g_camTarget.x -= 0.4f;
  210.         if (glfwGetKey ('E') == GLFW_PRESS) g_camTarget.y -= 0.4f;
  211.         if (glfwGetKey ('Q') == GLFW_PRESS) g_camTarget.y += 0.4f;
  212.         if (glfwGetKey ('i') == GLFW_PRESS) g_sphereCamRelPos.y -= 11.25f;
  213.         if (glfwGetKey ('k') == GLFW_PRESS) g_sphereCamRelPos.y += 11.25f;
  214.         if (glfwGetKey ('j') == GLFW_PRESS) g_sphereCamRelPos.x -= 11.25f;
  215.         if (glfwGetKey ('l') == GLFW_PRESS) g_sphereCamRelPos.x += 11.25f;
  216.         if (glfwGetKey ('o') == GLFW_PRESS) g_sphereCamRelPos.z -= 5.0f;
  217.         if (glfwGetKey ('u') == GLFW_PRESS) g_sphereCamRelPos.z += 5.0f;
  218.         if (glfwGetKey ('I') == GLFW_PRESS) g_sphereCamRelPos.y -= 1.125f;
  219.         if (glfwGetKey ('K') == GLFW_PRESS) g_sphereCamRelPos.y += 1.125f;
  220.         if (glfwGetKey ('J') == GLFW_PRESS) g_sphereCamRelPos.x -= 1.125f;
  221.         if (glfwGetKey ('L') == GLFW_PRESS) g_sphereCamRelPos.x += 1.125f;
  222.         if (glfwGetKey ('O') == GLFW_PRESS) g_sphereCamRelPos.z -= 0.5f;
  223.         if (glfwGetKey ('U') == GLFW_PRESS) g_sphereCamRelPos.z += 0.5f;
  224.  
  225.  
  226.     } // Check if the ESC key was pressed or the window was closed
  227.     while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
  228.            glfwGetWindowParam( GLFW_OPENED ) );
  229.  
  230.     // Clean up shaders, close OpenGL window and terminate GLFW
  231.     SSP.deleteProgram();
  232.     simpleVert.deleteShader();
  233.     simpleFrag.deleteShader();
  234.     glfwTerminate();
  235.  
  236.     return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement