如何使纹理在OpenGL中工作? [英] How do I get textures to work in OpenGL?

查看:51
本文介绍了如何使纹理在OpenGL中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http://arcsynthesis.org/gltut/上的教程来学习OpenGL,它是必需的,我必须使用它.通常,我想将教程15中的纹理应用于教程7(带有UBO的世界)中的对象.

I'm using the tutorials on http://arcsynthesis.org/gltut/ to learn OpenGL, it's required, I have to use it. Mostly I want to apply the textures from Tutorial 15 onto objects in tutorial 7 (world with UBO).

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

For now it seemed like the textures only work when mipmaps are turned on. This comes with a downside: The only mipmap used is the one with an index of zero, and that's the 1 colored 1x1 pixel one. I tried setting the minimum level of a mipmap higher or turning off mipmaps entirely, but even that doesn't fix thing, because then everything turns pitch black. Now I'll list the most important parts of my program

我想我会添加更多详细信息...

I guess I'll add more details...

顶点着色器具有以下内容:

The vertex shader has something like this:

#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中的部分:

Those were both shaders. And here are the parts added to the .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()中得到了这个

Naturally I've got this in 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;"时,一切看起来都很正常.我不知道我在做什么错.一个朋友试图帮助我,我们删除了一些不必要的东西,但没有任何帮助.

With all of this, I get put pitch black for everything, however when I change the outputColor equation into "texture + outputColor;", everything looks normal. I have no idea what I'm doing wrong here. A friend tried to help me, we removed some unnecessairy stuff, but we got nothing running.

推荐答案

好的,我已经完成了整个工作,并设法以某种方式使其运行.首先,我必须添加采样器:

Ok guys, I've worked on this whole thing, and did manage to somehow get it running. First off I had to add samplers:

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);
}

我还将此添加到了文件中:

I also added this to the file thing:

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的格式和类型. 渲染代码也变成了这样:

The xfer variable does get the format and type adjusted to the dds. Also the render code got turned into this:

//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内容:

And of course at the end of init() I needed to add the CreateSamplers thing:

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

对于所有这些麻烦,我深表歉意,但是猜想OpenGL确实是令人困惑的事情,我弄对了真是愚蠢的运气.只是发布此信息,以便人们知道

I'm sorry for all the trouble with all this, but guess OpenGL really is just this confusing and it was just dumb luck that I got it right. Just posting this so that people know

这篇关于如何使纹理在OpenGL中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