如何让纹理在 OpenGL 中工作?

Posted

技术标签:

【中文标题】如何让纹理在 OpenGL 中工作?【英文标题】:How do I get textures to work in OpenGL? 【发布时间】:2013-09-05 13:59:16 【问题描述】:

我正在使用http://arcsynthesis.org/gltut/ 的教程学习OpenGL,它是必需的,我必须使用它。大多数情况下,我想将教程 15 中的纹理应用到教程 7(UBO 世界)中的对象上。

目前,纹理似乎仅在打开 mipmap 时才有效。这有一个缺点:唯一使用的 mipmap 是索引为零的那个,那就是 1 个彩色 1x1 像素的一个。我尝试将 mipmap 的最低级别设置得更高或完全关闭 mipmap,但即使这样也不能解决问题,因为那样一切都会变黑。现在我将列出我的程序中最重要的部分

编辑:我想我会添加更多细节...

顶点着色器有这样的东西:

#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
layout(location = 2) in vec3 normal;
//Added these later 
layout(location = 5) in vec2 texCoord;
out vec2 colorCoord;

smooth out vec4 interpColor;
out vec3 vertexNormal;
out vec3 modelSpacePosition;
out vec3 cameraSpacePosition;

uniform mat4 worldToCameraMatrix;
uniform mat4 modelToWorldMatrix;
uniform mat3 normalModelToCameraMatrix;
uniform vec3 dirToLight;
uniform vec4 lightIntensity;
uniform vec4 ambientIntensity;
uniform vec4 baseColor;

uniform mat4 cameraToClipMatrix;

void main()

vertexNormal = normal;
vec3 normCamSpace = normalize(normalModelToCameraMatrix * vertexNormal);
cameraSpacePosition = normCamSpace;

float cosAngIncidence = dot(normCamSpace, dirToLight);
cosAngIncidence = clamp(cosAngIncidence, 0, 1);
modelSpacePosition.x = position.x;
modelSpacePosition.y = position.y;
modelSpacePosition.z = position.z;

vec4 temp = modelToWorldMatrix * position;
temp = worldToCameraMatrix * temp;
gl_Position = cameraToClipMatrix * temp;

interpColor = ((lightIntensity * cosAngIncidence) + (ambientIntensity)) * baseColor;
colorCoord= texCoord ;

片段着色器如下:

#version 330
in vec3 vertexNormal;
in vec3 modelSpacePosition;

smooth in vec4 interpColor;

uniform vec3 modelSpaceLightPos;
uniform vec4 lightIntensity2;
uniform vec4 ambientIntensity2;

out vec4 outputColor;

//Added later
in vec2 colorCoord;
uniform sampler2D colorTexture;


void main()

vec3 lightDir2 = normalize(modelSpacePosition - modelSpaceLightPos);

float cosAngIncidence2 = dot(normalize(vertexNormal), lightDir2);
cosAngIncidence2 = clamp(cosAngIncidence2, 0,  1);

float light2DistanceSqr = dot(modelSpacePosition - modelSpaceLightPos, modelSpacePosition - modelSpaceLightPos);

//added
vec4 texture2 = texture(colorTexture, colorCoord);

outputColor = ((ambientIntensity2 + (interpColor*2))/4) + 
((((interpColor) * lightIntensity2/200 * cosAngIncidence2) + (ambientIntensity2* interpColor )) 
/( ( sqrt(light2DistanceSqr) + light2DistanceSqr)/200 ));
//No outputColor for texture testing
outputColor =  texture2 ; 



它们都是着色器。以下是添加到 .cpp 中的部分:

#include <glimg/glimg.h>
#include "../framework/directories.h"     
[...]
const int g_colorTexUnit = 0;
GLuint g_checkerTexture = 0;

这是纹理的加载器:

void LoadCheckerTexture()

try


std::string filename(LOCAL_FILE_DIR);
filename += "checker.dds";
std::auto_ptr<glimg::ImageSet>
 pImageSet(glimg::loaders::dds::LoadFromFile(filename.c_str()));

