我正在Qt中做一个OpenGL应用程序,我在纹理方面遇到了问题。我的对象涂有单色纹理,但是我的图像是.png格式的彩色图像。 我在Visual Studio中使用C ++编程编写了应用程序donde,并且运行良好。
我试图禁用照度,但没有提供任何更改。 我还尝试强制着色器将所有对象绘制为红色,并且它可以正常工作,因此该表面允许我使用颜色进行绘制。 另外,我有一个不使用纹理的模式,我只用材质绘画,效果很好。 我看到了Qt提供的示例,它与我的并没有太大的区别。创建纹理的方式就像我的一样,在示例中,一个立方体被涂上了彩色纹理。
这是纹理类:
texture::texture()
{
initializeOpenGLFunctions();
}
texture::texture(QVector<QString> rutas, QVector<TexturesTypes> tipo)
{
initializeOpenGLFunctions();
for (int i = 0; i < rutas.size(); i++)
{
QString filename = rutas[i];
TexturesTypes tipoTextura = tipo[i];
QImage imagen = QImage(filename);
if(imagen.isNull())
{
std::cout<<"IS NULL"<<std::endl;
}
QOpenGLTexture *texture = new QOpenGLTexture(QImage(filename).mirrored());
if (texture == nullptr)
{
std::cout << filename.toStdString() << " cannot be loaded" << std::endl;
return;
}
std::cout<<"Textura cargada correctamente"<<std::endl;
//Construimos la textura con las imágenes que queremos y la función de cada imagen
switch (tipoTextura)
{
case COLOR:
{
color = texture;
color->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
color->setMagnificationFilter(QOpenGLTexture::Linear);
color->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
color->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
color->generateMipMaps();
break;
}
case BRILLO:
{
brillo = texture;
brillo->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
brillo->setMagnificationFilter(QOpenGLTexture::Linear);
brillo->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
brillo->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
brillo->generateMipMaps();
break;
}
case BUMPMAPPING:
{
bumpMapping = texture;
bumpMapping->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
bumpMapping->setMagnificationFilter(QOpenGLTexture::Linear);
bumpMapping->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
bumpMapping->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
bumpMapping->generateMipMaps();
break;
}
case DISPLACEMENTMAPPING:
{
displacementMapping = texture;
displacementMapping->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
displacementMapping->setMagnificationFilter(QOpenGLTexture::Linear);
displacementMapping->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
displacementMapping->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
displacementMapping->generateMipMaps();
break;
}
}
}
}
/**
* @brief Obtener una de las imágenes que forman la textura
* @param tipo Tipode imagen que se quiere obtener
* @return imagen Puntero a la imagen que se ha obtenido
*/
QOpenGLTexture* texture::getTextura(TexturesTypes tipo)
{
switch (tipo)
{
case COLOR:
{
return this->color;
}
case BRILLO:
{
return this->brillo;
}
case BUMPMAPPING:
{
return this->bumpMapping;
}
case DISPLACEMENTMAPPING:
{
return this->displacementMapping;
}
}
}
/**
* @brief Aplicar la textura al shader
* @param shader Shader al que se le va a aplicar la textura
*/
void texture::aplicar(shaderProgram* shader)
{
shader->use();
if (color != nullptr)
{
shader->setUniform("TexSamplerColor", 0);
color->bind(GL_TEXTURE0);
}
if (brillo != nullptr)
{
shader->setUniform("TexSamplerBrillo", 1);
brillo->bind(GL_TEXTURE1);
}
}
texture::~texture()
{
color->destroy();
brillo->destroy();
bumpMapping->destroy();
displacementMapping->destroy();
delete color;
delete brillo;
delete bumpMapping;
delete displacementMapping;
}
这是片段着色器:
#version 400
in vec3 position;
in vec3 normal;
in vec2 texCoord;
uniform vec3 lightPosition;
uniform vec3 Ia;
uniform vec3 Id;
uniform vec3 Is;
uniform sampler2D TexSamplerColor;
uniform sampler2D TexSamplerBrillo;
layout (location = 0) out vec4 FragColor;
vec3 ads(vec4 texColor, vec4 texBrillo)
{
vec3 Kad = texColor.rgb;
vec3 Ks = texBrillo.rgb;
float Shininess = texBrillo.a;
Shininess = Shininess*200;
vec3 n;
if (gl_FrontFacing) {
n = normalize(normal);
} else {
n = normalize(-normal);
}
vec3 l = normalize( lightPosition-position );
vec3 v = normalize( -position );
vec3 r = reflect( -l, n );
return (Ia * Kad) + (Id * Kad * max( dot(l, n), 0.0)) + (Is * Ks * pow( max( dot(r,v), 0.0), Shininess ));
}
void main() {
vec4 texColor = texture(TexSamplerColor, texCoord);
vec4 texBrillo = texture(TexSamplerBrillo, texCoord);
FragColor = vec4(ads(texColor,texBrillo), 1.0);
}
我希望将纹理很好地应用于我的对象,但是我拥有的是所有具有单色纹理的对象。
由于我的声誉,我无法发布图片,因此我在此处留下了一个链接: https://i.ibb.co/brTgchK/textura.png