[SOLVED] How to use textures in Qt OpenGL?
-
Hello
Why I don't see a texture on my triangle? https://github.com/8Observer8/TexturedTriangle
-
Here's what I found:
-
In the fragment shader, to get the color from the texture, you need to use the texture2d() glsl function. Like so:
@color = texture2d( textureUniform, texture );@ -
In Triangle::initTexture(), you're initializing that textureCoords array incorrectly. Change it to this:
@textureCoords[0] = 0.5f;
textureCoords[1] = 1.0f;
textureCoords[2] = 0.0f;
textureCoords[3] = 0.0f;
textureCoords[4] = 1.0f;
textureCoords[5] = 0.0f;@
That should do it.
-
-
Thank you! I solved this problem
Source: https://github.com/8Observer8/TexturedTriangle
Exe for Win 7: https://yadi.sk/d/dD84FsxPdPbBf -
I would recommend to not use QGLWidget in new code. This class is soon to be deprecated in favour of QOpenGLWindow, QOpenGLWidget or just a plain old QWindow + QOpenGLContext.
To work with textures, QOpenGLTexture will make you life much easier.
I would also recommend you to use the OpenGL core profile rather than the compatibility profile. In which case your shaders will need updating to replace "attribute" and "varying" with "in" and "out" in the vertex shader and to replace "varying" with "in" in the frag shader. Plus declaring an "out vec4 fragColor" or similar output variable in the fragment shader.
Then for sampling from a texture in your shaders you can always use the overloaded texture() function rather than the texture1d(), texture2d(), texture3d() etc. functions.
-
It is very interesting. I will install Qt 5.4. Thank you very much for your reply
-
ZapB, thank you again. I changed my project how did you say: https://github.com/8Observer8/TexturedTriangle
But I want use “attribute” and “varying” because it is OpenGL ES. I saw it here: C:\Qt\Qt5.4.0\Examples\Qt-5.4\opengl\qopenglwidget
How do you see I draw in the class Scene:
@void Scene::drawTriangle()
{
m_texture->bind();m_program.setAttributeArray( m_vertexAttr, m_triangle.vertices.data(), 3 ); m_program.setAttributeArray( m_texCoordAttr, m_triangle.texCoords.data(), 2 ); m_program.setUniformValue( m_texUniform, 0 ); m_program.enableAttributeArray( m_vertexAttr ); m_program.enableAttributeArray( m_texCoordAttr ); glDrawArrays( GL_TRIANGLES, 0, 3 ); m_program.disableAttributeArray( m_vertexAttr ); m_program.disableAttributeArray( m_texCoordAttr );
}@
But I want to draw in the class Triangle, like this:
@#ifndef TRIANGLE_H
#define TRIANGLE_H#include <vector>
class Triangle
{
public:
Triangle();
void initVertices();
void initTexCoords();void draw(); std::vector<float> vertices; std::vector<float> texCoords;
};
#endif // TRIANGLE_H@
@void Triangle::draw()
{
m_texture->bind();m_program.setAttributeArray( m_vertexAttr, m_triangle.vertices.data(), 3 ); m_program.setAttributeArray( m_texCoordAttr, m_triangle.texCoords.data(), 2 ); m_program.setUniformValue( m_texUniform, 0 ); m_program.enableAttributeArray( m_vertexAttr ); m_program.enableAttributeArray( m_texCoordAttr ); glDrawArrays( GL_TRIANGLES, 0, 3 ); m_program.disableAttributeArray( m_vertexAttr ); m_program.disableAttributeArray( m_texCoordAttr );
}@
I hope you understand my problem. The glDrawArrays() function is a member of the QOpenGLWidget class and so on
-
bq. How do you see I draw in the class Scene
I solved this:
@#ifndef TRIANGLE_H
#define TRIANGLE_H#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <vector>class Triangle
{
public:
Triangle( QOpenGLShaderProgram *program, QOpenGLTexture *texture,
int vertexAttr, int texCoordAttr, int texUniform );
void initVertices();
void initTexCoords();void draw(); std::vector<float> vertices; std::vector<float> texCoords;
private:
QOpenGLShaderProgram *m_program;
QOpenGLTexture *m_texture;
int m_vertexAttr;
int m_texCoordAttr;
int m_texUniform;
};#endif // TRIANGLE_H@
@
#include "Triangle.h"
#include <GL/gl.h>Triangle::Triangle( QOpenGLShaderProgram *program, QOpenGLTexture *texture,
int vertexAttr, int texCoordAttr, int texUniform ) :
m_program( program ),
m_texture( texture ),
m_vertexAttr( vertexAttr ),
m_texCoordAttr( texCoordAttr ),
m_texUniform( texUniform )
{
initVertices();
initTexCoords();
}void Triangle::initVertices()
{
vertices.clear();
vertices.resize( 9 );
// 0
vertices[0] = 0.0f;
vertices[1] = 0.5f;
vertices[2] = 0.0f;
// 1
vertices[3] = -0.5f;
vertices[4] = -0.5f;
vertices[5] = 0.0f;
// 2
vertices[6] = 0.5f;
vertices[7] = -0.5f;
vertices[8] = 0.0f;
}void Triangle::initTexCoords()
{
texCoords.clear();
texCoords.resize( 6 );
// 0
texCoords[0] = 0.5f;
texCoords[1] = 1.0f;
// 1
texCoords[2] = 0.0f;
texCoords[3] = 0.0f;
// 2
texCoords[4] = 1.0f;
texCoords[5] = 0.0f;
}void Triangle::draw()
{
m_texture->bind();m_program->setAttributeArray( m_vertexAttr, vertices.data(), 3 ); m_program->setAttributeArray( m_texCoordAttr, texCoords.data(), 2 ); m_program->setUniformValue( m_texUniform, 0 ); m_program->enableAttributeArray( m_vertexAttr ); m_program->enableAttributeArray( m_texCoordAttr ); glDrawArrays( GL_TRIANGLES, 0, 3 ); m_program->disableAttributeArray( m_vertexAttr ); m_program->disableAttributeArray( m_texCoordAttr );
}
@