PDA

View Full Version : Nothing visible /black screen in opengl 4.0



Rickard
02-05-2011, 11:17 AM
Hi

I currently trying to rewrite a application from opengl 3.2 (compabilty)to 4.0 (forward-compatible).

I've created a vertex array object containing an vbo with vertecies and another vbo with indices:


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

glGenBuffers(2,&vbos);

glBindBuffer(GL_ARRAY_BUFFER,vbos);
glBufferData(GL_ARRAY_BUFFER,sizeof(Vertex)*w*h,(c onst void*)vertices,GL_DYNAMIC_DRAW);

glVertexAttribPointer(in_pos_loc,4,GL_FLOAT,GL_FAL SE,sizeof(Vertex),(void*)0);getAllError(__FILE__,_ _LINE__);
glEnableVertexAttribArray(in_pos_loc);

glVertexAttribPointer(normal_loc,3,GL_FLOAT,GL_FAL SE,sizeof(Vertex),(void*)16);getAllError(__FILE__, __LINE__);
glEnableVertexAttribArray(normal_loc);

glVertexAttribPointer(tex_coord_loc,2,GL_FLOAT,GL_ FALSE,sizeof(Vertex),(void*)28);getAllError(__FILE __,__LINE__);
glEnableVertexAttribArray(tex_coord_loc);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbos+1);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(unsign ed short)*(w-1)*(h-1)*2*3,indices,GL_STATIC_DRAW);


and are trying to draw them with:



glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES,(w-1)*(h-1)*6, GL_UNSIGNED_BYTE, 0);
glBindVertexArray(0);



but all I get is a black screen.

my vert and frag shaders:

#version 150 core
precision highp float;

out vec4 out_Color;

in vec3 out_normal;
in vec2 out_tex_coord;

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


#version 150 core
precision highp float;

out vec4 out_Color;

in vec3 out_normal;
in vec2 out_tex_coord;

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


uniform/attrib locations are fetch with glGetUniformLocation and glGetAttribLocation.

But when I render the screen stays black.
If I change from GL_TRIANGLES to GL_POINTS i get a single pixel/dot in the middle of viewport, the rest stays black

randall
02-05-2011, 01:06 PM
How vbos is declared?

Shouldn't it be like this?

GLuint vbos[2];
glGenBuffers(2,vbos);

glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);

...

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]);

_arts_
02-05-2011, 01:23 PM
it's the same randall, if he declared them as an array.

randall
02-05-2011, 01:26 PM
If he declared vbos as an array it should be:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,*(vbos+1));

not:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbos+1);

But this line shows that he didn't declared vbos as an array:
glGenBuffers(2,&vbos);

I think that he made a mistake and declared vbos as a single GLuint.

Rickard
02-05-2011, 01:36 PM
Well, i've used method this several time before with vbos and the built in pipeline.
I tired with chaning so each vbo had its own variables but no change at all

randall
02-05-2011, 01:55 PM
If you write this:
glGenBuffers(2, vbos);

vbos must be at least two GLuint in size.

Rickard
02-05-2011, 02:04 PM
when change:
glGenBuffers(2,&vbos);
to:
glGenBuffers(1,&vbo1);
glGenBuffers(1,&vbo2);

it still gives the same result

Dan Bartlett
02-05-2011, 02:37 PM
Is that the actual vertex shader you're using, or have you posted the wrong code by mistake?
If it is the code you're using, then you're not passing the transformed vertex position/normals + texcoords onto the next stage correctly.
http://en.wikipedia.org/wiki/GLSL has an example of each shader stage.

Alfonse Reinheart
02-05-2011, 02:48 PM
it still gives the same result

That doesn't mean it wasn't broken. Especially since the Standard for Programming Language C does not define what happens when you do what you did.

It just means it just so happened to work.

More than likely, what happened is that the driver just so happened to give you 2 buffer object names that were in sequential order. Say, 1 and 2. It also trashed the stack (which is where C says that it doesn't know what happens) when it wrote the 2.

Therefore, when you bound "vbos+1", it just so happened that the second buffer you were given was the first buffer plus one.

Rickard
02-05-2011, 02:53 PM
Is that the actual vertex shader you're using, or have you posted the wrong code by mistake?
If it is the code you're using, then you're not passing the transformed vertex position/normals + texcoords onto the next stage correctly.
http://en.wikipedia.org/wiki/GLSL has an example of each shader stage.

My bad, copy paste error :P
The vertex shader:

#version 150 core
precision highp float;

in mat4 projection_matrix;
in mat4 view_matrix;
in mat4 model_matrix;

in vec4 in_pos;
in vec3 in_normal;
in vec2 in_tex_coord;

out vec3 out_normal;
out vec2 out_tex_coord;

void main(void){
out_normal = in_normal;
out_tex_coord = in_tex_coord;
vec4 pos = projection_matrix * view_matrix * model_matrix * in_pos;
pos.x = pos.x / pos.w;
pos.y = pos.y / pos.w;
pos.z = pos.z / pos.w;
pos.w = pos.w / pos.w;
gl_Position = pos;
}
and the fragment shader

#version 150 core
precision highp float;

out vec4 out_Color;

in vec3 out_normal;
in vec2 out_tex_coord;

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

Dan Bartlett
02-05-2011, 03:16 PM
You probably want projection_matrix, view_matrix & model_matrix to be declared as uniforms, rather than as attributes.

mobeen
02-05-2011, 09:38 PM
You dont have to do the perspective division since it will give you the position in normalized device coordinates. The vertex shader is expected to give the position in clipspace. This u get when you multiply the incoming vertex position with the combined modelviewprojection matrix as follows.



#version 150 core
precision highp float;

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;

in vec4 in_pos;
in vec3 in_normal;
in vec2 in_tex_coord;

out vec3 out_normal;
out vec2 out_tex_coord;

void main(void){
out_normal = in_normal;
out_tex_coord = in_tex_coord;
gl_Position = projection_matrix * view_matrix * model_matrix * in_pos;
}

and since your shader is not using the [in,out]_normal and [in,out]_tex_coord, the GLSL compiler optimizer will strip it and the location for these would be -1. Apart from this the output color should be written from the shader.

#version 150 core
precision highp float;

out vec4 out_Color;

in vec3 out_normal;
in vec2 out_tex_coord;

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

See if u get something using these changes.


Regards,
Mobeen

Rickard
02-06-2011, 01:55 AM
You probably want projection_matrix, view_matrix & model_matrix to be declared as uniforms, rather than as attributes.

Of course it should me uniform and not in. So silly of me.



See if u get something using these changes.


I did thoose changes and now it works. Thank you all very much.