Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. // --- BASIC.VERT
  2. #version 330 core
  3. layout (location = 0) in vec3 pos;
  4.  
  5. uniform mat4 view;
  6. uniform mat4 proj;
  7.  
  8. void main() {
  9.     gl_Position = proj * view * vec4(pos, 1.0);
  10. }
  11.  
  12. // --- BASIC.FRAG
  13. #version 330 core
  14. out vec4 color;
  15.  
  16. void main() {
  17.     color = vec4(1.0);
  18. }
  19.  
  20. // --- SOURCE.CPP
  21. #include <iostream>
  22. #include <vector>
  23. #include <GL/glew.h>
  24. #include <GLFW/glfw3.h>
  25. #include <glm/glm.hpp>
  26. #include <glm/gtc/matrix_transform.hpp>
  27.  
  28. #include "Shader.h"
  29. #include "PerlinNoise2D.h"
  30.  
  31. struct Vector3f {
  32.     float x, y, z;
  33. };
  34.  
  35. int main() {
  36.     // Setup GLFW
  37.     if (!glfwInit())
  38.         std::cout << "GLFW could not init!" << std::endl;
  39.  
  40.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  41.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  42.    
  43.     GLFWwindow* window = glfwCreateWindow(800, 600, "Terrain Generator", NULL, NULL);
  44.    
  45.     if (!window)
  46.         std::cout << "GLFW could not create window!" << std::endl;
  47.  
  48.     glfwMakeContextCurrent(window);
  49.  
  50.     // Setup GLEW
  51.     if (glewInit() != 0)
  52.         std::cout << "GLEW could not init!" << std::endl;
  53.  
  54.     glEnable(GL_DEPTH_TEST);
  55.  
  56.     // Additional classes
  57.     Shader shader("basic.vert", "basic.frag");
  58.     PerlinNoise2D noise;
  59.  
  60.     std::vector<Vector3f> points;
  61.     std::vector<unsigned int> connect;
  62.  
  63.     for (int z = 0; z < 16; z++) {
  64.         for (int x = 0; x < 16; x++) {
  65.             Vector3f vec;
  66.             vec.x = x;
  67.             vec.z = z;
  68.             vec.y = 0;//noise.getNoise(x, z) * 100;
  69.             points.push_back(vec);
  70.         }
  71.     }
  72.  
  73.     for (unsigned int i = 0; i < 15; i++) {
  74.         connect.push_back(i);
  75.         connect.push_back(i + 1);
  76.         connect.push_back(i + 16);
  77.     }
  78.  
  79.     for (auto i : connect) {
  80.         std::cout << points[i].x << ", " << points[i].y << ", " << points[i].z << std::endl;
  81.     }
  82.  
  83.     unsigned vao, vbo, ebo;
  84.     glGenVertexArrays(1, &vao);
  85.     glBindVertexArray(vao);
  86.  
  87.     glGenBuffers(1, &vbo);
  88.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  89.     glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3f) * points.size(), &points[0], GL_STATIC_DRAW);
  90.  
  91.     glEnableVertexAttribArray(0);
  92.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
  93.  
  94.     glGenBuffers(1, &ebo);
  95.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  96.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * connect.size(), &connect[0], GL_STATIC_DRAW);
  97.  
  98.     glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  99.     shader.use();
  100.     glm::mat4 view;
  101.     glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)800 / (float)600, -0.1f, 100.0f);
  102.     shader.setMat4("proj", proj);
  103.     shader.setMat4("view", view);
  104.  
  105.     while (!glfwWindowShouldClose(window)) {
  106.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  107.        
  108.         glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
  109.         //glDrawArrays(GL_TRIANGLES, 0, 3);
  110.        
  111.         glfwSwapBuffers(window);
  112.         glfwPollEvents();
  113.     }
  114.  
  115.     glDeleteBuffers(1, &ebo);
  116.     glDeleteBuffers(1, &vbo);
  117.     glDeleteVertexArrays(1, &vao);
  118.  
  119.     glfwDestroyWindow(window);
  120.     glfwTerminate();
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement