From 23ac7ee63498551ad0f4c7d97ea0b4dd74b28ae0 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Wed, 5 Mar 2025 23:18:09 +0100 Subject: [PATCH] feat(renderer): backface culling --- core/src/rendering/defaultmeshes.cpp | 75 ++++++++++++++-------------- core/src/rendering/renderer.cpp | 7 ++- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/core/src/rendering/defaultmeshes.cpp b/core/src/rendering/defaultmeshes.cpp index 8e8b1f1..703cd7c 100644 --- a/core/src/rendering/defaultmeshes.cpp +++ b/core/src/rendering/defaultmeshes.cpp @@ -2,42 +2,43 @@ static float CUBE_VERTICES[] = { // positions // normals // texture coords - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0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, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + 0.5, -0.5, -0.5, -0.0, -0.0, -1.0, 1.0, 0.0, + 0.5, 0.5, -0.5, -0.0, -0.0, -1.0, 1.0, 1.0, + -0.5, 0.5, -0.5, -0.0, -0.0, -1.0, 0.0, 1.0, + 0.5, -0.5, 0.5, 1.0, -0.0, -0.0, 0.0, 0.0, + 0.5, 0.5, 0.5, 1.0, -0.0, -0.0, 0.0, 1.0, + 0.5, 0.5, -0.5, 1.0, -0.0, -0.0, 1.0, 1.0, + -0.5, -0.5, 0.5, -0.0, -1.0, -0.0, 0.0, 0.0, + 0.5, -0.5, 0.5, -0.0, -1.0, -0.0, 1.0, 0.0, + 0.5, -0.5, -0.5, -0.0, -1.0, -0.0, 1.0, 1.0, + -0.5, -0.5, -0.5, -1.0, -0.0, -0.0, 1.0, 0.0, + -0.5, 0.5, -0.5, -1.0, -0.0, -0.0, 1.0, 1.0, + -0.5, 0.5, 0.5, -1.0, -0.0, -0.0, 0.0, 1.0, + -0.5, -0.5, 0.5, -0.0, -0.0, 1.0, 0.0, 0.0, + -0.5, 0.5, 0.5, -0.0, -0.0, 1.0, 0.0, 1.0, + 0.5, 0.5, 0.5, -0.0, -0.0, 1.0, 1.0, 1.0, + 0.5, 0.5, 0.5, -0.0, 1.0, -0.0, 1.0, 0.0, + -0.5, 0.5, 0.5, -0.0, 1.0, -0.0, 0.0, 0.0, + -0.5, 0.5, -0.5, -0.0, 1.0, -0.0, 0.0, 1.0, + -0.5, -0.5, -0.5, -0.0, -0.0, -1.0, 0.0, 0.0, + 0.5, -0.5, -0.5, -0.0, -0.0, -1.0, 1.0, 0.0, + -0.5, 0.5, -0.5, -0.0, -0.0, -1.0, 0.0, 1.0, + 0.5, -0.5, -0.5, 1.0, -0.0, -0.0, 1.0, 0.0, + 0.5, -0.5, 0.5, 1.0, -0.0, -0.0, 0.0, 0.0, + 0.5, 0.5, -0.5, 1.0, -0.0, -0.0, 1.0, 1.0, + -0.5, -0.5, -0.5, -0.0, -1.0, -0.0, 0.0, 1.0, + -0.5, -0.5, 0.5, -0.0, -1.0, -0.0, 0.0, 0.0, + 0.5, -0.5, -0.5, -0.0, -1.0, -0.0, 1.0, 1.0, + -0.5, -0.5, 0.5, -1.0, -0.0, -0.0, 0.0, 0.0, + -0.5, -0.5, -0.5, -1.0, -0.0, -0.0, 1.0, 0.0, + -0.5, 0.5, 0.5, -1.0, -0.0, -0.0, 0.0, 1.0, + 0.5, -0.5, 0.5, -0.0, -0.0, 1.0, 1.0, 0.0, + -0.5, -0.5, 0.5, -0.0, -0.0, 1.0, 0.0, 0.0, + 0.5, 0.5, 0.5, -0.0, -0.0, 1.0, 1.0, 1.0, + 0.5, 0.5, -0.5, -0.0, 1.0, -0.0, 1.0, 1.0, + 0.5, 0.5, 0.5, -0.0, 1.0, -0.0, 1.0, 0.0, + -0.5, 0.5, -0.5, -0.0, 1.0, -0.0, 0.0, 1.0, + }; static float SPHERE_VERTICES[] = { @@ -1191,4 +1192,4 @@ for idx, vert in enumerate(obj.data.vertices): print(FOOTER) -*/ \ No newline at end of file +*/ diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index f536536..7a94125 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -67,6 +67,9 @@ void renderInit(GLFWwindow* window, int width, int height) { void renderParts() { glDepthMask(GL_TRUE); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CW); // Use shader shader->use(); @@ -129,12 +132,13 @@ void renderParts() { shader->set("texScale", part->size); CUBE_MESH->bind(); - glDrawArrays(GL_TRIANGLES, 0, 36); + glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount); } } void renderSkyBox() { glDepthMask(GL_FALSE); + glCullFace(GL_FRONT); skyboxShader->use(); @@ -155,6 +159,7 @@ void renderHandles() { if (!editorToolHandles->adornee.has_value() || !editorToolHandles->active) return; glDepthMask(GL_TRUE); + glCullFace(GL_BACK); // Use shader handleShader->use();