0

I am currently trying to make a little game in OpenGL as an attempt to learn how to use the API. I've come to a point where I can move a camera around a simple scene, and I can render models and shade them with a simple phong model shader.

I'm right now working on texturing the models in the scene, so I got a copy of Maya and made (with quite some struggle) a square with a texture with the UV mapping made in within Maya.

When I render the scene, the texture is applied, but far from correct. I read the models as .obj files with a parser I wrote myself, and the textures are read using a funtion I found online a while back.

I'm not sure how to describe the problem in sufficient detail, nor what to look for in the code, but here are some code fractions that I would suspect contained the problem.

Reading the texture

 GLuint loadTexture(Image* image){
   GLuint textureId;
   glGenTextures(1, &textureId);
   glBindTexture(GL_TEXTURE_2D, textureId);
   glTexImage2D(GL_TEXTURE_2D,
     0,
     GL_RGB,
     image->width, image->height,
     0,
     GL_RGB,
     GL_UNSIGNED_BYTE,
     image->pixels);
   return textureId;
 }

Setting the texture prior to rendering the mesh

// set texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->body_texture);
current_shader->setUniformint(0, "Difuse_texture");

Vertex shader

#version 410

layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 1) in vec2 TextureCoord;



out vec3 Position;
out vec3 Normal;
out vec2 TexCoord;

uniform mat4 ModelMatrix;
uniform mat4 VeiwMatrix;
uniform mat4 ProjectionMatrix;
uniform mat3 NormalMatrix;


void main(){

  mat4 ModelVeiwMatrix = VeiwMatrix * ModelMatrix;
  mat4 MVP = ProjectionMatrix * ModelVeiwMatrix;

  TexCoord = TextureCoord;
  Normal = normalize( NormalMatrix * VertexNormal );
  Position = vec3(ModelVeiwMatrix * vec4(VertexPosition, 1.0));

  gl_Position = MVP * vec4(VertexPosition, 1.0);
}

Fragment shader

#version 410

in vec3 Position;
in vec3 Normal;
in vec2 TexCoord;

uniform vec4 LightPosition;
uniform vec3 LightIntensity;
uniform vec3 Kd;
uniform vec3 Ka;
uniform vec3 Ks;
uniform float Shininess;

uniform sampler2D Difuse_texture;

layout(location = 0) out vec4 FragColor;

vec4 ads(){
  vec3 n = normalize( Normal );
  vec3 s = normalize( vec3(LightPosition) - Position );
  vec3 v = normalize( vec3(-Position) );
  vec3 r = reflect( -s, n );

  vec3 specular_light = Ks * pow(max(dot(r, v), 0.0), Shininess);

  vec3 ad_light = Ka + Kd * max(dot(s, n), 0.0);

  vec4 TexColor = texture2D(Difuse_texture, TexCoord);

  return TexColor; // (vec4(LightIntensity, 1.0)  * (vec4(ad_light, 1.0) * TexColor + vec4(specular_light, 1.0)));
}


void main() {
  FragColor = ads();
}

I know some things are written strangely, but at this point I'm starting to just try anything to get it working.

Does anyone have a suggestion on how to solve this strange UV mapping?

EDIT:

OBJ LOADING

I have made the obj loader print all vertex attributes and compared these with the indexing in the .obj file. It looks like the verecies, normals and UVs are showing in the correct order.

Screenshot

The scene looks like this using just simple reg to green gradient as trexture image.

(The square should by my understading show the gradient from the texture? not just a single color)

Alignment sounds like a possible flaw, how can I correct this?

a http://imageshack.com/a/img674/9927/y0bJ51.png

SOLUTION

I made a very simple and easy to overlook mistake. In the top of the vertex shader i wrote

layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 1) in vec2 TextureCoord;

So I guess that when I sent the normal data to location 1, I set the Texture coordinates to normal data, so the UV coords never reached the fragment shader.

Changeing to the folowing resolved the problem without further change.

layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 2) in vec2 TextureCoord;
user1291510
  • 265
  • 5
  • 14
  • Without a screenshot, there's no way to really know what's "wrong" in this situation. One thing I can say immediately, however, is that you are using an 8-bit RGB image format and that has alignment issues that I don't see you properly taking care of (*e.g.* no call to `glPixelStorei(...)`). If your problem is something along the lines of a slanted image, then alignment is to blame. – Andon M. Coleman Nov 03 '14 at 20:45
  • "I read the models as .obj files with a parser I wrote myself" Are you sure the data is loaded correctly? There are some common pitfalls with using .obj files with opengl, as the data structures of both do not map to each other 1:1, i.e. there is a separate index per attribute in .obj, while in the GL, an index represents a whole vertex with _all_ attributes. – derhass Nov 03 '14 at 20:48

0 Answers0