Weird behavior of OpenGL / glGetUniformLocation()

旧巷老猫 提交于 2019-12-11 04:48:47

问题


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

Vertex Shader:

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

Fragment Shader:

#version 420 core

uniform sampler2D p_texture;

in vec2 texturePoint;

out vec4 outColor;

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

OpenGL Code:

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

The weird thing:

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

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).

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?


回答1:


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

Try this instead:

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

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



来源:https://stackoverflow.com/questions/14591948/weird-behavior-of-opengl-glgetuniformlocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!