简体   繁体   English

纹理渲染和VBO的[OpenGL / SDL / C ++]

[英]Texture rendering and VBO's [OpenGL/SDL/C++]

So, I've been working on a little game project for a bit and I've hit a snag that's annoying me to no end. 因此,我已经在一个小游戏项目上工作了一段时间,但遇到了一个困扰,使我无休止。 I load an obj file which then gets rendered after being put into a VBO. 我加载一个obj文件,然后将其放入VBO中进行渲染。 This part works fine, no problemo. 这部分工作正常,没有问题。 However, I've been trying to get it to render the accompanying texture with the supplied UVs with no success. 但是,我一直试图使它用提供的UV渲染伴随的纹理没有成功。 Currently, I just get a matte green colouration on my model. 目前,我的模型上只有磨砂绿色。 Upon investigating it in GDE, I've seen that texture gets loaded fine and occupies the GL_TEXTURE0 unit, so that's not the issue. 在GDE中进行研究后,我发现纹理可以很好地加载并占据GL_TEXTURE0单元,所以这不是问题。 I believe it may be my binding but I have no idea why this would fail... 我相信这可能是我的约束力,但我不知道为什么这会失败...

void Model_Man::render_models()
{
    for(int x=0; x<models.size(); x++)
    {
        if(models.at(x).visible==true)
        {
            glBindBuffer(GL_ARRAY_BUFFER,models.at(x).t_buff);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,models.at(x).i_buff);

            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_FLOAT,0,0);

            glClientActiveTexture(GL_TEXTURE0);

            glTexCoordPointer(2,GL_FLOAT,0,&models.at(x).uvs[0]);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);

            glActiveTexture(GL_TEXTURE0);
            int tex_loc = glGetUniformLocation(models.at(x).shaderid,"color_texture");
            glUniform1i(tex_loc,GL_TEXTURE0);
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, models.at(x).mats.at(0).texid);

            c_render.use_program(models.at(x).shaderid);
            glDrawElements(GL_TRIANGLES,models.at(x).f_index.size()*3,GL_UNSIGNED_INT,0);
            c_render.use_program();
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            glDisable(GL_TEXTURE_2D);
        }
    }
}

And my shader files... 还有我的着色器文件...

Shader.frag Shader.frag

uniform sampler2D color_texture;
void main() {
    // Set the output color of our current pixel
    gl_FragColor = texture2D(color_texture, gl_TexCoord[0].st);
}

Shader.vert Shader.vert

void main() {           
    gl_TexCoord[0] = gl_MultiTexCoord0;

    // Set the position of the current vertex 
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

And yes, I know I'm currently being horribly inefficient with my render loop :P but I'm already planning on refactoring it, I am just attempting to get this single model to draw correctly with everything I'm aiming to do. 是的,我知道我的render loop:P目前效率很低,但是我已经在计划重构它,我只是想让这个单一模型正确地绘制出我打算做的所有事情。 I have no clue why it wouldn't be rendering with the texture correctly applied - unless it's because I need to interleave my arrays but I'm still supplying it with uv data so I don't see why it fails. 我不知道为什么不能正确应用纹理来渲染-除非是因为我需要交织数组,但是我仍在向它提供uv数据,所以我不明白为什么它会失败。

The call that set the sampler uniform shall not set GL_TEXTUE0, but actually 0. 设置采样器统一的调用不应设置GL_TEXTUE0,而实际上应设置为0。

Indeed: 确实:

glUniform1i(location, 0)

For setting up a sampler uniform do: 要设置采样器制服,请执行以下操作:

glUseProgram(progId);
// ...
glActiveTexture(GL_TEXTURE0 + texUnit);
glBindTexture(texId);
glUniform1i(texUnit);

The main concept is that the uniform variable are a shader program state (it is mantained until you re-link the program or reset the uniform value). 主要概念是统一变量是着色器程序状态(在重新链接程序或重置统一值之前一直保持该状态)。 Without binding a program, glUniform1i shall fail since there's not shader program at which it can set the uniform value! 如果不绑定程序,glUniform1i将失败,因为没有可用于设置统一值的着色器程序!


As a general advice, call glGetError after each OpenGL call to detect these conditions. 作为一般建议,在每个OpenGL调用之后调用glGetError来检测这些情况。 Most of those calls can be removed by preprocessor in release version. 发行版本中的预处理器可以删除其中大部分的调用。

Well, found out that the big issue was that while I was binding a texture, I wasn't actually setting it in a way that it was understood as being used. 好吧,发现最大的问题是,当我绑定纹理时,我实际上并没有以被理解为被使用的方式对其进行设置。 Setting glClientActiveTexture(GL_TEXTURE0 + texUnit); 设置glClientActiveTexture(GL_TEXTURE0 + texUnit); in combination with glActiveTexture(); 与glActiveTexture()结合使用; ended up being the final solution. 最终成为最终解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM