Answer the question
In order to leave comments, you need to log in
Why is color displayed instead of texture in open gl?
I have a cube on stage. I draw a texture on it. But instead of the texture, only the green color is shown.
Please tell me how to fix.
I bring my code.
attribute vec4 a_position;
attribute vec2 a_textcoord;
attribute vec3 a_normal;
uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
varying vec4 v_position;
varying vec2 vTexcoord;
varying vec3 v_normal;
void main(void)
{
mat4 mvMatrix = viewMatrix * modelMatrix;
gl_Position = projectionMatrix * mvMatrix * a_position;
vTexcoord = a_textcoord;
v_normal = normalize(vec3(mvMatrix * vec4(a_normal , 0.0)));
v_position = mvMatrix * a_position;
}
uniform sampler2D u_texture;
varying vec4 v_position;
varying vec2 v_texcoord;
varying vec3 v_normal;
void main(void)
{
gl_FragColor = texture2D(u_texture, v_texcoord);
}
#include "mainwindow.h"
#include <QApplication>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QOpenGLWidget(parent),
m_texture(nullptr),
m_vertexBuffer(QOpenGLBuffer::VertexBuffer),
m_indexBuffer(QOpenGLBuffer::IndexBuffer){
}
MainWindow::~MainWindow()
{
}
void MainWindow::initializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
Scene1();
}
void MainWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 ViewMatrix;
ViewMatrix.setToIdentity();
ViewMatrix.translate(0.0f, 0.0f, -5.0f);
QMatrix4x4 modelmatrix;
modelmatrix.setToIdentity();
m_texture->bind(0);
m_shaderProgramm.bind();
m_shaderProgramm.setUniformValue("projectionMatrix", m_projectionMatrix);
m_shaderProgramm.setUniformValue("viewMatrix", ViewMatrix);
m_shaderProgramm.setUniformValue("modelMatrix", modelmatrix);
m_shaderProgramm.setUniformValue("u_texture", 0);
// Bind buffers
m_vertexBuffer.bind();
m_indexBuffer.bind();
int offset = 0;
int location = m_shaderProgramm.attributeLocation("a_position");
m_shaderProgramm.enableAttributeArray(location);
m_shaderProgramm.setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
location = m_shaderProgramm.attributeLocation("vTexcoord");
m_shaderProgramm.enableAttributeArray(location);
m_shaderProgramm.setAttributeBuffer(location, GL_FLOAT, offset, 2, sizeof(VertexData));
glDrawElements(GL_TRIANGLES, m_indexBuffer.size(), GL_UNSIGNED_INT, 0);
}
void MainWindow::Scene1()
{
initShaders();
initCube();
}
void MainWindow::initShaders()
{
if (!m_shaderProgramm.addShaderFromSourceFile(QOpenGLShader::Vertex, "://vshader.vsh"))
close();
if (!m_shaderProgramm.addShaderFromSourceFile(QOpenGLShader::Fragment, "://fshader.fsh"))
close();
if (m_shaderProgramm.link())
close();
if (!m_shaderProgramm.bind())
close();
}
void MainWindow::initCube()
{
float width_div_2 =1;
QVector<VertexData> vertexes;
//front, right, top, back, left, down
тут геометрия куба
//front, right, top, back, left, down
QVector<GLuint> indexes;
for (int i = 0; i < 24; i += 4){
indexes.append(i + 0);
indexes.append(i + 1);
indexes.append(i + 2);
indexes.append(i + 2);
indexes.append(i + 1);
indexes.append(i + 3);
}
// Create and fill vertex buffer and then -> release
m_vertexBuffer.create();
m_vertexBuffer.bind();
m_vertexBuffer.allocate(vertexes.constData(), vertexes.size() * sizeof(VertexData));
m_vertexBuffer.release();
// Create and fill index buffer and then -> release
m_indexBuffer.create();
m_indexBuffer.bind();
m_indexBuffer.allocate(indexes.constData(), indexes.size() * sizeof(GLuint));
m_indexBuffer.release();
m_texture = new QOpenGLTexture(QImage("://cube.jpg").mirrored());
m_texture->setMinificationFilter(QOpenGLTexture::Nearest);
m_texture->setMagnificationFilter(QOpenGLTexture::Linear);
m_texture->setWrapMode(QOpenGLTexture::Repeat);
}
Answer the question
In order to leave comments, you need to log in
variables of type varuing are passed from the vertex shader to the fragment shader and they are only read in the fragment one! Here is the link https://ru.stackoverflow.com/questions/766483/glsl ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question