2

我正在使用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;”时,一切看起来都很正常。我不知道我在这里做错了什么。一位朋友试图帮助我,我们删除了一些不必要的东西,但我们什么也没有运行。

4

2 回答 2

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 真的就是这么令人困惑,我做对了只是运气不好。只是发布这个让人们知道

于 2013-09-11T14:18:44.473 回答
1

您无法添加纹理可能是由于:

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

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


tm_l2g是变换局部obj空间->世界空间(Modelview)
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 渲染代码(初始化不包括其唯一的渲染代码,它们可以在此处找到)

//------------------------------------------------------------------
// 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 有时也有点小错误,但对于我的目的来说已经足够了。

于 2013-09-07T11:27:05.457 回答