Friday 15 April 2011

c++ - OpenGL texture and UV mapping issue -



c++ - OpenGL texture and UV mapping issue -

i trying create little game in opengl effort larn how utilize api. i've come point can move photographic camera around simple scene, , can render models , shade them simple phong model shader.

i'm right working on texturing models in scene, got re-create of maya , made (with quite struggle) square texture uv mapping made in within maya.

when render scene, texture applied, far correct. read models .obj files parser wrote myself, , textures read using funtion found online while back.

i'm not sure how describe problem in sufficient detail, nor in code, here code fractions suspect contained problem.

reading 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); homecoming textureid; }

setting texture prior rendering 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); homecoming texcolor; // (vec4(lightintensity, 1.0) * (vec4(ad_light, 1.0) * texcolor + vec4(specular_light, 1.0))); } void main() { fragcolor = ads(); }

i know things written strangely, @ point i'm starting seek working.

does have suggestion on how solve unusual uv mapping?

edit:

obj loading

i have made obj loader print vertex attributes , compared these indexing in .obj file. looks verecies, normals , uvs showing in right order.

screenshot

the scene looks using simple reg greenish gradient trexture image.

(the square should understading show gradient texture? not single color)

alignment sounds possible flaw, how can right this?

solution

i made simple , easy overlook mistake. in top of vertex shader wrote

layout(location = 0) in vec3 vertexposition; layout(location = 1) in vec3 vertexnormal; layout(location = 1) in vec2 texturecoord;

so guess when sent normal info location 1, set texture coordinates normal data, uv coords never reached fragment shader.

changeing folowing resolved problem without farther change.

layout(location = 0) in vec3 vertexposition; layout(location = 1) in vec3 vertexnormal; layout(location = 2) in vec2 texturecoord;

c++ opengl shader uv-mapping

No comments:

Post a Comment