OpenGL的怪异行为/glGetUniformLocation() [英] Weird behavior of OpenGL / glGetUniformLocation()

查看:161
本文介绍了OpenGL的怪异行为/glGetUniformLocation()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想做基础知识...从我的应用程序中给着色器提供信息.我尝试了所有方法,但没有任何效果,因为我永远无法弄清OpenGL是什么新功能和不推荐使用的功能

I just wanna do the basics... give the shaders information from my application. I tried everything and nothing worked because I can never figure out what is new and what is deprecated in OpenGL

顶点着色器:

#version 420 core

layout(location = 0) in vec2 p_rect;
layout(location = 1) in vec2 p_clipRect;

out vec2 texturePoint;

void main()
{
    gl_Position = vec4( p_rect, 0.0, 1.0 );
    texturePoint = p_clipRect;
}

片段着色器:

#version 420 core

uniform sampler2D p_texture;

in vec2 texturePoint;

out vec4 outColor;

void main()
{
    outColor = texture( p_texture, texturePoint );
}

OpenGL代码:

glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, texture );
GLint texture_id( glGetUniformLocation( programId, "p_texture" ) );
glUniform1i( texture_id, texture );

// Element
static const GLushort element[] = { 0, 1, 2, 3 };
GLuint element_id;
glGenBuffers( 1, &element_id );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, element_id );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( element ), element, GL_STATIC_DRAW );

// Vertex data
struct VertexInput
{
    GLfloat m_rect[ 8 ];
    GLfloat m_clipRect[ 8 ];
}
vertex;

// fill it up with data
GLfloat vertex_data[] =
{
    -1.0f, -1.0f,
    -1.0f,  1.0f,
    1.0f, -1.0f,
    1.0f,  1.0f,
    0.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
};

memcpy( &vertex, &vertex_data, sizeof( vertex_data ) );

// VBO
GLuint vertex_id;
glGenBuffers( 1, &vertex_id );
glBindBuffer( GL_ARRAY_BUFFER, vertex_id );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertex), &vertex, GL_STATIC_DRAW );

glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_TRUE, 0, (void*)offsetof( VertexInput, m_rect ) );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_TRUE, 0, (void*)offsetof( VertexInput, m_clipRect ) );

// render the VBO
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, element_id );
glDrawElements( GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void*)0 );

// clean it up ;-)
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );

奇怪的事情:

案例1:如果programId是实际的程序ID,则我得到的texture_id = 0,但是没有信息到达着色器...什么也没有发生...

Case 1: If programId is the actual program id, I get texture_id = 0 but no information gets to the shader... nothing happens...

情况2:如果programId不是实际的程序ID,我将得到texture_id = -1,但我的代码会完全渲染图像(WEARD).

Case 2: If programId is anything other than the actual program id, I get texture_id = -1 but my code RENDERS the image perfectly (WEARD).

问题是...我知道那仍然是错误的.我需要能够将信息提供给着色器,然后进行渲染...我真的不知道案例2的工作原理,但是事实是,我需要向着色器提供更多的信息,例如其他纹理,MVP矩阵等.我做不到怎么了?如何给我的程序ID正确的值,并在着色器中获取此信息?

The thing is that... I know it's still wrong. I need to be able to give information to the shader and then render... I really don't know how the case 2 is working, but the fact is that I need to give more information to the shaders, like other textures, MVP matrix and so son. I can't do it. What is wrong? How do I give the correct value of my program id and get this information in the shader?

推荐答案

glActiveTexture( GL_TEXTURE0 );
...
glUniform1i( texture_id, texture );

尝试以下方法:

glActiveTexture( GL_TEXTURE0 + 0 );
...
glUniform1i( texture_id, 0 );

应该为采样器制服分配所需纹理单位的索引,而不是纹理对象ID.

Sampler uniforms should be assigned the index of the desired texture unit, not the texture object ID.

这篇关于OpenGL的怪异行为/glGetUniformLocation()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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