OpenGL/Qt with shaders : no texture

Started by
2 comments, last by ote 12 years, 10 months ago
Hello everyone,

I'm having trouble drawing a model with his texture. With colors for each vertex it's ok, models are colored, but when I try to use a texture my model is render in black.

I'm pretty sure the texture loading is working cause I've been printing the id and they are increasing as I load a new texture.
Although when I use the Qt's function bindTexture(const QString &filename) I always have ids = 0. But with the classic glGenTextures, glBindTexture and glTexImage2D I get an id superior to 0 and it keeps increasing.

I already managed to get textured objects but I'm new to Qt and I think something is missing, but I can't figure by myself what, so I anyone could help me I'll really appreciate.

(sorry for my english)

Here's the relevant code :


//texture id is store as a GLuint in a structure
infoMesh.texture = m_RenduModele->loadTexture(nomTex);


How I load the texture :

GLuint RenduModele::loadTexture(std::string &nom)
{
QString appliDir = QCoreApplication::applicationDirPath();
QString cheminComplet = appliDir + nom.c_str();

glEnable(GL_TEXTURE_2D);

QImage texBuf;
texBuf.load(cheminComplet);
QImage tex = QGLWidget::convertToGLFormat(texBuf);

GLuint id = 0;
glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

GLint internFormat;
GLenum format;
if(tex.hasAlphaChannel())
{
internFormat = 4;
format = GL_RGBA;
}
else
{
internFormat = 3;
format = GL_RGB;
}

glTexImage2D(GL_TEXTURE_2D, 0, internFormat, tex.width(), tex.height(), 0, format, GL_UNSIGNED_BYTE, tex.bits());

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glDisable(GL_TEXTURE_2D);

return id;

//doesn't work
//return bindTexture(cheminComplet);
}


and the rendering :

void RenduModele::renderModeleMesh(Modele *modele, unsigned int numMesh)
{
m_program.bind();

glm::mat4 matrixMP = m_camera->getProjectionMatrix() * m_camera->getModelViewMatrix() * modele->getMatrice();

QMatrix4x4 mvp;
mat4toQmat4(matrixMP, mvp);

for(unsigned int i = 0; i < modele->getNbMesh(); i++)
{
g_resources infos = modele->m_info_rendu;

m_program.setUniformValue(infos.uniform.MVP, mvp);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, infos.texture);

infos.vertex_buffer->bind();

m_program.enableAttributeArray(infos.in.position);
m_program.enableAttributeArray(infos.in.texCoord);

infos.vertex_buffer->release();

infos.element_buffer->bind();
glDrawElements(GL_TRIANGLES, infos.count, GL_UNSIGNED_INT, NULL);
infos.element_buffer->release();

m_program.disableAttributeArray(infos.in.position);
m_program.disableAttributeArray(infos.in.texCoord);
}
m_program.release();
}


vertex shader :

uniform mat4 MVP;
uniform sampler2D texture;

in vec4 position;
in vec2 texCoord;

out vec4 v_couleur;

void main()
{

gl_Position = MVP * position;

v_couleur = vec4(texture2D(texture, texCoord.st).rgb, 1.0);
}


fragment shader :


in vec4 v_couleur;
out vec4 color;

void main()
{
color = colorFactor * v_couleur;
}
Advertisement
You have to assign the uniform sampler variable ("texture" in your case) to be the number of the texture unit you've bound your texture to (probably zero in this case, because you haven't called glActiveTexture).

You only need to do this once (not per mesh) -- probably just after your program bind in this case.
Thanks for your reply but how do I do that ? I've try to add that line

m_program.setUniformValue("texture", 0);

in the initializeGL function, after the program is bind, but it doesn't change anything, the modele is still black.

I also try to this :
glActiveTexture(GL_TEXTURE0);

in initializeGLbut still no improvement.
I finally manage to render a model with his texture. I was not giving the complete path of the file, so Qt wasn't finding it.

Now I'm trying to find a solution for not writing in the code the folder where images are, but consider this problem solved.

This topic is closed to new replies.

Advertisement