PDA

View Full Version : Strange effect rendering triangle



surazhsky
11-21-2016, 01:22 AM
Good day
I am trying to render one triangle with hard coded vertices in NDC coordinates:


const float vdata[] = {
-0.7f, -0.7f, 0.0f,
0.7f, -0.7f, 0.0f,
0.7f, 0.7f, 0.0f
};

unsigned short inds[] = {0,1,2};


with trivial shaders:
vert:

#version 330

layout (location = 0) in vec3 Position;

void main()
{
gl_Position = vec4(Position, 1.0);
}


frag:

#version 330
out vec4 ColorOut;

void main()
{
ColorOut = vec4(1.0, 1.0, 1.0, 1.0);
}


with red clear color,
as the result I get the triangle in correct place, but the color pattern is flickering with kind of "random" pattern (looks like memory corruption).
tried rendering just with glDrawArrays, and glDrawElements (thus the index buffer) with the same result...

Any ideas what's gone wrong would be appreciated...

rendering function looks like follows:


glViewport(0, 0, mBufferWidth, mBufferHeight);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

glBindBuffer(GL_ARRAY_BUFFER, mBufferId[buffer_type::VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vdata), vdata, GL_STATIC_DRAW);
glEnableVertexAttribArray(attr_type::POSITION);
glVertexAttribPointer(attr_type::POSITION, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
SwapBuffers(mHDC);


Thank you!
Tatiana

P.S. did not manage to upload image file, so here is the link:
https://1drv.ms/i/s!AgiCIBNkQKeZxTO07fvI5lsZmJRR

Silence
11-21-2016, 01:32 AM
First, vec4(1.0, 1.0, 1.0, 1.0) should be white, not red.

Second, do you call to BufferData each time the program enters the drawing function ? If so, you should not. Set it once for all.

surazhsky
11-21-2016, 02:25 AM
First, vec4(1.0, 1.0, 1.0, 1.0) should be white, not red.

Second, do you call to BufferData each time the program enters the drawing function ? If so, you should not. Set it once for all.

Yes, triangle should be white, background red:
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

Background is actually rendered correctly.

I plan to deform geometry in the future (when I solve the problem), hence setting buffer data each frame.
Please have a look at the attached image (https://1drv.ms/i/s!AgiCIBNkQKeZxTO07fvI5lsZmJRR)

john_connor
11-21-2016, 04:16 AM
I plan to deform geometry in the future (when I solve the problem), hence setting buffer data each frame.
Please have a look at the attached image (https://1drv.ms/i/s!AgiCIBNkQKeZxTO07fvI5lsZmJRR)

but you dont need to call everything each frame (likely 60 times per second!)
the only functions you need to call each frame is:

glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
(assuming you dont change the program object)

if you want to change the data, do it only once via "glBufferData(...)"
this function allocates enough memory for the data and uploads the data to OpenGL
use glBufferSubData(...) to skip memory allocation (if the data amount is the same as before)
the other functions (glVertexAtribPointer(...) etc) you dont need to call again

if you can do the deformation within the vertex shader, "transform feedback" could be a way to skip uploading the data from your application to OpenGL buffers

if your "deformation" can be done via "transformation" (matrices), you dont need to modify buffer data at all

surazhsky
11-21-2016, 04:54 AM
Thanks for reminding about glBufferSubData, I shall use that, re. changing geometry in vertex shader, I wish I could do it but I do not have all deformation matrices and have to call an sdk library function to deform vertices individually :(

Still all this does not explain why I get crazy colors inside triangle instead plane white color...

Silence
11-21-2016, 06:23 AM
Several other things:

1) Did you check any OpenGL errors in your code ? Ensure there are no errors returned either by the GL code and the shaders.
2) What OpenGL version are you running ?
3) Did you use BufferData only once when initializing your VAO/VBO ?

surazhsky
11-21-2016, 06:38 AM
Several other things:

1) Did you check any OpenGL errors in your code ? Ensure there are no errors returned either by the GL code and the shaders.
2) What OpenGL version are you running ?
3) Did you use BufferData only once when initializing your VAO/VBO ?

1. Yes, 1st thing I've done checked that error == GL_NO_ERROR (0) after each line
2. context 4.2
3. No, but I tried to use it once as well, same results... again looks like some memory corruption after fragment shader (vertices are good)

Silence
11-21-2016, 11:27 PM
Well I'm a bit out of idea...

You might want to use a tutorial, test the code of this tutorial, then compare with your own code to see any differences.

See this (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/) for example.

surazhsky
11-21-2016, 11:32 PM
Thanks, Silence, tried that...

The issue resolved by driver update.... Just never thought to blame NVidia...

Silence
11-22-2016, 12:33 AM
The issue resolved by driver update.... Just never thought to blame NVidia...

Just for curiosity, what platform, GPU, and driver version did you have at the time of the bug ?

Because having such a bug in triangle filling is more than just a simple bug to my opinion.

surazhsky
11-22-2016, 06:09 AM
>> Silence

Sorry, have no idea which driver I had, system is far from optimal (it's temporary computer for me) -- Windows 8 (NOT 8.1)
HP elitebook with NVIDIA Quadro K2000M

I installed the latest driver yesterday to try Vulkan... and voila...

Thank you for your help
Tatiana