devmane144

06-20-2015, 12:26 AM

Hello,

Using OpenGL tutorials found on opengl-tutorial, I constructed an .obj file loader as well as a texture file loader using SOIL. I have an issue with drawing a textured object. (Shown here (https://www.youtube.com/watch?v=OfOHd88mXNI&feature=youtu.be)) Assuming I created the loaders correctly, my issue is within my rendering method (actually drawing the triangles on the screen). As you can see my texture seems to bend and warp in a very strange way. I've asked a couple of different sources and they can't seem to explain why it happens. My (admittedly messy) code is like this:

Shaders:

#version 120

//VShader

attribute vec3 verPos;

attribute vec2 UV;

varying vec2 f_UV;

varying vec3 normal;

uniform mat4 comb;

void main()

{

normal = gl_Normal;

vec4 v = vec4(verPos, 1.0);

gl_Position = comb * v;

f_UV = UV;

}

#version 120

//FShader

uniform sampler2D TextSamp2D;

varying vec3 normal;

varying vec2 f_UV;

void main()

{

gl_FragColor = texture2D(TextSamp2D, f_UV);

}

And just in case the view/projection matrices matter, I will include this little tidbit of code as well.

camera::camera(){

projectionMatrix = glm::perspective(45.0f, float(640) / float(480), 0.1f, 1000.0f);

viewMatrix = glm::mat4(1.0);

}

void camera::setPos(glm::vec3 nPos){

myPos = nPos;

}

void camera::setProjectionMatrix(float fov, float scHeight, float scWidth, float disC, float disF){

projectionMatrix = glm::perspective(fov, scHeight/scWidth, disC, disF);

}

The view matrix is updated by throwing this into the viewMatrix property of a camera instantiated as mainCamera:

mainCamera.viewMatrix = glm::lookAt(myPos, myPos + forwardVec, glm::vec3(0,1,0));

And this is my renderer, this is run every frame (no I don't frame limit, but my computer is super junky, hence the GLSL 120) :

void obj3D::Draw(){

if(!loaded) return;

glDepthMask(GL_TRUE);

glUseProgram(myProgID);

glm::mat4 modelTrans = glm::translate(myPosition);

glm::quat myQuat;

myQuat = glm::quat(myAngle);

glm::mat4 modelRot = glm::toMat4(myQuat);

glm::mat4 modelScale = glm::scale(myScale);

glm::mat4 model = modelTrans * modelRot * modelScale;

glm::mat4 comb = mainCamera.projectionMatrix * mainCamera.viewMatrix * model;

GLuint matrixID = glGetUniformLocation(myProgID, "comb");

glUniformMatrix4fv(matrixID, 1, GL_FALSE, &comb[0][0]);

glActiveTexture(GL_TEXTURE0 + 0);

//myTexture is the texture ID after it is

//loaded into OpenGL, it's a member of the object class

glBindTexture(GL_TEXTURE_2D, myTexture);

glUniform1i(shaderTextureID, 0);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vBuffer);

glVertexAttribPointer(

0,

3,

GL_FLOAT,

GL_FALSE,

0,

(GLvoid*)0

);

glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);

glVertexAttribPointer(

1,

2,

GL_FLOAT,

GL_FALSE,

0,

(GLvoid*)0

);

glEnableVertexAttribArray(2);

glBindBuffer(GL_ARRAY_BUFFER, nBuffer);

glVertexAttribPointer(

2, // attribute

3, // size

GL_FLOAT, // type

GL_FALSE, // normalized?

0, // stride

(GLvoid*)0 // array buffer offset

);

glDrawArrays(GL_TRIANGLES, 0, numTriangles);

glDisableVertexAttribArray(0);

glDisableVertexAttribArray(1);

glDisableVertexAttribArray(2);

}

My question is, why does the texture bend and warp when I rotate the view matrix on the y axis? That is, at what horrifically easy juncture did I go so wrong?

Thanks,

Devmane144

Using OpenGL tutorials found on opengl-tutorial, I constructed an .obj file loader as well as a texture file loader using SOIL. I have an issue with drawing a textured object. (Shown here (https://www.youtube.com/watch?v=OfOHd88mXNI&feature=youtu.be)) Assuming I created the loaders correctly, my issue is within my rendering method (actually drawing the triangles on the screen). As you can see my texture seems to bend and warp in a very strange way. I've asked a couple of different sources and they can't seem to explain why it happens. My (admittedly messy) code is like this:

Shaders:

#version 120

//VShader

attribute vec3 verPos;

attribute vec2 UV;

varying vec2 f_UV;

varying vec3 normal;

uniform mat4 comb;

void main()

{

normal = gl_Normal;

vec4 v = vec4(verPos, 1.0);

gl_Position = comb * v;

f_UV = UV;

}

#version 120

//FShader

uniform sampler2D TextSamp2D;

varying vec3 normal;

varying vec2 f_UV;

void main()

{

gl_FragColor = texture2D(TextSamp2D, f_UV);

}

And just in case the view/projection matrices matter, I will include this little tidbit of code as well.

camera::camera(){

projectionMatrix = glm::perspective(45.0f, float(640) / float(480), 0.1f, 1000.0f);

viewMatrix = glm::mat4(1.0);

}

void camera::setPos(glm::vec3 nPos){

myPos = nPos;

}

void camera::setProjectionMatrix(float fov, float scHeight, float scWidth, float disC, float disF){

projectionMatrix = glm::perspective(fov, scHeight/scWidth, disC, disF);

}

The view matrix is updated by throwing this into the viewMatrix property of a camera instantiated as mainCamera:

mainCamera.viewMatrix = glm::lookAt(myPos, myPos + forwardVec, glm::vec3(0,1,0));

And this is my renderer, this is run every frame (no I don't frame limit, but my computer is super junky, hence the GLSL 120) :

void obj3D::Draw(){

if(!loaded) return;

glDepthMask(GL_TRUE);

glUseProgram(myProgID);

glm::mat4 modelTrans = glm::translate(myPosition);

glm::quat myQuat;

myQuat = glm::quat(myAngle);

glm::mat4 modelRot = glm::toMat4(myQuat);

glm::mat4 modelScale = glm::scale(myScale);

glm::mat4 model = modelTrans * modelRot * modelScale;

glm::mat4 comb = mainCamera.projectionMatrix * mainCamera.viewMatrix * model;

GLuint matrixID = glGetUniformLocation(myProgID, "comb");

glUniformMatrix4fv(matrixID, 1, GL_FALSE, &comb[0][0]);

glActiveTexture(GL_TEXTURE0 + 0);

//myTexture is the texture ID after it is

//loaded into OpenGL, it's a member of the object class

glBindTexture(GL_TEXTURE_2D, myTexture);

glUniform1i(shaderTextureID, 0);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vBuffer);

glVertexAttribPointer(

0,

3,

GL_FLOAT,

GL_FALSE,

0,

(GLvoid*)0

);

glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);

glVertexAttribPointer(

1,

2,

GL_FLOAT,

GL_FALSE,

0,

(GLvoid*)0

);

glEnableVertexAttribArray(2);

glBindBuffer(GL_ARRAY_BUFFER, nBuffer);

glVertexAttribPointer(

2, // attribute

3, // size

GL_FLOAT, // type

GL_FALSE, // normalized?

0, // stride

(GLvoid*)0 // array buffer offset

);

glDrawArrays(GL_TRIANGLES, 0, numTriangles);

glDisableVertexAttribArray(0);

glDisableVertexAttribArray(1);

glDisableVertexAttribArray(2);

}

My question is, why does the texture bend and warp when I rotate the view matrix on the y axis? That is, at what horrifically easy juncture did I go so wrong?

Thanks,

Devmane144