glGenTextures(1, &g_checkerTexture);
glBindTexture(GL_TEXTURE_2D, g_checkerTexture);

glimg::SingleImage image = pImageSet->GetImage(0, 0, 0);
glimg::Dimensions dims = image.GetDimensions();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, dims.width, dims.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, image.GetImageData());

glBindTexture(GL_TEXTURE_2D, 0);

catch(std::exception &e)

printf("%s\n", e.what());
throw;


当然,我在 void init() 中有这个:

LoadCheckerTexture();

然后在渲染对象时:

glActiveTexture(GL_TEXTURE0 + g_colorTexUnit);
glBindTexture(GL_TEXTURE_2D,g_checkerTexture);
g_pLeftMesh->Render();
glBindSampler(g_colorTexUnit, 0);
glBindTexture(GL_TEXTURE_2D, 0);

有了这一切,我的一切都变成了黑色,但是当我将 outputColor 方程更改为“texture + outputColor;”时,一切看起来都很正常。我不知道我在这里做错了什么。一位朋友试图帮助我,我们删除了一些不必要的东西,但没有任何运行。

【问题讨论】:

最后一行是什么: glBindSampler(g_colorTexUnit, 0);... 也许应该是 glActiveTexture ?第二件事:你是否在着色器中设置了制服:glUniform1i("colorTexture", g_colorTexUnit)? 在编译着色器时可能会出现一些错误,因为变量名称纹理与函数名称纹理()数学运算?尝试重命名。 @fen:这就是采样器对象在 OpenGL 3.3+ 中未绑定的方式。我不认为这有什么问题,因为 arcsynthesis 教程教授现代 OpenGL(包括用于更改纹理状态的采样器对象) @fen:嗯,按照安东的说法,应该不会有什么不同……而且确实没有。 @user1837009: 试过了,改名为texture2还是没什么区别 【参考方案1】:

好的,伙计们,我已经完成了整个工作,并且确实设法以某种方式让它运行起来。首先我必须添加采样器:

GLuint g_samplers;

//Add Later
void CreateSamplers()

