我无法在我的立方体上设置纹理:它呈现灰色。 我在Ubuntu上使用Qt 5.1和QTCreator,使用OpenGL 3.3。
以下是我的着色器:
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 Texc;
uniform sampler2D tex ;
uniform mat4 modelToWorld;
uniform mat4 worldToCamera;
uniform mat4 cameraToView;
out vec4 vColor;
void main()
{
gl_Position = cameraToView * worldToCamera * modelToWorld * vec4(position, 1.0);
vColor = texture2D(tex, Texc);
}
#version 330
in vec4 vColor;
out vec4 fColor;
void main()
{
fColor = vColor;
}
这是window.cpp文件,其中包含有用的功能:
void Window::initializeGL()
{
// Initialize OpenGL Backend
initializeOpenGLFunctions();
connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
printContextInformation();
// Set global information
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Application-specific initialization
{
// Create Shader (Do not release until VAO is created)
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertexText.txt");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fragmentText.txt");
m_program->link();
m_program->bind();
QOpenGLTexture *texture = new QOpenGLTexture(QImage(":/texture.jpg").mirrored());
//texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
texture->setMagnificationFilter(QOpenGLTexture::Linear);
texture->bind() ;
u_texturecolors = m_program->uniformLocation("tex");
// Cache Uniform Locations
u_modelToWorld = m_program->uniformLocation("modelToWorld");
u_worldToCamera = m_program->uniformLocation("worldToCamera");
u_cameraToView = m_program->uniformLocation("cameraToView");
//u_worldToView = m_program->uniformLocation("worldToView");
// Create Buffer (Do not release until VAO is created)
m_vertex.create();
m_vertex.bind();
m_vertex.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_vertex.allocate(sg_vertexes, sizeof(sg_vertexes));
// Create Vertex Array Object
m_object.create();
m_object.bind();
m_program->enableAttributeArray(0);
m_program->enableAttributeArray(1);
m_program->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
m_program->setAttributeBuffer(1, GL_FLOAT, Vertex::texcoordOffset(), Vertex::TexcoordTupleSize, Vertex::stride());
// Release (unbind) all
m_object.release();
m_vertex.release();
m_program->release();
m_transform.setDataMove();
}
}
void Window::paintGL()
{
// Clear
glClear(GL_COLOR_BUFFER_BIT);
// Render using our shader
m_program->bind();
m_program->setUniformValue(u_texturecolors, m_texturecolors);
m_program->setUniformValue(u_worldToCamera, m_camera.toMatrix());
m_program->setUniformValue(u_cameraToView, m_projection);
//m_program->setUniformValue(u_worldToView, m_projection);
{
m_object.bind();
m_program->setUniformValue(u_modelToWorld, m_transform.toMatrix());
glDrawArrays(GL_TRIANGLES, 0, sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
m_object.release();
}
m_program->release();
}
关联的window.h标题:
class Window : public QOpenGLWindow,
protected QOpenGLFunctions
{
Q_OBJECT
// OpenGL Events
public:
Window();
~Window();
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
void teardownGL();
public slots:
void update() ;
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
// OpenGL State Information
QOpenGLBuffer m_vertex;
QOpenGLVertexArrayObject m_object;
QOpenGLShaderProgram *m_program;
// Shader Information
int u_modelToWorld;
int u_worldToCamera;
int u_cameraToView;
int u_texturecolors ;
QVector3D m_texturecolors ;
Camera3D m_camera;
QMatrix4x4 m_projection;
Transform3D m_transform;
};
最后是顶点标题:
class Vertex
{
public:
// Constructors
Q_DECL_CONSTEXPR Vertex();
Q_DECL_CONSTEXPR explicit Vertex(const QVector3D &position);
Q_DECL_CONSTEXPR Vertex(const QVector3D &position, const QVector3D &color);
Q_DECL_CONSTEXPR Vertex(const QVector3D &position, const QVector2D &texcoord);
// Accessors / Mutators
Q_DECL_CONSTEXPR const QVector3D& position() const;
Q_DECL_CONSTEXPR const QVector3D& color() const;
Q_DECL_CONSTEXPR const QVector2D& texcoord() const;
void setPosition(const QVector3D& position);
void setColor(const QVector3D& color);
void setTexcoord(const QVector2D& texcoord);
// OpenGL Helpers
static const int PositionTupleSize = 3;
static const int ColorTupleSize = 3;
static const int TexcoordTupleSize = 2 ;
static Q_DECL_CONSTEXPR int positionOffset();
static Q_DECL_CONSTEXPR int colorOffset();
static Q_DECL_CONSTEXPR int texcoordOffset();
static Q_DECL_CONSTEXPR int stride();
private:
QVector3D m_position;
QVector3D m_color;
QVector2D m_texcoord ;
};
Q_DECLARE_TYPEINFO(Vertex, Q_MOVABLE_TYPE);
// Constructors
Q_DECL_CONSTEXPR inline Vertex::Vertex() {}
Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position) : m_position(position) {}
Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position, const QVector3D &color) : m_position(position), m_color(color) {}
Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position, const QVector2D &texcoord) : m_position(position), m_texcoord(texcoord) {}
// Accessors / Mutators
Q_DECL_CONSTEXPR inline const QVector3D& Vertex::position() const { return m_position; }
Q_DECL_CONSTEXPR inline const QVector3D& Vertex::color() const { return m_color; }
Q_DECL_CONSTEXPR inline const QVector2D& Vertex::texcoord() const { return m_texcoord; }
void inline Vertex::setPosition(const QVector3D& position) { m_position = position; }
void inline Vertex::setColor(const QVector3D& color) { m_color = color; }
void inline Vertex::setTexcoord(const QVector2D& texcoord) { m_texcoord = texcoord; }
// OpenGL Helpers
Q_DECL_CONSTEXPR inline int Vertex::positionOffset() { return offsetof(Vertex, m_position); }
Q_DECL_CONSTEXPR inline int Vertex::colorOffset() { return offsetof(Vertex, m_color); }
Q_DECL_CONSTEXPR inline int Vertex::texcoordOffset() { return offsetof(Vertex, m_texcoord); }
Q_DECL_CONSTEXPR inline int Vertex::stride() { return sizeof(Vertex); }
#endif // VERTEX_HSR
答案 0 :(得分:0)
您正尝试在顶点着色器中应用纹理。相反,您应该在片段着色器中应用它。通过删除纹理并提供纹理坐标来修改顶点着色器:
out vec2 TexCoord;
void main()
{
// Removed this line: vColor = texture2D(tex, Texc);
TexCoord = Texc;
}
修改片段着色器以像在顶点着色器中一样读取纹理:
in vec2 TexCoord;
void main()
{
fColor = texture2D(tex, TexCoord);
}