Skip to content

Commit 6414922

Browse files
committed
added mesh culling
1 parent 57ba809 commit 6414922

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

examples/example_glfw3.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,8 @@ using omath::Vector3;
2121
// Your 4x4 matrix type
2222
using Mat4x4 = omath::opengl_engine::Mat4X4;
2323

24-
// Rotation angles for the Mesh (whatever you use for rotation)
25-
using RotationAngles =
26-
omath::opengl_engine::ViewAngles; // TODO: if you have a dedicated angle type for mesh rotation, use it
27-
28-
// View angles type for camera trait
29-
using ViewAngles = RotationAngles; // TODO: if your camera uses a different view-angle type, change this
30-
31-
// Your trait types (that satisfy the concepts declared in your headers)
32-
using CameraTrait = /* TODO: your camera engine trait type here */ void;
33-
using MeshTrait = /* TODO: your mesh rotation trait type here */ void;
24+
// Rotation angles for the Mesh
25+
using RotationAngles = omath::opengl_engine::ViewAngles;
3426

3527
// For brevity, alias the templates instantiated with your types
3628
using VertexType = omath::primitives::Vertex<Vector3<float>>;
@@ -47,6 +39,7 @@ layout (location = 2) in vec3 aUv;
4739
4840
uniform mat4 uMVP;
4941
uniform mat4 uModel;
42+
5043
out vec3 vNormal;
5144
out vec3 vUv;
5245
@@ -159,10 +152,16 @@ int main()
159152
return -1;
160153
}
161154

155+
// ---------- GL state ----------
162156
glEnable(GL_DEPTH_TEST);
163157

158+
// Face culling
159+
glEnable(GL_CULL_FACE);
160+
glCullFace(GL_BACK); // cull back faces
161+
glFrontFace(GL_CCW); // counter-clockwise is front
162+
164163
// ---------- Build Cube Mesh (CPU side) ----------
165-
std::vector<omath::primitives::Vertex<>> vbo;
164+
std::vector<VertexType> vbo;
166165
vbo.reserve(8);
167166

168167
Vector3<float> p000{-0.5f, -0.5f, -0.5f};
@@ -192,9 +191,9 @@ int main()
192191
vbo.push_back(v6); // 6
193192
vbo.push_back(v7); // 7
194193

195-
std::vector<Vector3<std::uint32_t>> ebo;
196-
ebo.reserve(12);
197194
using Idx = Vector3<std::uint32_t>;
195+
std::vector<Idx> ebo;
196+
ebo.reserve(12);
198197

199198
// front (z+)
200199
ebo.emplace_back(1, 5, 7);
@@ -238,7 +237,7 @@ int main()
238237
glBufferData(GL_ARRAY_BUFFER, cube.m_vertex_buffer.size() * sizeof(VertexType), cube.m_vertex_buffer.data(),
239238
GL_STATIC_DRAW);
240239

241-
// flatten Ebo to GL indices
240+
// flatten EBO to GL indices
242241
std::vector<GLuint> flatIndices;
243242
flatIndices.reserve(cube.m_vertex_array_object.size() * 3);
244243
for (const auto& tri : cube.m_vertex_array_object)
@@ -271,13 +270,14 @@ int main()
271270
float nearPlane = 0.1f;
272271
float farPlane = 100.f;
273272
auto fov = omath::projection::FieldOfView::from_degrees(90.f);
274-
// NOTE: you must replace CameraTrait alias above with your real type
273+
275274
MyCamera camera{camPos, {}, viewPort, fov, nearPlane, farPlane};
276275

277276
// ---------- Shader ----------
278277
GLuint shaderProgram = createShaderProgram();
279278
GLint uMvpLoc = glGetUniformLocation(shaderProgram, "uMVP");
280279
GLint uModel = glGetUniformLocation(shaderProgram, "uModel");
280+
281281
static float old_frame_time = glfwGetTime();
282282

283283
// ---------- Main loop ----------
@@ -288,7 +288,8 @@ int main()
288288
float currentTime = glfwGetTime();
289289
float deltaTime = currentTime - old_frame_time;
290290
old_frame_time = currentTime;
291-
int fbW, fbH;
291+
292+
int fbW = 0, fbH = 0;
292293
glfwGetFramebufferSize(window, &fbW, &fbH);
293294
glViewport(0, 0, fbW, fbH);
294295

@@ -300,7 +301,7 @@ int main()
300301
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
301302

302303
RotationAngles rot = cube.get_rotation_angles();
303-
rot.yaw += omath::opengl_engine::YawAngle::from_degrees(40.f * deltaTime);
304+
rot.yaw += omath::opengl_engine::YawAngle ::from_degrees(40.f * deltaTime);
304305
rot.roll += omath::opengl_engine::RollAngle::from_degrees(40.f * deltaTime);
305306

306307
if (rot.pitch.as_degrees() == 90.f)
@@ -310,15 +311,17 @@ int main()
310311

311312
const Mat4x4& viewProj = camera.get_view_projection_matrix();
312313
const auto& model = cube.get_to_world_matrix();
314+
313315
glUseProgram(shaderProgram);
314316

315-
// Send matrix to GPU
316-
const float* mvpPtr = viewProj.raw_array().data(); // assumes column-major float[16]
317+
// Send matrices to GPU
318+
const float* mvpPtr = viewProj.raw_array().data();
319+
const float* modelPtr = model.raw_array().data();
317320

318321
glUniformMatrix4fv(uMvpLoc, 1, GL_FALSE, mvpPtr);
319-
const float* modelPtr = model.raw_array().data(); // assumes column-major float[16]
320-
glBindVertexArray(VAO);
321322
glUniformMatrix4fv(uModel, 1, GL_FALSE, modelPtr);
323+
324+
glBindVertexArray(VAO);
322325
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(flatIndices.size()), GL_UNSIGNED_INT, nullptr);
323326

324327
glfwSwapBuffers(window);

0 commit comments

Comments
 (0)