PDA

View Full Version : Models are messy without gl_Vertex



Karmux
08-09-2011, 05:24 PM
I'm loading my 3d models with Assimp library and there is no problem if I calculate vertex position in shader this way:

gl_Position = projection * view * model * gl_Vertex;
http://www.upload.ee/preview/1566434/right.png

When using my own vertex position uniform, then models are just a flat mess.

gl_Position = projection * view * model * vec4(inVertex, 1.0);
http://www.upload.ee/preview/1566435/wrong.png

Currently I'm using glUniformMatrix4fv to pass matrices to shader (there is same effect with glBindAttribLocation).

My code is very similar to this (http://www.lighthouse3d.com/cg-topics/code-samples/importing-3d-models-with-assimp/) (I don't have bouncing box and node mTransformation). I tried to use node transformations but then I didn't see even that mess anymore. Should I use it anyway? If yes, then why gl_Vertex (and fixed function pipeline) doesn't require node transformations?

Alfonse Reinheart
08-09-2011, 05:35 PM
That second one looks suspiciously like you're passing texture coordinates rather than model space positions. You may want to check your attribute indices to make sure you're binding them to the right ones.

Karmux
08-10-2011, 02:49 PM
If I comment out everything except vertices then there is nothing seen. Only with gl_Vertex I see the correct model. Any ideas?

Alfonse Reinheart
08-10-2011, 03:25 PM
Any ideas?

No. You haven't shown us anything. Post your shaders.

Also, did you check the attribute indices as I asked?

Karmux
08-10-2011, 04:33 PM
Indices are 0 for vertices, 1 for normals and 2 for texture coords.

Vertex shader:

#version 330

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

in vec3 inVertex;
in vec3 inNormal;
in vec2 inTexCoord;

out vec3 normal;
out vec2 texCoord;

void main() {
//gl_Position = projection * view * model * vec4(inVertex, 1.0); // Doesn't work
gl_Position = projection * view * model * gl_Vertex; // Works
normal = normalize(vec3(model * view * vec4(inNormal, 0.0)));
texCoord = inTexCoord;
}

Fragment shader:

#version 330

in vec3 normal;
in vec2 texCoord;

uniform sampler2D tex;
uniform int texCount;
uniform vec4 color;

void main() {
//gl_FragColor = texture2D(tex, texCoord);
gl_FragColor = diffuse;
}

Alfonse Reinheart
08-10-2011, 04:41 PM
//gl_Position = projection * view * model * vec4(inVertex, 1.0); // Doesn't work
gl_Position = projection * view * model * gl_Vertex; // Works

When you say that one works and one doesn't, are you saying that, if you only change which lines are commented, then it works?

If so, then you need to stop using glVertexPointer. glVertexPointer feeds data to gl_Vertex; it cannot feed data to anything else.

Generic attributes (attributes not defined by the language) are fed with glVertexAttribPointer.


gl_FragColor = diffuse;

There is no `diffuse` defined, so where does that come from?

Karmux
08-10-2011, 05:02 PM
//gl_Position = projection * view * model * vec4(inVertex, 1.0); // Doesn't work
gl_Position = projection * view * model * gl_Vertex; // Works

When you say that one works and one doesn't, are you saying that, if you only change which lines are commented, then it works?
Yes.


If so, then you need to stop using glVertexPointer. glVertexPointer feeds data to gl_Vertex; it cannot feed data to anything else.

Generic attributes (attributes not defined by the language) are fed with glVertexAttribPointer.
I'm using glVertexAttribPointer and I don't have any glVertexPointer in my code:

// buffer for vertex positions
if (mesh->HasPositions()) {
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->mNumVertices, mesh->mVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0);
}



gl_FragColor = diffuse;

There is no `diffuse` defined, so where does that come from?
I accidentally deleted material layout, diffuse is defined there.

Alfonse Reinheart
08-10-2011, 06:29 PM
I'm using glVertexAttribPointer and I don't have any glVertexPointer in my code:

Then your OpenGL implementation is broken. I'm guessing you're using NVIDIA, right?

Also, creating a buffer object is not the same thing as telling OpenGL where the data comes from. You generally do that every frame.

I'm guessing that there's some code somewhere that's interfering with what you think attribute 0 is.

Karmux
08-11-2011, 02:23 AM
Problem solved!

Attribute locations must be setup before calling glLinkProgram.