Add dynamic patch grid resolution
This commit is contained in:
parent
5212d5aa19
commit
a45473c77e
|
@ -8,12 +8,18 @@
|
||||||
|
|
||||||
#include "fbo.h"
|
#include "fbo.h"
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::vec2 texCoord;
|
||||||
|
};
|
||||||
|
|
||||||
class Scene {
|
class Scene {
|
||||||
public:
|
public:
|
||||||
Scene(int width, int height);
|
Scene(int width, int height);
|
||||||
~Scene();
|
~Scene();
|
||||||
|
|
||||||
void Init();
|
void Init();
|
||||||
|
void GenerateGrid(int divisions, std::vector<Vertex>& verts, std::vector<GLuint>& inds);
|
||||||
void Display(GLFWwindow* window);
|
void Display(GLFWwindow* window);
|
||||||
void DrawGui(GLFWwindow* window);
|
void DrawGui(GLFWwindow* window);
|
||||||
void Idle();
|
void Idle();
|
||||||
|
|
|
@ -39,6 +39,9 @@ const std::string kHGTPath = "hgt/N02E016.hgt";
|
||||||
|
|
||||||
GLuint tex_id = -1;
|
GLuint tex_id = -1;
|
||||||
|
|
||||||
|
std::vector<Vertex> vertices;
|
||||||
|
std::vector<GLuint> indices;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Scene::Scene(int width, int height)
|
Scene::Scene(int width, int height)
|
||||||
|
@ -70,7 +73,7 @@ void Scene::Init() {
|
||||||
// GL_DEPTH_TEST causes artifacts in meshes made up of multiple triangles
|
// GL_DEPTH_TEST causes artifacts in meshes made up of multiple triangles
|
||||||
// disabling for now
|
// disabling for now
|
||||||
//glEnable(GL_DEPTH_TEST);
|
//glEnable(GL_DEPTH_TEST);
|
||||||
//glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
fbo_.Init(window_width, window_height);
|
fbo_.Init(window_width, window_height);
|
||||||
post_fbo_.Init(window_width, window_height);
|
post_fbo_.Init(window_width, window_height);
|
||||||
|
@ -87,33 +90,70 @@ void Scene::Init() {
|
||||||
Uniforms::Init();
|
Uniforms::Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Scene::GenerateGrid(int divisions, std::vector<Vertex>& verts, std::vector<GLuint>& inds) {
|
||||||
|
float step = 1.0f / static_cast<float>(divisions);
|
||||||
|
float halfSize = 0.5f;
|
||||||
|
|
||||||
|
// Create vertices for patches
|
||||||
|
for (int i = 0; i <= divisions; i++) {
|
||||||
|
for (int j = 0; j <= divisions; j++) {
|
||||||
|
float x = -halfSize + j * step;
|
||||||
|
float y = -halfSize + i * step;
|
||||||
|
|
||||||
|
Vertex vertex;
|
||||||
|
vertex.position = glm::vec3(x, y, 0.0f);
|
||||||
|
vertex.texCoord = glm::vec2(static_cast<float>(j) / divisions, static_cast<float>(i) / divisions);
|
||||||
|
|
||||||
|
verts.push_back(vertex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create indices for patches
|
||||||
|
for (int i = 0; i < divisions; i++) {
|
||||||
|
for (int j = 0; j < divisions; j++) {
|
||||||
|
int bottomLeft = i * (divisions + 1) + j;
|
||||||
|
int bottomRight = bottomLeft + 1;
|
||||||
|
int topLeft = bottomLeft + (divisions + 1);
|
||||||
|
int topRight = topLeft + 1;
|
||||||
|
|
||||||
|
inds.push_back(topLeft);
|
||||||
|
inds.push_back(topRight);
|
||||||
|
inds.push_back(bottomRight);
|
||||||
|
inds.push_back(bottomLeft);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Currently creates a test triangle and initializes its buffers
|
// Currently creates a test triangle and initializes its buffers
|
||||||
void Scene::InitBuffers() {
|
void Scene::InitBuffers() {
|
||||||
GLuint patch_vbo;
|
int divisions = 2; // Number of divisions along one axis of the grid
|
||||||
|
GenerateGrid(divisions, vertices, indices);
|
||||||
float patchVertices[] = {
|
|
||||||
// Positions // Texture Coords
|
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // Bottom-left
|
|
||||||
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Bottom-right
|
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // Top-right
|
|
||||||
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // Top-left
|
|
||||||
};
|
|
||||||
|
|
||||||
|
// Create and bind VAO, VBO, and EBO, and pass the data to OpenGL
|
||||||
|
GLuint vao, vbo, ebo;
|
||||||
glGenVertexArrays(1, &vao_);
|
glGenVertexArrays(1, &vao_);
|
||||||
glGenBuffers(1, &patch_vbo);
|
glGenBuffers(1, &vbo);
|
||||||
|
glGenBuffers(1, &ebo);
|
||||||
|
|
||||||
glBindVertexArray(vao_);
|
glBindVertexArray(vao_);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, patch_vbo);
|
// Bind and fill the vertex buffer
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(patchVertices), &patchVertices, GL_STATIC_DRAW);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
// Position attribute
|
// Bind and fill the element buffer
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// Define vertex position attribute
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
|
||||||
|
|
||||||
// Texture coordinate attribute
|
// Define texture coordinate attribute
|
||||||
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
||||||
|
|
||||||
|
// Unbind VAO
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,7 +224,7 @@ void Scene::Display(GLFWwindow* window) {
|
||||||
|
|
||||||
glBindVertexArray(vao_);
|
glBindVertexArray(vao_);
|
||||||
glPatchParameteri(GL_PATCH_VERTICES, 4);
|
glPatchParameteri(GL_PATCH_VERTICES, 4);
|
||||||
glDrawArrays(GL_PATCHES, 0, 4);
|
glDrawElements(GL_PATCHES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue