Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. //Initialisation and opengl version setup
  2. bool initOpenGL() {
  3. // init glfw and glew
  4. // setup callback functions for handling keyboard and mouse
  5. // create window and setup viewport
  6.  
  7. if(all_ok)
  8. return true;
  9. else
  10. return false;
  11. }
  12.  
  13. int main(){
  14. if (!initOpenGL())
  15. {
  16. // An error occured
  17. std::cerr << "initialization failed" << std::endl;
  18. return -1;
  19. }
  20.  
  21. // load and compile shader logic
  22.  
  23. ShaderProgram myShader;
  24. myShader.loadShaders("shaders/shader_name.vert", "shaders/shader_name.frag");
  25.  
  26. //Load Mesh
  27. Mesh myMesh;
  28.  
  29. /*
  30. For simple demo here, I am loading wavefront file but in the problem the vertices and the surfaces changes continuously, that means the vbos and vaos would be changing in the game loop .
  31. */
  32.  
  33. myMesh.loadOBJ("models/some_mesh.obj");
  34.  
  35. //load texture for texturing the wavefront object (again for demonstration purpose)
  36.  
  37. Texture2D myTexture;
  38.  
  39. texture.loadTexture("textures/some_image.png", true);
  40.  
  41. /* Setup light pos and other stuff like model world position */
  42. [..]
  43.  
  44. while (!glfwWindowShouldClose(gWindow)){
  45. //Setup model, view, projection matrices
  46. [..]
  47.  
  48. //setup uniforms in shaders like:
  49. myShader.use();
  50. myShader.setUniform("model", glm::mat4(1.0));
  51. myShader.setUniform("view", view);
  52. myShader.setUniform("projection", projection);
  53. myShader.setUniform("viewPos", viewPos);
  54. [...]
  55.  
  56. // DRAW mesh
  57. myTexture.bind(0);
  58. myMesh.draw(); // Renders the OBJ mesh
  59. myTexture.unbind(0);
  60. }
  61.  
  62. }
  63.  
  64. bool Mesh::loadOBJ(const std::string &filename){
  65. //parse obj file
  66.  
  67. // iterate over each face to construct one flat array *mVertices* containing the position, normal and texCoord info like:
  68. mVertices = [[vec3(x,y,z), vec3(nx,ny,nx), vec2(tx,ty)], [...], [...], ...]
  69.  
  70. /* mVertices is an array of 'Vertex'. where :
  71.  
  72. struct Vertex{
  73. glm::vec3 position;
  74. glm::vec3 normal;
  75. glm::vec2 texCoords;
  76. };
  77.  
  78. */
  79. //initialize VBOs and VAOs
  80. glGenVertexArrays(1, &mVAO);
  81. glGenBuffers(1, &mVBO);
  82.  
  83. glBindVertexArray(mVAO);
  84. glBindBuffer(GL_ARRAY_BUFFER, mVBO);
  85. glBufferData(GL_ARRAY_BUFFER, mVertices.size() * sizeof(Vertex), &mVertices[0], GL_STATIC_DRAW);
  86.  
  87. // Vertex Positions
  88. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)0);
  89. glEnableVertexAttribArray(0);
  90.  
  91. // Normals attribute
  92. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)(3 * sizeof(GLfloat)));
  93. glEnableVertexAttribArray(1);
  94.  
  95. // Vertex Texture Coords
  96. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)(6 * sizeof(GLfloat)));
  97. glEnableVertexAttribArray(2);
  98.  
  99. // unbind to make sure other code does not change it somewhere else
  100. glBindVertexArray(0);
  101.  
  102. }
  103.  
  104. glBindVertexArray(mVAO);
  105. glDrawArrays(GL_TRIANGLES, 0, mVertices.size());
  106. glBindVertexArray(0);
  107.  
  108. bool Texture2D::loadTexture(const string &fileName, bool generateMipMaps)
  109. {
  110. int width, height, components;
  111.  
  112. // Use stbi image library to load our image
  113. unsigned char *imageData = stbi_load(fileName.c_str(), &width, &height, &components, STBI_rgb_alpha);
  114.  
  115. if (imageData == NULL)
  116. {
  117. std::cerr << "Error loading texture '" << fileName << "'" << std::endl;
  118. return false;
  119. }
  120.  
  121. // Invert image
  122. int widthInBytes = width * 4;
  123. unsigned char *top = NULL;
  124. unsigned char *bottom = NULL;
  125. unsigned char temp = 0;
  126. int halfHeight = height / 2;
  127. for (int row = 0; row < halfHeight; row++)
  128. {
  129. top = imageData + row * widthInBytes;
  130. bottom = imageData + (height - row - 1) * widthInBytes;
  131. for (int col = 0; col < widthInBytes; col++)
  132. {
  133. temp = *top;
  134. *top = *bottom;
  135. *bottom = temp;
  136. top++;
  137. bottom++;
  138. }
  139. }
  140.  
  141. glGenTextures(1, &mTexture);
  142. glBindTexture(GL_TEXTURE_2D, mTexture);
  143.  
  144.  
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  149.  
  150. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
  151.  
  152. if (generateMipMaps)
  153. glGenerateMipmap(GL_TEXTURE_2D);
  154.  
  155. stbi_image_free(imageData);
  156. glBindTexture(GL_TEXTURE_2D, 0); // unbind texture when done so we don't accidentally mess up our mTexture
  157.  
  158. return true;
  159. }
  160.  
  161. glActiveTexture(GL_TEXTURE0 + texUnit);
  162. glBindTexture(GL_TEXTURE_2D, mTexture);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement