Models are messy without gl_Vertex

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;

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

gl_Position = projection * view * model * vec4(inVertex, 1.0);

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

My code is very similar to this (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?

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.

If I comment out everything except vertices then there is nothing seen. Only with gl_Vertex I see the correct model. Any ideas?

Any ideas?

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

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

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;
}
    //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?

Yes.

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);
        }

I accidentally deleted material layout, diffuse is defined there.

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.

Problem solved!

Attribute locations must be setup before calling glLinkProgram.