Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include common headers
- #include "common_header.h"
- // Include shader class
- #include "shaders.h"
- // Include GLEW
- #include <GL/glew.h>
- // Include GLFW
- #include <GL/glfw.h>
- // Include GLM
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- //include glew
- #include <gl/glew.h>
- using namespace glm;
- using namespace std;
- //shaders
- CShader simpleFrag, simpleVert;
- CShaderProgram SSP;
- bool LoadShaders(void)
- {
- simpleVert.loadShader("simple.vert", GL_VERTEX_SHADER);
- simpleFrag.loadShader("simple.frag", GL_FRAGMENT_SHADER);
- SSP.createProgram();
- SSP.addShaderToProgram(&simpleVert);
- SSP.addShaderToProgram(&simpleFrag);
- SSP.linkProgram();
- SSP.useProgram();
- return true; //~_^
- }
- //matrices
- GLuint ModelToWorld;
- GLuint WorldToCamera;
- GLuint CameraToClip;
- //camera matrix setup
- static vec3 g_camTarget(0.0f, 0.4f, 0.0f);
- static vec3 g_sphereCamRelPos(67.5f, -46.0f, 150.0f); //In spherical coordinates.
- float DegToRad(float fAngDeg)
- {
- const float fDegToRad = 3.14159f * 2.0f / 360.0f;
- return fAngDeg * fDegToRad;
- }
- vec3 ResolveCamPosition()
- {
- float phi = DegToRad(g_sphereCamRelPos.x);
- float theta = DegToRad(g_sphereCamRelPos.y + 90.0f);
- float fSinTheta = sinf(theta);
- float fCosTheta = cosf(theta);
- float fCosPhi = cosf(phi);
- float fSinPhi = sinf(phi);
- vec3 dirToCamera(fSinTheta * fCosPhi, fCosTheta, fSinTheta * fSinPhi);
- return (dirToCamera * g_sphereCamRelPos.z) + g_camTarget;
- }
- mat4 CalcLookAtMatrix(const vec3 &cameraPt, const vec3 &lookPt, const vec3 &upPt)
- {
- vec3 lookDir = normalize(lookPt - cameraPt);
- vec3 upDir = normalize(upPt);
- vec3 rightDir = normalize(cross(lookDir, upDir));
- vec3 perpUpDir = cross(rightDir, lookDir);
- mat4 rotMat(1.0f);
- rotMat[0] = vec4(rightDir, 0.0f);
- rotMat[1] = vec4(perpUpDir, 0.0f);
- rotMat[2] = vec4(-lookDir, 0.0f);
- rotMat = transpose(rotMat);
- mat4 transMat(1.0f);
- transMat[3] = vec4(-cameraPt, 1.0f);
- return rotMat * transMat;
- }
- int main( void )
- {
- // Initialise GLFW
- if( !glfwInit() )
- {
- fprintf( stderr, "Failed to initialize GLFW\n" );
- return -1;
- }
- glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
- glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // Open a window and create its OpenGL context
- if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
- {
- fprintf( stderr, "Failed to open GLFW window.\n" );
- glfwTerminate();
- return -1;
- }
- // Initialize GLEW
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- return -1;
- }
- glfwSetWindowTitle( "Test 3D" );
- //initialize objectspace matrix
- mat4 mObjSpace = mat4(
- vec4(1.0f, 0.0f, 0.0f, 0.0f),
- vec4(0.0f, 1.0f, 0.0f, 0.0f),
- vec4(0.0f, 0.0f, 1.0f, 0.0f),
- vec4(0.0f, 0.0f, 0.0f, 1.0f)
- ); // this should be no transformation
- //first, use shaders
- LoadShaders();
- //uniform for Object to World translation matrix
- GLuint uniformOtW = glGetUniformLocation(SSP.getProgramID(), "OtW");
- GLuint uniformWtC = glGetUniformLocation(SSP.getProgramID(), "WtC");
- GLuint uniformCtC = glGetUniformLocation(SSP.getProgramID(), "CtC");
- //we need a VAO
- GLuint VAOID;
- glGenVertexArrays(1, &VAOID);
- glBindVertexArray(VAOID);
- //data for buffer
- static const GLfloat vertices[9] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f
- };
- //setup buffer and fill
- GLuint verticesBuffer;
- glGenBuffers(1, &verticesBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- // Ensure we can capture the escape key being pressed below
- glfwEnable( GLFW_STICKY_KEYS );
- // Dark blue background
- glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
- glClearDepth(1.0f);
- do{
- //clear
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- const vec3 &camPos = ResolveCamPosition();
- //matrices... oh joi
- //object to world transformations
- //mat4 OtW = translate(mObjSpace, vec3(0.5f, 0.0f, 0.0f)); //move 0.5 (1/4 "screen" right)
- //mat4 OtW = translate(mObjSpace, vec3(0.0f, 0.0f, 0.0f)); //no transform
- mat4 OtW = scale(mObjSpace, vec3(100.0f, 1.0f, 100.0f));//make it really big
- mat4 WtC = CalcLookAtMatrix(camPos, g_camTarget, vec3(0.0f, 1.0f, 0.0f));
- //cam2clip matrix
- float FoV = 45 - (5 * glfwGetMouseWheel());
- 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
- SSP.useProgram();
- //set uniforms
- glUniformMatrix4fv(uniformOtW, 1, GL_FALSE, &OtW[0][0]);
- glUniformMatrix4fv(uniformWtC, 1, GL_FALSE, &WtC[0][0]);
- glUniformMatrix4fv(uniformCtC, 1, GL_FALSE, &CtC[0][0]);
- //send down the pipe
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- //draw active VertexArray
- glDrawArrays(GL_TRIANGLES, 0, 3);
- // Swap buffers
- glfwSwapBuffers();
- //handle keypresses
- int temp = glfwGetKey('w');
- if (glfwGetKey ('W') == GLFW_PRESS) g_camTarget.z -= 4.0f;
- if (glfwGetKey ('s') == GLFW_PRESS) g_camTarget.z += 4.0f;
- if (glfwGetKey ('d') == GLFW_PRESS) g_camTarget.x += 4.0f;
- if (glfwGetKey ('a') == GLFW_PRESS) g_camTarget.x -= 4.0f;
- if (glfwGetKey ('e') == GLFW_PRESS) g_camTarget.y -= 4.0f;
- if (glfwGetKey ('q') == GLFW_PRESS) g_camTarget.y += 4.0f;
- if (glfwGetKey ('W') == GLFW_PRESS) g_camTarget.z -= 0.4f;
- if (glfwGetKey ('S') == GLFW_PRESS) g_camTarget.z += 0.4f;
- if (glfwGetKey ('D') == GLFW_PRESS) g_camTarget.x += 0.4f;
- if (glfwGetKey ('A') == GLFW_PRESS) g_camTarget.x -= 0.4f;
- if (glfwGetKey ('E') == GLFW_PRESS) g_camTarget.y -= 0.4f;
- if (glfwGetKey ('Q') == GLFW_PRESS) g_camTarget.y += 0.4f;
- if (glfwGetKey ('i') == GLFW_PRESS) g_sphereCamRelPos.y -= 11.25f;
- if (glfwGetKey ('k') == GLFW_PRESS) g_sphereCamRelPos.y += 11.25f;
- if (glfwGetKey ('j') == GLFW_PRESS) g_sphereCamRelPos.x -= 11.25f;
- if (glfwGetKey ('l') == GLFW_PRESS) g_sphereCamRelPos.x += 11.25f;
- if (glfwGetKey ('o') == GLFW_PRESS) g_sphereCamRelPos.z -= 5.0f;
- if (glfwGetKey ('u') == GLFW_PRESS) g_sphereCamRelPos.z += 5.0f;
- if (glfwGetKey ('I') == GLFW_PRESS) g_sphereCamRelPos.y -= 1.125f;
- if (glfwGetKey ('K') == GLFW_PRESS) g_sphereCamRelPos.y += 1.125f;
- if (glfwGetKey ('J') == GLFW_PRESS) g_sphereCamRelPos.x -= 1.125f;
- if (glfwGetKey ('L') == GLFW_PRESS) g_sphereCamRelPos.x += 1.125f;
- if (glfwGetKey ('O') == GLFW_PRESS) g_sphereCamRelPos.z -= 0.5f;
- if (glfwGetKey ('U') == GLFW_PRESS) g_sphereCamRelPos.z += 0.5f;
- } // Check if the ESC key was pressed or the window was closed
- while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
- glfwGetWindowParam( GLFW_OPENED ) );
- // Clean up shaders, close OpenGL window and terminate GLFW
- SSP.deleteProgram();
- simpleVert.deleteShader();
- simpleFrag.deleteShader();
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement