Guest User

Functional VBO Modification

a guest
Jun 21st, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. // CPPTutorial.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <Windows.h>
  6. #include <fstream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. #include <glad/glad.h>
  11. #include <GLFW/glfw3.h>
  12. #include <bitset>
  13.  
  14. #include <Shader.h>
  15.  
  16. GLuint LoadShader(const char* vertexFile, const char* fragmentFile);
  17.  
  18. // Define this function so it can be seen in main()
  19. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  20. int processInput(GLFWwindow* window);
  21. std::string readFile(const char* filePath);
  22.  
  23. const char* vertexShaderSourceLocation = "VertexShader.vert";
  24. const char* fragmentShaderSourceLocation = "FragmentShader.frag";
  25.  
  26. const char* vertexShaderSource;
  27. const char* fragmentShaderSource;
  28.  
  29. /**"#version 330 core\n"
  30. "layout (location = 0) in vec3 aPos;\n"
  31. "void main()\n"
  32. "{\n"
  33. " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
  34. "}\0";
  35.  
  36. = "#version 330 core\n"
  37. "out vec4 FragColor;\n"
  38. "void main() {\n"
  39. "FragColor = vec4(0.5f, 0.0f, 1.0f, 1.0f);} ";
  40. **/
  41.  
  42.  
  43.  
  44. int main()
  45. {
  46.  
  47. glfwInit();
  48. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  49. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  50. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  51.  
  52. GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
  53. if (window == NULL)
  54. {
  55. std::cout << "Failed to create GLFW window" << std::endl;
  56. glfwTerminate();
  57. return -1;
  58. }
  59. glfwMakeContextCurrent(window);
  60.  
  61. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  62. {
  63. std::cout << "Failed to initialize GLAD" << std::endl;
  64. return -1;
  65. }
  66.  
  67. glViewport(0, 0, 800, 600);
  68.  
  69. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  70.  
  71. // Initialisation done. Let's draw something:
  72.  
  73. float vertices[] = {
  74. // 3-pos // 4-col
  75. 0.0f, 1.0f, 0.0f, 0.6f, 0.0f, 0.6f, 1.0f,
  76. -0.5f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f,
  77. 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  78. };
  79.  
  80. unsigned int indices[] = {
  81. 0, 1, 2,
  82. 1, 2, 3
  83. };
  84.  
  85. // ***** Pass Vertex Information
  86.  
  87. unsigned int VBO; // Vertex buffer object
  88. glGenBuffers(1, &VBO); // Generate uid for this buffer
  89. glBindBuffer(GL_ARRAY_BUFFER, VBO); // Bind our buffer to GL_STREAM_DRAW, setting it as the current working buffer of this type.
  90. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW); // Copy our vertex data into the buffer memory. Static draw shows it is set once and used repeatedly
  91.  
  92. //Load Shader program in !!
  93. GLuint shaderProgram = LoadShader(vertexShaderSourceLocation, fragmentShaderSourceLocation);
  94.  
  95. // Specify that this is the program we want to use from now on.
  96. glUseProgram(shaderProgram);
  97.  
  98. //Shader ourShader("FragmentShader.frag", "VertexShader.vert");
  99.  
  100. // Tell OpenGL how to interpret our vertex data:
  101. // 0 is the location of the position attribute, specified in vertex shader. vec3. float. not normalized. stride is distance between consecutive vertex attributes. offset of 0 as it is at the start of the buffer.
  102. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  103. glEnableVertexAttribArray(0);
  104.  
  105. // EBO for storing the order in which we render vertices.
  106. unsigned int EBO;
  107. glGenBuffers(1, &EBO);
  108. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  109. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
  110.  
  111. // Generate a Vertex Array Object to store our rendering procedure.
  112. unsigned int VAO;
  113. glGenVertexArrays(1, &VAO);
  114. // 1. bind Vertex Array Object, Element Buffer Object
  115. glBindVertexArray(VAO);
  116. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  117. // 2. copy our vertices array in a buffer for OpenGL to use
  118. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  119. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
  120. // 2. copy our vertex indices in a buffer for OpenGL to use
  121. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  122. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(indices), indices);
  123. // 3. then set our vertex attributes pointers:
  124. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0); // Pos vec3
  125. glEnableVertexAttribArray(0);
  126. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3* sizeof(float))); // Col vec4
  127. glEnableVertexAttribArray(1);
  128.  
  129. vertices[0] = 0.4f;
  130.  
  131. // Wireframes
  132. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  133.  
  134. while (!glfwWindowShouldClose(window))
  135. {
  136.  
  137. int movement = processInput(window);
  138.  
  139. if(movement != 0)
  140. std::cout << "movement: " << std::bitset<8>(movement) << std::endl;
  141.  
  142. std::cout << vertices[1] << std::endl;
  143.  
  144. if (movement && 1UL)
  145. {
  146. //vertices[1] += 0.1f;
  147. //vertices[4] += 0.1f;
  148. //vertices[7] += 0.1f;
  149. }
  150.  
  151. // clear colorbuffer
  152. glClearColor(0.6f, 0.0f, 0.6f, 1.0f);
  153. glClear(GL_COLOR_BUFFER_BIT);
  154.  
  155. glUseProgram(shaderProgram);
  156.  
  157. float timeValue = glfwGetTime();
  158. float colorValue = (sin(timeValue*3) / 2.0f) + 0.5f;
  159.  
  160. std::cout << colorValue << std::endl;
  161. //float colorVec[4] = { colorValue, (colorValue / 2.0f), 0.0f, 1.0f };
  162. //ourShader.setVec4("vertexColor", colorValue, colorValue / 2.0f, 0.0f, 1.0f);
  163.  
  164. int vertexColorLocation = glGetUniformLocation(shaderProgram, "variableColor");
  165. glUniform4f(vertexColorLocation, colorValue, colorValue/2.0f, 0.0f, 1.0f);
  166.  
  167. /**
  168. glUniform1f(glGetUniformLocation(shaderProgram, "horizOffset"), colorValue);
  169.  
  170. glBindVertexArray(VAO);
  171. glDrawArrays(GL_TRIANGLES, 0, 3);**/
  172.  
  173. glUniform1f(glGetUniformLocation(shaderProgram, "horizOffset"), colorValue);
  174.  
  175. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  176. // 2. copy our vertices array in a buffer for OpenGL to use
  177. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  178. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
  179. // 2. copy our vertex indices in a buffer for OpenGL to use
  180. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  181. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(indices), indices);
  182. // 3. then set our vertex attributes pointers:
  183. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0); // Pos vec3
  184. glEnableVertexAttribArray(0);
  185. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float))); // Col vec4
  186. glEnableVertexAttribArray(1);
  187. glDrawArrays(GL_TRIANGLES, 0, 3);
  188.  
  189. glfwSwapBuffers(window);
  190. glfwPollEvents();
  191. }
  192.  
  193. glfwTerminate(); // Properly clean all of GLFW's allocated resources
  194. return 0;
  195. }
  196.  
  197. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  198. {
  199. glViewport(0, 0, width, height);
  200. std::cout << "Resized to " << width << ", " << height << std::endl;
  201. }
  202.  
  203. int processInput(GLFWwindow* window)
  204. {
  205. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  206. glfwSetWindowShouldClose(window, true);
  207.  
  208. unsigned int output = 0;
  209.  
  210. if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
  211. output = output | 1UL;
  212. if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
  213. output = output | 2UL;
  214. if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
  215. output = output | 4UL;
  216. if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
  217. output = output | 8UL;
  218.  
  219. return output;
  220. }
  221.  
  222. std::string readFile(const char* filePath) {
  223. std::string content;
  224. std::ifstream fileStream(filePath, std::ios::in);
  225.  
  226. if (!fileStream.is_open()) {
  227. std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
  228. return "";
  229. }
  230.  
  231. std::string line = "";
  232. while (!fileStream.eof()) {
  233. std::getline(fileStream, line);
  234. content.append(line + "\n");
  235. }
  236.  
  237. fileStream.close();
  238. return content;
  239. }
  240.  
  241. GLuint LoadShader(const char* vertexPath, const char* fragmentPath)
  242. {
  243. // 1. retrieve the vertex/fragment source code from filePath
  244. std::string vertexCode;
  245. std::string fragmentCode;
  246. std::ifstream vShaderFile;
  247. std::ifstream fShaderFile;
  248. // ensure ifstream objects can throw exceptions:
  249. vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  250. fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  251. try
  252. {
  253. // open files
  254. vShaderFile.open(vertexPath);
  255. fShaderFile.open(fragmentPath);
  256. std::stringstream vShaderStream, fShaderStream;
  257. // read file's buffer contents into streams
  258. vShaderStream << vShaderFile.rdbuf();
  259. fShaderStream << fShaderFile.rdbuf();
  260. // close file handlers
  261. vShaderFile.close();
  262. fShaderFile.close();
  263. // convert stream into string
  264. vertexCode = vShaderStream.str();
  265. fragmentCode = fShaderStream.str();
  266. }
  267. catch (std::ifstream::failure e)
  268. {
  269. std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
  270. }
  271. const char* vShaderCode = vertexCode.c_str();
  272. const char* fShaderCode = fragmentCode.c_str();
  273.  
  274. // 2. compile shaders
  275. unsigned int vertex, fragment;
  276. int success;
  277. char infoLog[512];
  278.  
  279. // vertex Shader
  280. vertex = glCreateShader(GL_VERTEX_SHADER);
  281. glShaderSource(vertex, 1, &vShaderCode, NULL);
  282. glCompileShader(vertex);
  283. // print compile errors if any
  284. glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
  285. if (!success)
  286. {
  287. glGetShaderInfoLog(vertex, 512, NULL, infoLog);
  288. std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
  289. };
  290.  
  291. // similiar for Fragment Shader
  292. fragment = glCreateShader(GL_FRAGMENT_SHADER);
  293. glShaderSource(fragment, 1, &fShaderCode, NULL);
  294. glCompileShader(fragment);
  295. // print compile errors if any
  296. glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
  297. if (!success)
  298. {
  299. glGetShaderInfoLog(fragment, 512, NULL, infoLog);
  300. std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
  301. };
  302.  
  303. // shader Program
  304. GLuint ID = glCreateProgram();
  305. glAttachShader(ID, vertex);
  306. glAttachShader(ID, fragment);
  307. glLinkProgram(ID);
  308. // print linking errors if any
  309. glGetProgramiv(ID, GL_LINK_STATUS, &success);
  310. if (!success)
  311. {
  312. glGetProgramInfoLog(ID, 512, NULL, infoLog);
  313. std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
  314. }
  315.  
  316. // delete the shaders as they're linked into our program now and no longer necessary
  317. glDeleteShader(vertex);
  318. glDeleteShader(fragment);
  319.  
  320. return ID;
  321. }
Add Comment
Please, Sign In to add comment