glGenSamplers(1, &g_samplers);

    glSamplerParameteri(g_samplers, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glSamplerParameteri(g_samplers, GL_TEXTURE_WRAP_T, GL_REPEAT);

//Linear mipmap Nearest
glSamplerParameteri(g_samplers, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(g_samplers, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

我也将这个添加到文件中:

glimg::OpenGLPixelTransferParams xfer = glimg::GetUploadFormatType(pImageSet->GetFormat(), 0);

glimg::SingleImage image = pImageSet->GetImage(0, 0, 0);
glimg::Dimensions dims = image.GetDimensions();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dims.width, dims.height, 0, 
  xfer.format, xfer.type, image.GetImageData());

xfer 变量确实将格式和类型调整为 dds。 渲染代码也变成了这样:

//Added necessary 
glActiveTexture(GL_TEXTURE0 + g_colorTexUnit);
glBindTexture(GL_TEXTURE_2D,g_checkerTexture);
glBindSampler(g_colorTexUnit, g_samplers);
g_pLeftMesh->Render();
glBindSampler(g_colorTexUnit, 0);
glBindTexture(GL_TEXTURE_2D, 0);

当然,在 init() 的末尾我需要添加 CreateSamplers 的东西:

//Added this later
LoadCheckerTexture();
CreateSamplers();

我很抱歉给这一切带来了麻烦,但我猜 OpenGL 真的就是这么令人困惑,而且我做对了只是运气不好。只是发布这个让人们知道

【讨论】:

【参考方案2】:

您添加纹理失败的原因可能是:

您是否为对象添加了纹理坐标? (这是最可能的原因,因为您正在向非纹理教程添加纹理),将纹理添加到 VAO。 您是否添加了uniform textureunit (Sampler2D)? (它必须是统一的,否则纹理将无法正常工作) 您的纹理是否已加载、绑定、启用 (GL_TEXTURE_2D)? 您的活动纹理单元 - 0 吗?如果不更改布局/多纹理坐标或设置活动纹理0

这两个代码是简单的纹理着色器(纹理单元0),没有特殊的东西(如光、混合、凹凸...):

tm_l2g 是变换局部 obj 空间 -> 世界空间(模型视图) tm_g2s 是变换世界空间 -> 屏幕空间(投影) pos 是顶点坐标 txt 是纹理坐标 col 是颜色

不要忘记将统一名称和布局位置更改为您的。

顶点:

//------------------------------------------------------------------
#version 420 core
//------------------------------------------------------------------
uniform mat4x4 tm_l2g;
uniform mat4x4 tm_g2s;

layout(location=0) in vec3 pos;
layout(location=1) in vec4 col;
layout(location=2) in vec2 txr;

out smooth vec4 pixel_col;
out smooth vec2 pixel_txr;
//------------------------------------------------------------------
void main(void)
    
    vec4 p;
    p.xyz=pos;
    p.w=1.0;
    p=tm_l2g*p;
    p=tm_g2s*p;
    gl_Position=p;
    pixel_col=col;
    pixel_txr=txr;
    
//------------------------------------------------------------------

片段:

//------------------------------------------------------------------
#version 420 core
//------------------------------------------------------------------
in smooth vec4 pixel_col;
in smooth vec2 pixel_txr;
uniform sampler2D   txr_texture0;
out layout(location=0) vec4 frag_col;
//------------------------------------------------------------------
void main(void)
    
    vec4 col;
    col=texture(txr_texture0,pixel_txr.st);
    frag_col=col*pixel_col;
    
//------------------------------------------------------------------

[edit1] CPU 旧式 OpenGL 渲染代码(初始化不包括其唯一的渲染代码,它们可以在 here 找到)

//------------------------------------------------------------------
// set modelview,projection,textures,bind GLSL programs...
GLfloat a=10.0,z=0.0;
glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
// textured quad
glTexCoord2f(0.0,0.0); glVertex3f(-a,-a,z);
glTexCoord2f(0.0,1.0); glVertex3f(-a,+a,z);
glTexCoord2f(1.0,1.0); glVertex3f(+a,+a,z);
glTexCoord2f(1.0,0.0); glVertex3f(+a,-a,z);
// reverse order quad to be shore that at least one passes by CULL_FACE
glTexCoord2f(1.0,0.0); glVertex3f(+a,-a,z);
glTexCoord2f(1.0,1.0); glVertex3f(+a,+a,z);
glTexCoord2f(0.0,1.0); glVertex3f(-a,+a,z);
glTexCoord2f(0.0,0.0); glVertex3f(-a,-a,z);
glEnd();

//------------------------------------------------------------------

[edit2] 好的,这里是 VAO/VBO 渲染代码,...

//------------------------------------------------------------------------------
// enum of VBO locations (it is also your layout location) I use enums for simple in code changes
enum _vbo_enum
    
    _vbo_pos=0,     // glVertex
    _vbo_col,       // glColor
    _vbo_tan,       // glNormal
    _vbo_unused0,   // unused (at least i dont see anything at this location in your code)
    _vbo_unused1,   // unused (at least i dont see anything at this location in your code)
    _vbo_txr,       // glTexCoord
    _vbos
    ;
//------------------------------------------------------------------------------
// 'global' names and size for OpenGL mesh in VAO/VBO ... similar ot texture names/handles
GLuint vao[1],vbo[_vbos],num_pnt=0;
//------------------------------------------------------------------------------
void VAO_init_cube()    // call this before VAO use,...but after OpenGL init !
    
    //[1] first you need some model to render (mesh), here is a simple cube
    // size,position of cube - change it that it is visible in your scene
    const GLfloat a=1.0,x=0.0,y=0.0,z=0.0;
    // cube points 3f x,y,z
    GLfloat mesh_pos[]=
        
        x-a,y-a,z-a,x-a,y+a,z-a,x+a,y+a,z-a,x+a,y-a,z-a,
        x-a,y-a,z+a,x-a,y+a,z+a,x+a,y+a,z+a,x+a,y-a,z+a,
        x-a,y-a,z-a,x-a,y-a,z+a,x+a,y-a,z+a,x+a,y-a,z-a,
        x-a,y+a,z-a,x-a,y+a,z+a,x+a,y+a,z+a,x+a,y+a,z-a,
        x-a,y-a,z-a,x-a,y+a,z-a,x-a,y+a,z+a,x-a,y-a,z+a,
        x+a,y-a,z-a,x+a,y+a,z-a,x+a,y+a,z+a,x+a,y-a,z+a,
        ;
    // cube colors 3f r,g,b
    GLfloat mesh_col[]=
        
        0.0,0.0,0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,0.0,0.0,
        0.0,0.0,1.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,1.0,
        0.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,0.0,0.0,
        0.0,1.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,
        0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,
        1.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,1.0,0.0,1.0,
        ;
    // cube normals 3f x,y,z
    GLfloat mesh_tan[]=
        
        -0.6,-0.6,-0.6,-0.6,+0.6,-0.6,+0.6,+0.6,-0.6,+0.6,-0.6,-0.6,
        -0.6,-0.6,+0.6,-0.6,+0.6,+0.6,+0.6,+0.6,+0.6,+0.6,-0.6,+0.6,
        -0.6,-0.6,-0.6,-0.6,-0.6,+0.6,+0.6,-0.6,+0.6,+0.6,-0.6,-0.6,
        -0.6,+0.6,-0.6,-0.6,+0.6,+0.6,+0.6,+0.6,+0.6,+0.6,+0.6,-0.6,
        -0.6,-0.6,-0.6,-0.6,+0.6,-0.6,-0.6,+0.6,+0.6,-0.6,-0.6,+0.6,
        +0.6,-0.6,-0.6,+0.6,+0.6,-0.6,+0.6,+0.6,+0.6,+0.6,-0.6,+0.6,
        ;
    // cube texture coords 2f s,t
    GLfloat mesh_txr[]=
        
        0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,
        0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,
        0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,
        0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,
        0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,
        0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,
        ;
    // init VAO/VBO
    glGenVertexArrays(1,vao);   // allocate 1 x VAO
    glGenBuffers(_vbos,vbo);    // allocate _vbos x VBO
    // copy mesh to VAO/VBO ... after this you do not need the mesh anymore
    GLint i,sz,n;               // n = number of numbers per 1 entry
    glBindVertexArray(vao[0]);
    num_pnt=sizeof(mesh_pos)/(sizeof(GLfloat)*3);   // num of all points in mesh

    i=_OpenGLVAOgfx_pos; n=3; sz=sizeof(GLfloat)*n;
    glBindBuffer(GL_ARRAY_BUFFER,vbo[i]);
    glBufferData(GL_ARRAY_BUFFER,sz*num_pnt,mesh_pos,GL_STATIC_DRAW);
    glEnableVertexAttribArray(i);
    glVertexAttribPointer(i,n,GL_FLOAT,GL_FALSE,0,0);

    i=_OpenGLVAOgfx_col; n=3; sz=sizeof(GLfloat)*n;
    glBindBuffer(GL_ARRAY_BUFFER,vbo[i]);
    glBufferData(GL_ARRAY_BUFFER,sz*num_pnt,mesh_col,GL_STATIC_DRAW);
    glEnableVertexAttribArray(i);
    glVertexAttribPointer(i,n,GL_FLOAT,GL_FALSE,0,0);

    i=_OpenGLVAOgfx_tan; n=3; sz=sizeof(GLfloat)*n;
    glBindBuffer(GL_ARRAY_BUFFER,vbo[i]);
    glBufferData(GL_ARRAY_BUFFER,sz*num_pnt,mesh_tan,GL_STATIC_DRAW);
    glEnableVertexAttribArray(i);
    glVertexAttribPointer(i,n,GL_FLOAT,GL_FALSE,0,0);

    i=_OpenGLVAOgfx_txr; n=2; sz=sizeof(GLfloat)*n;
    glBindBuffer(GL_ARRAY_BUFFER,vbo[i]);
    glBufferData(GL_ARRAY_BUFFER,sz*num_pnt,mesh_txr,GL_STATIC_DRAW);
    glEnableVertexAttribArray(i);
    glVertexAttribPointer(i,n,GL_FLOAT,GL_FALSE,0,0);

    glBindVertexArray(0);
    
//------------------------------------------------------------------------------
void VAO_draw() // call this to draw your mesh,... need to enable and bind textures,...  before use
    
    glDisable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glBindVertexArray(vao[0]);
     glEnableVertexAttribArray(_vbo_pos);
     glEnableVertexAttribArray(_vbo_col);
     glEnableVertexAttribArray(_vbo_tan);
    glDisableVertexAttribArray(_vbo_unused0);
     glEnableVertexAttribArray(_vbo_txr);

    glDrawArrays(GL_QUADS,0,num_pnt);

    glDisableVertexAttribArray(_vbo_pos);
    glDisableVertexAttribArray(_vbo_col);
    glDisableVertexAttribArray(_vbo_tan);
    glDisableVertexAttribArray(_vbo_unused0);
    glDisableVertexAttribArray(_vbo_unused1);
    glDisableVertexAttribArray(_vbo_txr);
    glBindVertexArray(0);
    
//------------------------------------------------------------------------------
void VAO_exit() // clean up ... call this when you do not need VAO/VBO anymore
    
    glDisableVertexAttribArray(_vbo_pos);
    glDisableVertexAttribArray(_vbo_col);
    glDisableVertexAttribArray(_vbo_tan);
    glDisableVertexAttribArray(_vbo_unused0);
    glDisableVertexAttribArray(_vbo_unused1);
    glDisableVertexAttribArray(_vbo_txr);
    glBindVertexArray(0);
    glDeleteVertexArrays(1,vao);
    glDeleteBuffers(_vbos,vbo);
    
//------------------------------------------------------------------------------

[edit3] 如果你是 win32/64 用户,你可以试试我的 IDE for GLSL

它非常简单易用,但不能更改纹理/属性位置。按 [F1] 寻求帮助,... [F9] 运行 [F10] 返回正常 OpenGL 模式。 txt-editor 有时也有点小错误,但对于我的目的来说已经足够了。

GLSL IDE

【讨论】:

感谢这里的帮助,但这似乎并没有太大变化。为了提供更多输入,我对着色器进行了扩展。不知道,这是否能让我更轻松。 您的着色器现在看起来不错,但在 CPU 代码中您需要将 texcoords 添加到网格中。 (它们来自非纹理教程,因此很有可能没有纹理坐标)检查调用 glTexCoord2f(0.1,0.1);在网格渲染之前,看看颜色是否改变。如果是,则意味着您的混搭没有纹理坐标,需要添加它们(在 CPU 代码中而不是在着色器中!) 你也可以尝试不同的纹理坐标,而不仅仅是 0.1,0.1 只是为了保持岸边(我不知道你的纹理看起来像你现在的想法是打出不同的颜色) 恐怕不能申请“glTexCoord2f(0.1,0.1);”因为它声称“glTexCoord2f”是未定义的。再一次,我怀疑我是否应该应用它,因为教程从不使用它......然后我又不知道还能尝试什么。 这很奇怪,glTexCoord 是标准的 OpenGL 1.0 函数,所以它必须在那里,...现代 GLSL 使用 VAO 代替...您需要创建另一个填充您的文本坐标的 VBO 并将其绑定到您的VAO而不是使用glTexCoord,但是对于测试应该足够我之前写的了

以上是关于如何让纹理在 OpenGL 中工作?的主要内容,如果未能解决你的问题,请参考以下文章

无法让 Stencil Buffer 在 iOS 4+ 中工作(5.0 工作正常)。 [OpenGL ES 2.0]

OpenGL 和 SDL '#version required and missing' 但在调试中工作

OpenGL ES 无法使 LUMINANCE FBO 在 Android 中工作

QT和OpenGL如何正确集成并显示纹理

在 Linux 上,如何将桌面捕获到 OpenGL 纹理?

如何在opengl中平滑多个四边形的纹理