#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <learnopengl/shader_m.h>
#include <learnopengl/camera.h>
#include <iostream>
#include <vector>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int modifiers);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int useWireframe = 0;
int displayGrayscale = 0;
Camera camera(glm::vec3(67.0f, 627.5f, 169.9f),
glm::vec3(0.0f, 1.0f, 0.0f),
-128.1f, -42.4f);
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL: Terrain CPU", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
Shader heightMapShader("8.3.cpuheight.vs","8.3.cpuheight.fs");
stbi_set_flip_vertically_on_load(true);
int width, height, nrChannels;
unsigned char *data = stbi_load("heightmaps/iceland_heightmap.png", &width, &height, &nrChannels, 0);
if (data)
{
std::cout << "Loaded heightmap of size " << height << " x " << width << std::endl;
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
std::vector<float> vertices;
float yScale = 64.0f / 256.0f, yShift = 16.0f;
int rez = 1;
unsigned bytePerPixel = nrChannels;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
unsigned char* pixelOffset = data + (j + width * i) * bytePerPixel;
unsigned char y = pixelOffset[0];
vertices.push_back( -height/2.0f + height*i/(float)height );
vertices.push_back( (int) y * yScale - yShift);
vertices.push_back( -width/2.0f + width*j/(float)width );
}
}
std::cout << "Loaded " << vertices.size() / 3 << " vertices" << std::endl;
stbi_image_free(data);
std::vector<unsigned> indices;
for(unsigned i = 0; i < height-1; i += rez)
{
for(unsigned j = 0; j < width; j += rez)
{
for(unsigned k = 0; k < 2; k++)
{
indices.push_back(j + width * (i + k*rez));
}
}
}
std::cout << "Loaded " << indices.size() << " indices" << std::endl;
const int numStrips = (height-1)/rez;
const int numTrisPerStrip = (width/rez)*2-2;
std::cout << "Created lattice of " << numStrips << " strips with " << numTrisPerStrip << " triangles each" << std::endl;
std::cout << "Created " << numStrips * numTrisPerStrip << " triangles total" << std::endl;
unsigned int terrainVAO, terrainVBO, terrainIBO;
glGenVertexArrays(1, &terrainVAO);
glBindVertexArray(terrainVAO);
glGenBuffers(1, &terrainVBO);
glBindBuffer(GL_ARRAY_BUFFER, terrainVBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &terrainIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned), &indices[0], GL_STATIC_DRAW);
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
heightMapShader.use();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100000.0f);
glm::mat4 view = camera.GetViewMatrix();
heightMapShader.setMat4("projection", projection);
heightMapShader.setMat4("view", view);
glm::mat4 model = glm::mat4(1.0f);
heightMapShader.setMat4("model", model);
glBindVertexArray(terrainVAO);
for(unsigned strip = 0; strip < numStrips; strip++)
{
glDrawElements(GL_TRIANGLE_STRIP,
numTrisPerStrip+2,
GL_UNSIGNED_INT,
(void*)(sizeof(unsigned) * (numTrisPerStrip+2) * strip));
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &terrainVAO);
glDeleteBuffers(1, &terrainVBO);
glDeleteBuffers(1, &terrainIBO);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int modifiers)
{
if(action == GLFW_PRESS)
{
switch(key)
{
case GLFW_KEY_SPACE:
useWireframe = 1 - useWireframe;
break;
case GLFW_KEY_G:
displayGrayscale = 1 - displayGrayscale;
break;
default:
break;
}
}
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
HI