0
\$\begingroup\$

I've been learning OpenGL over the past year or so, and I've never face this issue before, I've been refactoring the old version of my renderer to make it cleaner now that I'm more confident with OpenGL (or so I thought). I tried to render a simple cube to make sure I everything was working correctly in the new version. Even though the OpenGL initialization is identical to the old the cube renders incorrectly with the front faces see through. Like in this image: Cube

Here is the old renderer I made from LearnOpenGL.com: enter image description here

And here is the new OpenGL initialization function for the see through cube one:

gl_context = SDL_GL_CreateContext(window);
YE_CRITICAL_ASSERTION(gl_context != nullptr , "Failed to create OpenGL context");

int glad_init = gladLoadGLLoader(SDL_GL_GetProcAddress);
YE_CRITICAL_ASSERTION(glad_init != 0 , "Failed to initialize GLAD");

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.f);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);

// glEnable(GL_MULTISAMPLE);

glFrontFace(GL_CCW);

glClearColor(config.clear_color.r , config.clear_color.g , config.clear_color.b , config.clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (config.vsync) SDL_GL_SetSwapInterval(kOpenGLSwapInterval);

And the render function:

glViewport(0 , 0 , size.x , size.y);
glClearColor(config.clear_color.r , config.clear_color.g , config.clear_color.b , config.clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
texture->Bind();
shader->Bind();
cube_vao->Draw(YE::DrawMode::TRIANGLES);
shader->Unbind();
glActiveTexture(GL_TEXTURE0);
texture->Unbind();
SDL_GL_SwapWindow(window);

And for reference the old ones as well:

Initialize:

int sdl_init = SDL_Init(SDL_INIT_EVERYTHING);
YE_ASSERT(sdl_init == 0 , "SDL2 failed to initialize");
if (sdl_init != 0) return false;

// SDL_SetRelativeMouseMode(SDL_TRUE);

config.flags |= SDL_WINDOW_OPENGL;

window = SDL_CreateWindow(config.title.c_str() , config.x , config.y , config.w , config.h , config.flags);
YE_ASSERT(window != nullptr , "SDL2 failed to create window");
if (window == nullptr) return false;

        window_size = { config.w , config.h };
        window_pos = { config.x , config.y };
        framebuffer_size = window_size;

        rendering_to_scrn = config.render_to_screen;
        clear_color = { config.r , config.g , config.b , config.a };

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK , SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER , 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE , 8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS , 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES , 24);

SDL_SetWindowMinimumSize(window , 200 , 200);

context = SDL_GL_CreateContext(window);
YE_ASSERT(context != nullptr , "SDL2 failed to create OpenGL context");
if (context == nullptr) return false;
      
if (config.vsync)
            SDL_GL_SetSwapInterval(1);

int glad_init = gladLoadGLLoader(SDL_GL_GetProcAddress);
YE_ASSERT(glad_init != 0 , "GLAD failed to initialize");
if (glad_init == 0) return false;

glEnable(GL_DEPTH_TEST);
glEnable(GL_LEQUAL);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_MULTISAMPLE);

glClearColor(clear_color.r , clear_color.g , clear_color.b , clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

Draw:

glViewport(0 , 0 , static_cast<uint32_t>(window_size.x) , static_cast<uint32_t>(window_size.y));
glClearColor(clear_color.r , clear_color.g , clear_color.b , clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

// tons of shader calls for model and boxes and light and everything else
// ...

glActiveTexture(TEXTURE0);
cube_text->Bind();
cube_shader->Bind();
cube->Draw(); 
cube_shader->Unbind();
cube_text->Unbind();

SDL_GL_SwapWindow(window->GetWindow());

Also here are my glm calls for calculating the view/proj matrices:

New:

// not moving camera yet so no recalculating vectors
projection = glm::perspective(
    glm::radians(fov) , 
    static_cast<float>(win_size.x) / win_size.y , 
    clip.x , clip.y
); 
view = glm::lookAt(position , position + front , up); 

Old:

front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(front);

right = glm::normalize(glm::cross(front , world_up));
up = glm::normalize(glm::cross(right , front));

projection_matrix = glm::perspective(glm::radians(fov) , (16.f / 9.f) , clipping_near , clipping_far);
view_matrix = glm::lookAt(position , position + front , up);

also here are my shaders:

Vertex:

#version 460 core

layout (location = 0) in vec3 in_pos;
layout (location = 1) in vec3 in_color;
layout (location = 2) in vec2 in_texcoord;
layout (location = 3) in float in_opacity;

out vec3 frag_color;
out vec2 frag_texcoord;
out float frag_opacity;

uniform bool camera_active = false;

uniform mat4 model;
uniform mat4 proj;
uniform mat4 view;

void main() {
    gl_Position = model * vec4(in_pos , 1.0);
    if (camera_active) {
        gl_Position *= proj * view;
    }
    frag_color = in_color;
    frag_texcoord = in_texcoord;
}

Fragment:

#version 460 core

in vec3 frag_color;
in vec2 frag_texcoord;
in float frag_opacity;

out vec4 FragColor;

uniform vec3 view_pos;
uniform sampler2D tex0;
uniform sampler2D tex1;

void main() {
    FragColor = texture(tex0 , frag_texcoord);
}

I can not find what I'm doing wrong and would appreciate any help at all. As a note I have copied the cube vertex data from the old to the new so it is the same, and also the cube vertex data from learnOpenGL.com doesnt work either. The same problem occurs.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I was doing my matrix multiplications in the wrong order :/ its proj * view * model * vec4(pos , 1.0); for what I wanted.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .