added mesh culling

This commit is contained in:
2025-11-30 06:19:38 +03:00
parent 57ba809076
commit 6414922884

View File

@@ -21,16 +21,8 @@ using omath::Vector3;
// Your 4x4 matrix type // Your 4x4 matrix type
using Mat4x4 = omath::opengl_engine::Mat4X4; using Mat4x4 = omath::opengl_engine::Mat4X4;
// Rotation angles for the Mesh (whatever you use for rotation) // Rotation angles for the Mesh
using RotationAngles = using RotationAngles = omath::opengl_engine::ViewAngles;
omath::opengl_engine::ViewAngles; // TODO: if you have a dedicated angle type for mesh rotation, use it
// View angles type for camera trait
using ViewAngles = RotationAngles; // TODO: if your camera uses a different view-angle type, change this
// Your trait types (that satisfy the concepts declared in your headers)
using CameraTrait = /* TODO: your camera engine trait type here */ void;
using MeshTrait = /* TODO: your mesh rotation trait type here */ void;
// For brevity, alias the templates instantiated with your types // For brevity, alias the templates instantiated with your types
using VertexType = omath::primitives::Vertex<Vector3<float>>; using VertexType = omath::primitives::Vertex<Vector3<float>>;
@@ -47,6 +39,7 @@ layout (location = 2) in vec3 aUv;
uniform mat4 uMVP; uniform mat4 uMVP;
uniform mat4 uModel; uniform mat4 uModel;
out vec3 vNormal; out vec3 vNormal;
out vec3 vUv; out vec3 vUv;
@@ -159,10 +152,16 @@ int main()
return -1; return -1;
} }
// ---------- GL state ----------
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
// Face culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); // cull back faces
glFrontFace(GL_CCW); // counter-clockwise is front
// ---------- Build Cube Mesh (CPU side) ---------- // ---------- Build Cube Mesh (CPU side) ----------
std::vector<omath::primitives::Vertex<>> vbo; std::vector<VertexType> vbo;
vbo.reserve(8); vbo.reserve(8);
Vector3<float> p000{-0.5f, -0.5f, -0.5f}; Vector3<float> p000{-0.5f, -0.5f, -0.5f};
@@ -192,9 +191,9 @@ int main()
vbo.push_back(v6); // 6 vbo.push_back(v6); // 6
vbo.push_back(v7); // 7 vbo.push_back(v7); // 7
std::vector<Vector3<std::uint32_t>> ebo;
ebo.reserve(12);
using Idx = Vector3<std::uint32_t>; using Idx = Vector3<std::uint32_t>;
std::vector<Idx> ebo;
ebo.reserve(12);
// front (z+) // front (z+)
ebo.emplace_back(1, 5, 7); ebo.emplace_back(1, 5, 7);
@@ -238,7 +237,7 @@ int main()
glBufferData(GL_ARRAY_BUFFER, cube.m_vertex_buffer.size() * sizeof(VertexType), cube.m_vertex_buffer.data(), glBufferData(GL_ARRAY_BUFFER, cube.m_vertex_buffer.size() * sizeof(VertexType), cube.m_vertex_buffer.data(),
GL_STATIC_DRAW); GL_STATIC_DRAW);
// flatten Ebo to GL indices // flatten EBO to GL indices
std::vector<GLuint> flatIndices; std::vector<GLuint> flatIndices;
flatIndices.reserve(cube.m_vertex_array_object.size() * 3); flatIndices.reserve(cube.m_vertex_array_object.size() * 3);
for (const auto& tri : cube.m_vertex_array_object) for (const auto& tri : cube.m_vertex_array_object)
@@ -271,13 +270,14 @@ int main()
float nearPlane = 0.1f; float nearPlane = 0.1f;
float farPlane = 100.f; float farPlane = 100.f;
auto fov = omath::projection::FieldOfView::from_degrees(90.f); auto fov = omath::projection::FieldOfView::from_degrees(90.f);
// NOTE: you must replace CameraTrait alias above with your real type
MyCamera camera{camPos, {}, viewPort, fov, nearPlane, farPlane}; MyCamera camera{camPos, {}, viewPort, fov, nearPlane, farPlane};
// ---------- Shader ---------- // ---------- Shader ----------
GLuint shaderProgram = createShaderProgram(); GLuint shaderProgram = createShaderProgram();
GLint uMvpLoc = glGetUniformLocation(shaderProgram, "uMVP"); GLint uMvpLoc = glGetUniformLocation(shaderProgram, "uMVP");
GLint uModel = glGetUniformLocation(shaderProgram, "uModel"); GLint uModel = glGetUniformLocation(shaderProgram, "uModel");
static float old_frame_time = glfwGetTime(); static float old_frame_time = glfwGetTime();
// ---------- Main loop ---------- // ---------- Main loop ----------
@@ -288,7 +288,8 @@ int main()
float currentTime = glfwGetTime(); float currentTime = glfwGetTime();
float deltaTime = currentTime - old_frame_time; float deltaTime = currentTime - old_frame_time;
old_frame_time = currentTime; old_frame_time = currentTime;
int fbW, fbH;
int fbW = 0, fbH = 0;
glfwGetFramebufferSize(window, &fbW, &fbH); glfwGetFramebufferSize(window, &fbW, &fbH);
glViewport(0, 0, fbW, fbH); glViewport(0, 0, fbW, fbH);
@@ -300,7 +301,7 @@ int main()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
RotationAngles rot = cube.get_rotation_angles(); RotationAngles rot = cube.get_rotation_angles();
rot.yaw += omath::opengl_engine::YawAngle::from_degrees(40.f * deltaTime); rot.yaw += omath::opengl_engine::YawAngle ::from_degrees(40.f * deltaTime);
rot.roll += omath::opengl_engine::RollAngle::from_degrees(40.f * deltaTime); rot.roll += omath::opengl_engine::RollAngle::from_degrees(40.f * deltaTime);
if (rot.pitch.as_degrees() == 90.f) if (rot.pitch.as_degrees() == 90.f)
@@ -310,15 +311,17 @@ int main()
const Mat4x4& viewProj = camera.get_view_projection_matrix(); const Mat4x4& viewProj = camera.get_view_projection_matrix();
const auto& model = cube.get_to_world_matrix(); const auto& model = cube.get_to_world_matrix();
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
// Send matrix to GPU // Send matrices to GPU
const float* mvpPtr = viewProj.raw_array().data(); // assumes column-major float[16] const float* mvpPtr = viewProj.raw_array().data();
const float* modelPtr = model.raw_array().data();
glUniformMatrix4fv(uMvpLoc, 1, GL_FALSE, mvpPtr); glUniformMatrix4fv(uMvpLoc, 1, GL_FALSE, mvpPtr);
const float* modelPtr = model.raw_array().data(); // assumes column-major float[16]
glBindVertexArray(VAO);
glUniformMatrix4fv(uModel, 1, GL_FALSE, modelPtr); glUniformMatrix4fv(uModel, 1, GL_FALSE, modelPtr);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(flatIndices.size()), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(flatIndices.size()), GL_UNSIGNED_INT, nullptr);
glfwSwapBuffers(window); glfwSwapBuffers(window);