YaBoi
YaBoi

Reputation: 11

Tetrahedron side not appearing, not winding order issue

Having problems getting the Green colored side to appear, initially thought it was a culling or winding order issue but could use a fresh perspective into what I'm doing wrong.

The issue is currently the greenside but will always be whatever triangle is drawn first.

Libaries used: Glew & Glfw3

#include <iostream>

#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"

const char* WINDOW_TITLE = "Practice";
const GLint WINDOW_WIDTH = 600;
const GLint WINDOW_HEIGHT = 600;

static void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}

int main()
{
    GLFWwindow* window;

    if (!glfwInit()) {
        std::cout << "GLFW initialisation failed." << std::endl;
        return -1;
    }

    glfwSetErrorCallback(error_callback);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

    window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);

    if (!window)
    {
        std::cout << "GLFW failed to create window." << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (err != GLEW_OK)
    {
        /* Problem: glewInit failed, something is seriously wrong */
        std::cout << "GLEW initialisation failed: " << glewGetErrorString(err) << std::endl;
        return -1;
    }
    std::cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;

    /* Choose a colour to clear the screen (RGBA range 0-1) */
    glClearColor(0.4f, 0.5f, 0.6f, 1.0f);

    glEnable(GL_CULL_FACE | GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glCullFace(GL_BACK);

    /* Loop until the user closes the window */

    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);

        glRotatef(.5f, 1.f, 1.f, 1.f);

        float r = 0.5f; 
        glBegin(GL_TRIANGLES);

1,0,0 Facing (GREEN) - Face not appearing

        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(r, r, -r);
        glVertex3f(r, -r, r);
        glVertex3f(-r, -r, -r);

0,1,0 Facing (RED)

        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(r, r, -r);
        glVertex3f(-r, -r, -r);
        glVertex3f(-r, r, r);

0,0,1 Facing (BLUE)

        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-r, r, r);
        glVertex3f(-r, -r, -r);
        glVertex3f(r, -r, r);

1,1,1 Facing (WHITE)

        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(r, r, -r);
        glVertex3f(-r, r, r);
        glVertex3f(r, -r, r);

        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Upvotes: 1

Views: 100

Answers (1)

Rabbid76
Rabbid76

Reputation: 211166

GL_CULL_FACE and GL_DEPTH_TEST are enumeration constants, do not specify a bits of a bitset. The value of GL_CULL_FACE is 0x0B44 and the value of GL_DEPTH_TEST is 0x0B71. Therefore, glEnable(GL_CULL_FACE | GL_DEPTH_TEST doesn't make any sense and will cause an GL_INVALID_ENUM error. The argument of glEnable must be a single enumeration constant. You have to enable each capability separately:

glEnable(GL_CULL_FACE | GL_DEPTH_TEST);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

Your front face have a clockwise winding order. The default winding order is counter-clockwise. Hence you need to change the specification of the orientation of front-facing polygons with glFrontFace:

glFrontFace(GL_CW);

Upvotes: 2

Related Questions