Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #version 330 core
  2.  
  3. layout (location = 0) in vec3 aPos;
  4. void main()
  5. {
  6. gl_Position = vec4(aPos, 1.0f);
  7. }
  8.  
  9. #version 330 core
  10. out vec4 FragColor;
  11. void main()
  12. {
  13. FragColor = vec4(1.0f,1.0f,1.0f,1.0f);
  14. }
  15.  
  16. #include <glad/glad.h>
  17. #include <GLFW/glfw3.h>
  18. #include "shader.h"
  19. #include <glm/glm.hpp>
  20. #include <glm/gtc/matrix_transform.hpp>
  21. #include <glm/gtc/type_ptr.hpp>
  22. #include "Object.h"
  23.  
  24. int width = 600;
  25. int height = 600;
  26.  
  27. int main()
  28. {
  29. //window creation -----------------------------------------------------------
  30. glfwInit();
  31. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  32. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  33. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  34. glfwWindowHint(GLFW_SAMPLES, 16);
  35. GLFWwindow* window = glfwCreateWindow(width, height, "Mumel", NULL, NULL);
  36.  
  37. glfwMakeContextCurrent(window);
  38.  
  39. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  40. {
  41. std::cout << "Error during GLAD initialization" << std::endl;
  42. glfwTerminate();
  43. return -1;
  44. }
  45.  
  46. glViewport(0, 0, width, height);
  47. //----------------------------------------------------------------------
  48. Shader sh("./shader/vShader.vSh", "./shader/fShader.fSh");
  49.  
  50. Circle * circle = new Circle(0.5); //is explained later in the post
  51.  
  52. while (!glfwWindowShouldClose(window))
  53. {
  54. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  55. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  56.  
  57. sh.use();
  58. circle->draw();
  59.  
  60. glfwSwapBuffers(window);
  61. glfwPollEvents();
  62. }
  63. }
  64.  
  65. #ifndef OBJECT_H
  66. #define OBJECT_H
  67.  
  68. #include <glad/glad.h>
  69. #include <iostream>
  70. #include "shader.h"
  71. #include <vector>
  72.  
  73.  
  74. class Circle
  75. {
  76. public:
  77. static const int size = 6;
  78.  
  79. Circle()
  80. {
  81. std::vector<float> vertexData(3 * size);
  82. // center
  83. vertexData.at(0) = 0.0f;
  84. vertexData.at(1) = 0.0f;
  85. vertexData.at(2) = 0.0f;
  86.  
  87. vertexData.at(3) = 0.5f;
  88. vertexData.at(5) = 0.0f;
  89. vertexData.at(4) = -0.5f;
  90.  
  91. vertexData.at(6) = -0.5f;
  92. vertexData.at(8) = 0.0f;
  93. vertexData.at(7) = -0.5f;
  94.  
  95. vertexData.at(9) = -0.5f;
  96. vertexData.at(11) = 0.0f;
  97. vertexData.at(10) = 0.5f;
  98.  
  99. vertexData.at(12) = 0.5f;
  100. vertexData.at(14) = 0.0f;
  101. vertexData.at(13) = 0.5f;
  102.  
  103. vertexData.at(15) = 0.5f;
  104. vertexData.at(17) = 0.0f;
  105. vertexData.at(16) = -0.5f;
  106.  
  107.  
  108. glGenBuffers(1, &VBO);
  109. glGenVertexArrays(1, &VAO);
  110. glBindVertexArray(VAO);
  111.  
  112. float * dataArr = vertexData.data(); //The GLSL shader needs a C Array, this returns the pointer to the first array object used by std::vector
  113. int dataSize = vertexData.size();
  114.  
  115. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  116. glBufferData(GL_ARRAY_BUFFER, dataSize * sizeof(float), dataArr, GL_STATIC_DRAW);
  117.  
  118. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  119. glEnableVertexAttribArray(0);
  120.  
  121. glBindVertexArray(0);
  122. glBindBuffer(GL_ARRAY_BUFFER, 0);
  123. }
  124.  
  125. void draw()
  126. {
  127. glBindVertexArray(VAO);
  128. glDrawArrays(GL_TRIANGLE_FAN, 0, 3 * (size - 2));
  129. glBindVertexArray(0);
  130. }
  131. private:
  132. unsigned int VBO, VAO;
  133.  
  134. };
  135.  
  136. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement