bool CMeshLoader::loadVertexBufferObjectFromMesh(std::string const & fileName, int & TriangleCount, GLuint & PositionBufferHandle, GLuint & ColorBufferHandle, GLuint & NormalBufferHandle) { CMesh * Mesh = loadASCIIMesh(fileName); if (! Mesh) return false; Mesh->resizeMesh(SVector3(1)); Mesh->centerMeshByExtents(SVector3(0)); Mesh->computeNormals(); createVertexBufferObject(* Mesh, TriangleCount, PositionBufferHandle, ColorBufferHandle, NormalBufferHandle); return true; }
int main () { //start logger system assert(restart_gl_log()); hardware = {}; //must initialize window before starting gl stuff //create our main window assert(start_gl()); cursor = {}; cursor.vertexData = new GLfloat[36 + (3 * 6)]; setCursorCoordinates(cursor.vertexData, &cursor); GLfloat*cursorColourData = new GLfloat[36 + (3 * 6)]; { for (int i = 0; i < 12; ++i) { cursorColourData[i * 3] = 0.5f; cursorColourData[i * 3 + 1] = 0.0f; cursorColourData[i * 3 + 2] = 0.5f; }; for (int j = 12; j < 17; ++j) { cursorColourData[j * 3] = 0.5f; cursorColourData[j * 3 + 1] = 0.5f; cursorColourData[j * 3 + 2] = 0.5f; } } grid = {}; grid.numberOfLines = 100; grid.heightValue = 0.0f; GLfloat *gridColourData = new GLfloat[100 * 2 * 3]; { int totalVerteces = 100 * 2; for (int i = 0; i < totalVerteces; ++i) { gridColourData[i * 3 ] = 0.5f; gridColourData[i * 3 + 1] = 0.0f; gridColourData[i * 3 + 2] = 0.5f; } } //Create our gridPoints coordinates GLfloat *gridVertexData = new GLfloat[grid.numberOfLines * 6]; { for (int i = 0; i < grid.numberOfLines; ++i) { //draw the lines parallel to the x axis if (i < 50) { gridVertexData[i * 6] = i - 25; // gridVertexData[i * 6 + 1] = grid.heightValue; gridVertexData[i * 6 + 2] = -100.f; gridVertexData[i * 6 + 3] = i - 25; // gridVertexData[i * 6 + 4] = grid.heightValue; gridVertexData[i * 6 + 5] = 100.0f; } //draw the lines parallel to the z axis; if (i >= 50) { gridVertexData[i * 6] = -100.0f; // gridVertexData[i * 6 + 1] = grid.heightValue; gridVertexData[i * 6 + 2] = i - 50 - 25.0f; gridVertexData[i * 6 + 3] = 100.0f; // gridVertexData[i * 6 + 4] = grid.heightValue; gridVertexData[i * 6 + 5] = i - 50 - 25.0f; } } } //create our wall items //2 triangles , 3 points each, 3 coordinate Wall wall = {}; wall.scale = vec3(1.0f, 0, 1.0f); wall.position = vec3(1.0f, 1.0f, 1.0f); GLfloat *wallVertexData = new GLfloat[2 * 3 * 3]; { wallVertexData[0] = wall.scale.v[0] * 0.5f; wallVertexData[1] = 0.0f; wallVertexData[2] = - wall.scale.v[2] * 0.5f; wallVertexData[3] = -wall.scale.v[0] * 0.5f; wallVertexData[4] = 0.0f; wallVertexData[5] = + wall.scale.v[2] * 0.5f; wallVertexData[6] = - wall.scale.v[0] * 0.5f; wallVertexData[7] = 0.0f; wallVertexData[8] = - wall.scale.v[2] * 0.5f; wallVertexData[9] = + wall.scale.v[0] * 0.5f; wallVertexData[10] = 0.0f; wallVertexData[11] = - wall.scale.v[2] * 0.5f; wallVertexData[12] = + wall.scale.v[0] * 0.5f; wallVertexData[13] = 0.0f; wallVertexData[14] = + wall.scale.v[2] * 0.5f; wallVertexData[15] = - wall.scale.v[0] * 0.5f; wallVertexData[16] = 0.0f; wallVertexData[17] = + wall.scale.v[2] * 0.5f; } GLfloat *wallColourData = new GLfloat[2 * 3 * 3]; { for (int i = 0; i < 6; ++i) { wallColourData[i * 3 + 0] = 0.8f; wallColourData[i * 3 + 1] = 0.8f; wallColourData[i * 3 + 2] = 0.8f; } } glfwSetCursorPosCallback(hardware.window,cursor_position_callback); glfwSetInputMode(hardware.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetKeyCallback(hardware.window, key_callback); glfwSetInputMode(hardware.window,GLFW_STICKY_KEYS, 1); GLuint shader_program = create_programme_from_files(VERTEX_SHADER, FRAGMENT_SHADER); /* get version info */ glEnable (GL_DEPTH_TEST); /* enable depth-testing */ glDepthFunc (GL_LESS); createVertexBufferObject(&wallColourVbo, 3 * 3 * 2 * sizeof(GLfloat), wallColourData); createVertexBufferObject(&wallVertexVbo, 3 * 3 * 2 * sizeof(GLfloat), wallVertexData); createVertexBufferObject(&grid.vertexVbo, grid.numberOfLines * 6 * sizeof(GLfloat), gridVertexData); createVertexBufferObject(&grid.colourVbo, grid.numberOfLines * 6 * sizeof(GLfloat), gridColourData); createVertexBufferObject(&cursor.vertexVbo, (36 + (3 * 6)) * sizeof(GLfloat), cursor.vertexData); createVertexBufferObject(&cursor.colourVbo, (36 + (3 * 6)) * sizeof(GLfloat), cursorColourData); createVertexArrayObjet(&wallVao, &wallVertexVbo, 3); createVertexArrayObjet(&grid.vao, &grid.vertexVbo, 3); createVertexArrayObjet(&cursor.vao, &cursor.vertexVbo, 3); cursor.colourAttributeIndex = 1; grid.colourAttributeIndex = 1; wallColourAttributeIndex = 1; setColourMesh(&cursor.vao, &cursor.colourVbo, 3, &cursor.colourAttributeIndex); setColourMesh(&grid.vao, &grid.colourVbo, 3, &grid.colourAttributeIndex); setColourMesh(&wallVao, &wallColourVbo, 3, &wallColourAttributeIndex); free(cursor.vertexData); free(cursorColourData); free(wallVertexData); free(wallColourData); // camera stuff #define PI 3.14159265359 #define DEG_TO_RAD (2.0 * PI) / 360.0 float near = 0.1f; float far = 100.0f; double fov = 67.0f * DEG_TO_RAD; float aspect = (float)hardware.vmode->width /(float)hardware.vmode->height; // matrix components double range = tan (fov * 0.5f) * near; double Sx = (2.0f * near) / (range * aspect + range * aspect); double Sy = near / range; float Sz = -(far + near) / (far - near); float Pz = -(2.0f * far * near) / (far - near); GLfloat proj_mat[] = { Sx, 0.0f, 0.0f, 0.0f, 0.0f, Sy, 0.0f, 0.0f, 0.0f, 0.0f, Sz, -1.0f, 0.0f, 0.0f, Pz, 0.0f }; camera = {}; //create view matrix camera.pos[0] = 0.0f; // don't start at zero, or we will be too close camera.pos[1] = 0.0f; // don't start at zero, or we will be too close camera.pos[2] = 0.5f; // don't start at zero, or we will be too close camera.T = translate (identity_mat4 (), vec3 (-camera.pos[0], -camera.pos[1], -camera.pos[2])); camera.Rpitch = rotate_y_deg (identity_mat4 (), -camera.yaw); camera.Ryaw = rotate_y_deg (identity_mat4 (), -camera.yaw); camera.viewMatrix = camera.Rpitch * camera.T; cursor.yaw = cursor.roll = cursor.pitch += 0.0f; calculateCursorRotations(&cursor); //create the viewmatrix of the wall wall.T= translate (identity_mat4 (), vec3 (2.0f, 2.0f, 2.0f)); glUseProgram(shader_program); camera.view_mat_location = glGetUniformLocation(shader_program, "view"); camera.proj_mat_location = glGetUniformLocation(shader_program, "proj"); glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, camera.viewMatrix.m); glUniformMatrix4fv(camera.proj_mat_location, 1, GL_FALSE, proj_mat); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); walls.push_back(wall); while (!glfwWindowShouldClose (hardware.window)) { updateMovement(&camera); calculateViewMatrices(&camera, &cursor); //set the new view matrix @ the shader level glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, hardware.vmode->width, hardware.vmode->height); glUseProgram(shader_program); //draw the cursor glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, cursor.viewMatrix.m); glBindVertexArray(cursor.vao); glDrawArrays(GL_TRIANGLES, 0, 18); //draw the walls in place //for each wall, draw it. //get view matrix for ( std::vector<Wall>::iterator it = walls.begin(); it != walls.end(); ++it) { Wall tempWall = *it; getTransformationMatrix(&tempWall); glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, tempWall.transformationMatrix.m); glBindVertexArray(wallVao); glDrawArrays(GL_TRIANGLES, 0, 6); } //draw the grid glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, camera.viewMatrix.m); glBindVertexArray(grid.vao); glDrawArrays(GL_LINES, 0, grid.numberOfLines* 2); glfwPollEvents(); if (GLFW_PRESS == glfwGetKey(hardware.window, GLFW_KEY_ESCAPE)) { glfwSetWindowShouldClose(hardware.window, 1); } glfwSwapBuffers(hardware.window); } /* close GL context and any other GLFW resources */ glfwTerminate(); return 0; }