PDA

View Full Version : opengl 3.2 core profile - first triangle



Schnulla
07-05-2010, 06:16 AM
Hi,

somehow my first "OpenGL 3.2 core profile" triangle always
stays white even if I directly change the color to red
in the minimal fragment shader:


#version 150 core

out vec4 out_Color;

void main(void)
{
out_Color = vec4(1.0, 0.0, 0.0, 1.0);
}


#version 150 core

in vec3 in_Position;

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


Here is my code:


float* vert = new float[9];

vert[0] = 0.0; vert[1] = 0.5; vert[2] =-1.0;
vert[3] =-1.0; vert[4] =-0.5; vert[5] =-1.0;
vert[6] = 1.0; vert[7] =-0.5; vert[8]= -1.0;

unsigned int m_vaoID;
unsigned int m_vboID;

glGenVertexArrays(1, &m_vaoID);
glBindVertexArray(m_vaoID);

glGenBuffers(1, &m_vboID);

glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vert, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glBindVertexArray(0);

delete[] vert;
delete[] col;

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &m_vboID);
glDeleteVertexArrays(1, &m_vaoID);

Help is really appreciated!

Dan Bartlett
07-05-2010, 07:53 AM
Have you checked that the shaders compile ok + the program linked ok, with:


glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
glGetProgramiv(prog, GL_LINK_STATUS, &status);
It's possible to have problems with #version and the method you use for merging the shader into one string. Usually it's ok to have program code combined into one line, because it's separated with semi-colons, but the #version statement does not end with a semicolon, so you need to make sure there's a line break included in the string:

If your code is merged into one long line like this, it will fail:

'#version 150 core in vec3 in_Position; void main(void) {gl_Position= vec4(in_Position, 1.0);}'
But if you include a line-break it will be ok:

'version 150 core'+#13#10+
'in vec3 in_Position; void main(void) {gl_Position= vec4(in_Position, 1.0);}'

You should also really check the attribute location with glGetAttribLocation, or bind it manually with glBindAttribLocation, rather than just using 0.


posLoc = glGetAttribLocation(prog, "in_Position");
glVertexAttribPointer(posLoc, ...);

And, you've remembered also to attach the shaders to the program, before linking (with glAttachShader)? :P

Schnulla
07-05-2010, 08:10 AM
Hi,

according to my log there are no compiling or linking errors:


Uploading source code for shader Tex-PFL.frag.
Upload complete!

Uploading source code for shader Tex-PFL.vert.
Upload complete!

Compiling shader Tex-PFL.frag.
Compiling successful!

Fragment shader was successfully compiled to run on hardware.

Compiling shader Tex-PFL.vert.
Compiling successful!

Vertex shader was successfully compiled to run on hardware.

Attaching shader Tex-PFL.frag to shader program Tex-PFL.
Shader attached!

Attaching shader Tex-PFL.vert to shader program Tex-PFL.
Shader attached!

Linking shader program Tex-PFL.
Linking successful!

Fragment shader(s) linked, vertex shader(s) linked.

Validating shader program Tex-PFL.
Program is valid!

Validation successful.

Concerning glGetAttribLocation:
Actually I use glGetAttribLocation but removed it
for the code snippet which I posted above :)
And the vertices are positioned correctly so this
seems to work. Only the color is wrong =/

I have no clue what is going on... :(

Dan Bartlett
07-05-2010, 08:45 AM
You've remembered to glUseProgram before drawing too? :P
I converted the code to Delphi + ran it, I got a red triangle so it seems to work ok.

Schnulla
07-05-2010, 09:01 AM
Yes I used glUseProgram directly after linking!

But guess what I rebooted my PC and now it works :eek:

Yeha! Seems the demos I watched this morning
f$%$ed up my drivers or whatever. I'm sorry
for wasting your time!! :o