Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --- BASIC.VERT
- #version 330 core
- layout (location = 0) in vec3 pos;
- uniform mat4 view;
- uniform mat4 proj;
- void main() {
- gl_Position = proj * view * vec4(pos, 1.0);
- }
- // --- BASIC.FRAG
- #version 330 core
- out vec4 color;
- void main() {
- color = vec4(1.0);
- }
- // --- SOURCE.CPP
- #include <iostream>
- #include <vector>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include "Shader.h"
- #include "PerlinNoise2D.h"
- struct Vector3f {
- float x, y, z;
- };
- int main() {
- // Setup GLFW
- if (!glfwInit())
- std::cout << "GLFW could not init!" << std::endl;
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- GLFWwindow* window = glfwCreateWindow(800, 600, "Terrain Generator", NULL, NULL);
- if (!window)
- std::cout << "GLFW could not create window!" << std::endl;
- glfwMakeContextCurrent(window);
- // Setup GLEW
- if (glewInit() != 0)
- std::cout << "GLEW could not init!" << std::endl;
- glEnable(GL_DEPTH_TEST);
- // Additional classes
- Shader shader("basic.vert", "basic.frag");
- PerlinNoise2D noise;
- std::vector<Vector3f> points;
- std::vector<unsigned int> connect;
- for (int z = 0; z < 16; z++) {
- for (int x = 0; x < 16; x++) {
- Vector3f vec;
- vec.x = x;
- vec.z = z;
- vec.y = 0;//noise.getNoise(x, z) * 100;
- points.push_back(vec);
- }
- }
- for (unsigned int i = 0; i < 15; i++) {
- connect.push_back(i);
- connect.push_back(i + 1);
- connect.push_back(i + 16);
- }
- for (auto i : connect) {
- std::cout << points[i].x << ", " << points[i].y << ", " << points[i].z << std::endl;
- }
- unsigned vao, vbo, ebo;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3f) * points.size(), &points[0], GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
- glGenBuffers(1, &ebo);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * connect.size(), &connect[0], GL_STATIC_DRAW);
- glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
- shader.use();
- glm::mat4 view;
- glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)800 / (float)600, -0.1f, 100.0f);
- shader.setMat4("proj", proj);
- shader.setMat4("view", view);
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
- //glDrawArrays(GL_TRIANGLES, 0, 3);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glDeleteBuffers(1, &ebo);
- glDeleteBuffers(1, &vbo);
- glDeleteVertexArrays(1, &vao);
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement