1
votes

I've been trying to do simple texture mapping with SOIL, and I've been having bizarre outputs..

The output ONLY displays when a PNG texture is loaded. (SOIL_load_OGL_texture) Other textures appear as a greyish or a white.

vertices passed as:

struct Vertex {
    float position[3];
    float texturepos[2];
};

Vertex vertices[] = { // w and h are width and height of square.
    0,0,0,0,0,
    w,0,0,1,0,
    0,h,0,0,1,
    w,0,0,1,0,
    w,h,0,1,1,
    0,h,0,0,1
};

vertex shader:

attribute vec2 texpos;
attribute vec3 position;
uniform mat4 transform;
varying vec2 pixel_texcoord;
void main(void) {
    gl_Position=transform * vec4(position,1.0);
    pixel_texcoord=texpos;
}

fragment shader:

varying vec2 pixel_texcoord;
uniform sampler2D texture;
void main(void) {
    gl_FragColor=texture2D(texture,pixel_texcoord);
}

All of the uniforms and attributes are validated.

Texture trying to render:

image for render (is 128x128, power of 2.)

Output [with normal shaders]:

output1

However, I think the problem lies entirely in something really bizarre that happened when I tried to debug it.

I changed the fragment shader to:

varying vec2 pixel_texcoord;
uniform sampler2D texture;
void main(void) {
    gl_FragColor=vec4(pixel_texcoord.x,0,pixel_texcoord.y,1);
}

And got this result: output2 Something is very wrong with the texture coordinates, as according to the shader, Y is now X, and X no longer exists. Can anyone explain this?

If my texture coordinates are correctly positioned, then I'll start looking at another image library..

[EDIT] I tried loading an image through raw gimp-generated data, and it had the same problem. It's as if the texture coordinates are 1-dimensional..

1
You should add how you set up the vertex streams. The glvertexattribpointer etc calls. - starmole
From depicting the texture coordinates in your debug shader it becomes clear that it cannot really have anything to do with the texture image at all. I second starmole that in the case showing the actual vertex format setup (and drawing) is crucial for getting a solution to the problem. - Christian Rau
Where are your #version directives? - genpfault

1 Answers

0
votes

Found the problem! Thanks to starmole's advice, I took another look at the glVertexAttribPointer calls, which were formatted as this:

glVertexAttribPointer(attribute_vertex,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),0);
glVertexAttribPointer(attribute_texture_coordinate,2,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*) (sizeof(GLfloat) * 2));

The 2 in (void*) (sizeof(GLfloat) * 2)); should have been a 3, as there were 3 vertex coordinates.

Everything works perfectly now.

It's amazing how such a small typo can break it so badly.