c++ - 如何让纹理在 OpenGL 中工作?

标签 c++ opengl textures

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

目前看来纹理只有在启用 mipmap 时才有效。这有一个缺点:唯一使用的 mipmap 是索引为零的 mipmap,也就是 1 色 1x1 像素的 mipmap。我尝试将 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;”时,一切看起来都很正常。我不知道我在这里做错了什么。一位 friend 试图帮助我,我们删除了一些不必要的东西,但我们没有运行任何东西。

最佳答案

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

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 真的就是这么令人困惑,我做对了真是太幸运了。只是张贴这个让人们知道

关于c++ - 如何让纹理在 OpenGL 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638394/

相关文章:

C++ 如何读取函数的前几个字节? (32位机)

c++ - 如何使用 OpenCV 和 C++ 检测车牌中的数字?

c++ - 重定向标准输出时调用 dup2 失败

c++ - 在 OpenGL 中绘制有限 segmentation 的贝塞尔曲线

iOS+SceneKit : How to apply toon shader on texture?

c++ - 为什么出现 "no matching overload found"错误,即使我定义了它们

c++ - 从视口(viewport)坐标转换为 [-1,1]^2 顶点 [OpenGL]

c++ - 如何使用 vtk 旋转/平移/翻转 vtkActor2D 对象?

c++ - 在执行 glTexImage2D 和 glTexSubImage2D 之前是否需要调用 glGenTextures 和 glBindTexture

opengl - SDL/OpenGL 纹理透明度