Mesh::Mesh(const char* path) { //TextureID = glGetUniformLocation(programID, "texture_sampler"); std::vector< glm::vec3 > vertices; std::vector< glm::vec2 > uvs; std::vector< glm::vec3 > normals; AssimpLoadFile(path, vertices, uvs, normals); std::vector<unsigned short> indices; std::vector<glm::vec3> indexed_vertices; std::vector<glm::vec2> indexed_uvs; std::vector<glm::vec3> indexed_normals; indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs, indexed_normals); //AssimpLoadFile(path, indexed_vertices, indexed_uvs, indexed_normals, indices); indices_count = indices.size(); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); glGenBuffers(1, &uv_buffer); glBindBuffer(GL_ARRAY_BUFFER, uv_buffer); glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(glm::vec2), &indexed_uvs[0], GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0 ); glGenBuffers(1, &normal_buffer); glBindBuffer(GL_ARRAY_BUFFER, normal_buffer); glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); glGenBuffers(1, &element_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0] , GL_STATIC_DRAW); glBindVertexArray(0); /* if(tiva != 0) { tiva = new btTriangleIndexVertexArray( indices.size() / 3, //assuming indices is divisible by 3 may not be true (int*)&indices[0], sizeof(unsigned short), indexed_vertices.size(), (btScalar*)&indexed_vertices[0], sizeof(glm::vec3) ); }*/ }
void Texture::loadResources() { bikeTexture = loadTGATexture("textures/bike_text.tga"); planeTexture = loadTGATexture("textures/square_plane.tga"); std::vector<glm::vec3> vertices; std::vector<glm::vec3> normals; std::vector<glm::vec2> uvs; std::vector<glm::vec3> colors; if(!Texture::loadOBJ("objs/bike.obj",vertices, uvs,normals)) { printf("Error loading bike.obj\n"); } indexVBO(vertices, uvs, normals, bikeModel.indices, bikeModel.vertices, bikeModel.uvs, bikeModel.normals); }
void Mesh::load( const char * fileName ) { bool res = loadOBJ(fileName, vertices, uvs, normals); std::vector<glm::vec3> indexed_vertices; std::vector<glm::vec2> indexed_uvs; std::vector<glm::vec3> indexed_normals; indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs, indexed_normals); glGenBuffers(1, &verticesBuffer); glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer); glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_STATIC_DRAW); glGenBuffers(1, &uvsBuffer); glBindBuffer(GL_ARRAY_BUFFER, uvsBuffer); glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(glm::vec2), &indexed_uvs[0], GL_STATIC_DRAW); glGenBuffers(1, &normalsBuffer); glBindBuffer(GL_ARRAY_BUFFER, normalsBuffer); glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_STATIC_DRAW); glGenBuffers(1, &indicesBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0] , GL_STATIC_DRAW); }
void InitializeResources() { printf("InitializeResources()\n"); // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units ProjectionMatrix = glm::perspective( 45.0f, // The horizontal Field of View, in degrees : the amount of "zoom". Think "camera lens". Usually between 90° (extra wide) and 30° (quite zoomed in) 4.0f / 3.0f, // Aspect Ratio. Depends on the size of your window. Notice that 4/3 == 800/600 == 1280/960, sounds familiar ? 0.1f, // Near clipping plane. Keep as big as possible, or you'll get precision issues. 100.0f // Far clipping plane. Keep as little as possible. ); // Or, for an ortho camera : //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates // Camera matrix ViewMatrix = glm::lookAt( cameraLoc, // Camera is cameraLoc, in World Space cameraDir, // and looks to cameraDir orientation // Head is orientation ); ModelMatrix = glm::mat4(1.0f); // Model matrix : an identity matrix (model will be at the origin) // Our ModelViewProjection : multiplication of our 3 matrices MVP = ProjectionMatrix * ViewMatrix * ModelMatrix; // Remember, matrix multiplication is the other way around //loads object bool res = LoadAssimp("Resources/cheb.obj", indices, vertices, uvs, normals); // index the vbo indices.clear(); indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs, indexed_normals); // Vertex Array Object and set it as the current one glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders //ShaderIDs[0] = LoadShaders( "VertexShader.vs", "FragmentShader.fs" ); pickingProgramID = LoadShaders( "Picking.vertexshader", "Picking.fragmentshader" ); gprogramID = LoadShaders( "TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader" ); ProgramID = LoadShaders( "VertexShader.vs", "FragmentShader.fs" ); ShaderIDs[0] = ProgramID; ProgramID = LoadShaders( "WireFrame_VertexShader.vs", "FragmentShader.fs" ); ShaderIDs[1] = ProgramID; // Get a handle for our uniforms MatrixID = glGetUniformLocation(ProgramID, "MVP"); ViewMatrixID = glGetUniformLocation(ProgramID, "V"); ModelMatrixID = glGetUniformLocation(ProgramID, "M"); NormalMatrixID = glGetUniformLocation(ProgramID, "NormalMatrix"); // Loads the texture Texture = loadDDS("Resources/purple.DDS"); // Sets the uniform for the texture sampler TextureID = glGetUniformLocation(ProgramID, "myTexturesampler"); // Generate a vertex buffer, put the resulting identifier in vertexbuffer glGenBuffers(1, &vertexbuffer); // The following commands will talk about our 'vertexbuffer' buffer glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); // Give our vertices to OpenGL. glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_STATIC_DRAW); // Generate a vertex buffer, put the resulting identifier in vertexbuffer glGenBuffers(1, &uvbuffer); // The following commands will talk about our 'uvbuffer' buffer glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); // Give our uv's to OpenGL. glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(glm::vec2), &indexed_uvs[0], GL_STATIC_DRAW); // Generate a normal buffer, put the resulting identifier in normalbuffer glGenBuffers(1, &normalbuffer); // The following commands will talk about our 'normalbuffer' buffer glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); // Give our normal's to OpenGL. glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_STATIC_DRAW); // Generate a index buffer, put the resulting identifier in elementbuffer glGenBuffers(1, &elementbuffer); // The following commands will talk about our 'elementbuffer' buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); // Give our index's to OpenGL. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW); // Use our shader glUseProgram(ShaderIDs[0]); LightPosID = glGetUniformLocation(ProgramID, "lightPos"); CameraID = glGetUniformLocation(ProgramID, "C"); gVertexArrayID; glGenVertexArrays(1, &gVertexArrayID); glBindVertexArray(gVertexArrayID); gvertexbuffer; glGenBuffers(1, &gvertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, gvertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); colorbuffer; glGenBuffers(1, &colorbuffer); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); }
int tutorial_10(GLFWwindow* window) { // Initialise GLFW if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); return -1; } glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL // Open a window and create its OpenGL context window = glfwCreateWindow(1024, 768, "Tutorial 10: Transparency", NULL, NULL); // (In the accompanying source code, this variable is global) if (window == NULL){ fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n"); glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // Initialize GLEW glewExperimental = true; // Needed in core profile if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); return -1; } // Ensure we can capture the escape key being pressed below glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); // Dark blue background glClearColor(0.0f, 0.0f, 0.4f, 0.0f); // Enable depth test glEnable(GL_DEPTH_TEST); // Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS); // Cull triangles which normal is not towards the camera //glEnable(GL_CULL_FACE); //Create VertexArray element & bind it GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders GLuint programID = LoadShaders("..\\external\\shaders\\tutorial10.vert", "..\\external\\shaders\\tutorial10.frag"); // Get a handle for our "MVP" uniform GLuint MatrixID = glGetUniformLocation(programID, "MVP"); GLuint ViewMatrixID = glGetUniformLocation(programID, "V"); GLuint ModelMatrixID = glGetUniformLocation(programID, "M"); // Load the texture using any two methods //GLuint Texture = loadBMP_custom("..\\external\\textures\\uvtemplate.bmp"); GLuint Texture = loadDDS("..\\external\\textures\\uvmap.DDS"); // Get a handle for our "myTextureSampler" uniform GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler"); // Read our .obj file std::vector<glm::vec3> vertices; std::vector<glm::vec2> uvs; std::vector<glm::vec3> normals; bool res = loadOBJ("..\\external\\objects\\suzanne.obj", vertices, uvs, normals); std::vector<unsigned short> indices; std::vector<glm::vec3> indexed_vertices; std::vector<glm::vec2> indexed_uvs; std::vector<glm::vec3> indexed_normals; indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs, indexed_normals); // Load it into a VBO GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); GLuint uvbuffer; glGenBuffers(1, &uvbuffer); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW); GLuint normalbuffer; glGenBuffers(1, &normalbuffer); glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW); // Generate a buffer for the indices as well GLuint elementbuffer; glGenBuffers(1, &elementbuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW); // Get a handle for our "LightPosition" uniform glUseProgram(programID); GLuint LightID = glGetUniformLocation(programID, "LightPosition_worldspace"); // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); do{ // Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use our shader glUseProgram(programID); // Compute the MVP matrix from keyboard and mouse input computeMatricesFromInputs(window); glm::mat4 ProjectionMatrix = getProjectionMatrix(); glm::mat4 ViewMatrix = getViewMatrix(); glm::mat4 ModelMatrix = glm::mat4(1.0); glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix; // Send our transformation to the currently bound shader, // in the "MVP" uniform glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]); glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]); glm::vec3 lightPos = glm::vec3(4, 4, 4); glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z); // Bind our texture in Texture Unit 0 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, Texture); // Set our "myTextureSampler" sampler to user Texture Unit 0 glUniform1i(TextureID, 0); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( 0, // attribute. No particular reason for 0, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); // 2nd attribute buffer : UVs glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); glVertexAttribPointer( 1, // attribute. No particular reason for 1, but must match the layout in the shader. 2, // size : U+V => 2 GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); // 3rd attribute buffer : normals glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); glVertexAttribPointer( 2, // attribute 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); // Index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); // Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, vertices.size()); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); // Swap buffers glfwSwapBuffers(window); glfwPollEvents(); } // Check if the ESC key was pressed or the window was closed while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); // Cleanup VBO and shader glDeleteBuffers(1, &vertexbuffer); glDeleteBuffers(1, &uvbuffer); glDeleteBuffers(1, &normalbuffer); glDeleteBuffers(1, &elementbuffer); glDeleteProgram(programID); glDeleteVertexArrays(1, &TextureID); glDeleteVertexArrays(1, &VertexArrayID); // Close OpenGL window and terminate GLFW glfwTerminate(); return 0; }