PDA

View Full Version : Trying to render a square: all black screen.



Ploppz
03-28-2016, 08:48 AM
I've been staring at this code for too long... it's the usual problem that the screen is all black. The model loaded is a square with x,y,z,u,v (ply: http://sprunge.us/KHbZ). For now I don't use the uv coordinates and not the
texture either: just drawing red (trying to).
Here's the relevant code: https://bpaste.net/show/274aea06d970 and the main loop: https://bpaste.net/show/3ac88afda6f0

The camera is 3 units away from origin, looking at origin (radius = 3).
setFormat and createShaderProgram should work fine, so forget about them.

Here's a record of what my PLY-reading code adds to the VBO and EBO: https://bpaste.net/show/5b7e706c8351

It's all black no matter what I do. I tried disabling culling and depth buffer. I tried not uploading texture and removing the UV and UV_f attributes and the tex uniform, using this instead of setFormat as I needed to set stride to 5 floats:


GLint attrib = glGetAttribLocation(square_prg, "XYZ");
assert(attrib != -1);
glEnableVertexAttribArray(attrib);
glVertexAttribPointer(attrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)((intptr_t)(0)));


I've often had the problem that the screen is black at first, but it's usually not this hard to find the error - I really cannot see what I'm doing wrong.

Thanks in advance.

GClements
03-28-2016, 12:28 PM
I really cannot see what I'm doing wrong.

Nor can anyone else when all they have are disjointed snippets of code with no clue as to how they fit together.

E.g. the code in your post shows a call to glVertexAttribPointer() but you provide no clues as to where that fits into the code on bpaste. Is the VAO bound when you make that call? Is the VBO bound?

OpenGL has a very large amount of state (the concise documentation of this state in section 23 of the OpenGL 4.5 compatibility profile specification takes up 86 pages), and the behaviour of many OpenGL functions is affected by some part of that state, meaning that the precise effect of a function call cannot be known without first knowing the state at the time of the call.

Ploppz
03-29-2016, 12:34 AM
The last bit of code replaced the setFormat call in the ModelRenderer constructor, so yes it was indeed called before anything else than the program got created, and moving it to after the creation of the VAO and VBO, it now works! Thanks.