this post was submitted on 10 Oct 2023
9 points (100.0% liked)

C++

1782 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
 

I was doing this part of this opengl tutorial and when trying to replicate the rectangle example it ends in a segfault, it starts up when i dont create the VAO (the VAObject variable) but when i do that it doesnt draw anything at all, i recently recompiled glfw to use glx. rectangle source: https://pastebin.com/LD8QPa47 canned_glfw_window.hpp: https://pastebin.com/0pkxSrBK canned_glfw_window.cpp: https://pastebin.com/an3GQcy1 (i put most of the logic in the last 2 files in order to be able to reuse it later)

top 9 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago (1 children)

This is really hard to follow. You will likely find it much easier to learn if you just write simple, top-to-bottom C style code as much as possible while you learn OpenGL and GLFW. You can abstract things into classes later when you have a better feel for the APIs, but right now you are shooting yourself in the foot...

Your immediate problem seems to be that you did not bind the vertex array after creating it. Changing the code to this:

VAObject vao(1);
vao.bind_vao();

prevents the crash for me -- however, there is also an issue with the vertices. With only the above change, the window is drawn blank. Changing vertices to match the example like this:

float vertices[] = {
     0.5f,  0.5f, 0.0f,  // top right
     0.5f, -0.5f, 0.0f,  // bottom right
    -0.5f, -0.5f, 0.0f,  // bottom left
    -0.5f,  0.5f, 0.0f   // top left
};

results in an orange rectangle drawn.

[–] [email protected] 1 points 1 year ago (1 children)

i tried doing it again in c-like code, and it failed again in the same fashion: https://pastebin.com/d8XzRzKq (PS: i left the GLFWWindow abstraction because i wanted to focus only in the render part), im starting to think that is a opengl thing, because the triangle example worked with opengl 2.1, even with the abstractions that i made (except for the fact that the triangle was fully white, because the shaders didnt compile)

[–] [email protected] 2 points 1 year ago (2 children)

You're incorrectly passing the shader type instead of the shader handle as the first argument to the glShaderSource calls. Also, you've put vbo instead of ebo for the glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...) call. If I change those, I get the orange rectangle again.

This may come in handy if it works on your implementation: https://www.khronos.org/opengl/wiki/Debug_Output

Note the example code at the very bottom of the page.

That helped me catch the shader type/handle issue. The vbo/ebo one I noticed by reading the code line by line and saying "wait, elements but you're passing vbo?!"

[–] [email protected] 1 points 1 year ago (1 children)

one more thing, i was debugging the triangle example (this one uses all the abstractions that i created) and using the logging functions it says me that the shader program is not linking, i added a debug function in the GLShaderProgram class and it says that the fragment shader does not have a main function, when i compile it without it it renders a white triangle, when i compile it with it it displays a blank window, can you tell me where is the problem?: https://pastebin.com/8VbyUNFR

[–] [email protected] 1 points 1 year ago (1 children)

Looks like you wrote std::shared_ptr fragment_shader(new GLShader(&vertex_shader_src, 1, GL_FRAGMENT_SHADER, NULL)); instead of passing &fragment_shader_src.

[–] [email protected] 1 points 1 year ago

yeah, i noticed that before, i already fixed it, thank you anyway

[–] [email protected] 1 points 1 year ago (1 children)

i have never been so happy to see a fucking orange rectangle in a black background in all my life ajhadhgdjh

[–] [email protected] 2 points 1 year ago (1 children)

Welcome to graphics programming! I know that feeling well...

[–] [email protected] 1 points 1 year ago