]> git.lizzy.rs Git - shadowclad.git/commitdiff
Some debug stuff to figure out normals and switch to flat shading
authoroutfrost <kotlet.bahn@gmail.com>
Thu, 4 Jun 2020 01:54:41 +0000 (03:54 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Thu, 4 Jun 2020 01:54:41 +0000 (03:54 +0200)
src/engine/render.c

index bd4456bd55637388e578991d5288c79510303527..b09f25bbd803ae91a2983c057f30c9b2c24a45cc 100644 (file)
@@ -6,7 +6,8 @@
 #include "geometry.h"
 #include "performance.h"
 
-#define RENDER_DEBUG_ 0
+#define SCENE_DEBUG_ 0
+#define RENDER_DEBUG_ 1
 
 float viewportAspectRatio = 1.0f;
 const Scene* cameraAnchor;
@@ -37,6 +38,8 @@ void initRender() {
        glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
        glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
+       
+       glShadeModel(GL_FLAT);
 }
 
 void renderFrame() {
@@ -68,9 +71,9 @@ static void renderScene(const Scene* scene, const Transform baseTransform) {
        glLoadTransposeMatrixf((const GLfloat*) &transform);
 
        glDisable(GL_LIGHTING);
-#if RENDER_DEBUG_
+#if SCENE_DEBUG_
        drawAxes();
-#endif // RENDER_DEBUG_
+#endif // SCENE_DEBUG_
        glEnable(GL_LIGHTING);
 
        if (scene->solid) {
@@ -127,6 +130,12 @@ static void drawAxes() {
        glEnd();
 }
 
+#if RENDER_DEBUG_
+static GLfloat ab(GLfloat a) {
+       return a < 0 ? -a : a;
+}
+#endif // RENDER_DEBUG_
+
 static void drawSolid(const Solid* solid) {
        if (solid == NULL) {
                return;
@@ -145,6 +154,25 @@ static void drawSolid(const Solid* solid) {
                for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
                        const Face face = mesh.faces[faceIndex];
                        
+#if RENDER_DEBUG_
+                       if (hasNormals) {
+                               glDisable(GL_LIGHTING);
+                               glDisable(GL_TEXTURE_2D);
+                               glBegin(GL_LINES);
+                               for (size_t i = 0; i < face.numIndices; ++i) {
+                                       size_t vertIndex = face.indices[i];
+                                       Vector3D vertex = mesh.vertices[vertIndex];
+                                       Vector3D normal = mesh.normals[vertIndex];
+                                       glColor3f(ab(normal.x), ab(normal.y), ab(normal.z));
+                                       glVertex3f(vertex.x, vertex.y, vertex.z);
+                                       glVertex3f(vertex.x + normal.x, vertex.y + normal.y, vertex.z + normal.z);
+                               }
+                               glEnd();
+                               glEnable(GL_TEXTURE_2D);
+                               glEnable(GL_LIGHTING);
+                       }
+#endif // RENDER_DEBUG_
+                       
                        GLenum faceMode;
                        switch (face.numIndices) {
                                case 1: faceMode = GL_POINTS; break;