PDA

View Full Version : wtf is wrong with this shader?



Vexator
12-24-2006, 05:14 AM
using this fragment shader results in no geometry being drawn. if i remove one of the two nested if-statements, it works.. can someone tell me what's wrong?

shader code:


varying vec2 v_Coordinates;

uniform bool u_Wireframe;

uniform bool u_UseTexture0;
uniform sampler2D u_Texture0;

uniform bool u_UseTextureRectangle0;
uniform sampler2DRect u_TextureRectangle0;

void main()
{
// set default
vec4 FinalColor = vec4( 1.0, 1.0, 1.0, 1.0 );

// diffuse map
if( !u_Wireframe )
{
if( u_UseTexture0 )
{
FinalColor = texture2D( u_Texture0, v_Coordinates );
}
else if( u_UseTextureRectangle0 )
{
FinalColor = texture2DRect( u_TextureRectangle0, v_Coordinates );
}
}

// output color
gl_FragColor = vec4( FinalColor );
}program code:


void Widget::Render( Shader *shader )
{
if( shader )
{
// reset uniforms
shader->SetUniform( "u_Wireframe", false );

shader->SetUniform( "u_UseTexture0", false );
shader->SetUniform( "u_UseTextureRectangle0", false );

// setup texture
if( m_pkTexture )
{
// activate texture
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, m_pkTexture->GetId() );

// set uniforms
if( m_pkTexture->IsNPOT() )
{
shader->SetUniform( "u_TextureRectangle0", 0 );
shader->SetUniform( "u_UseTextureRectangle0", true );
}
else
{
shader->SetUniform( "u_Texture0", 0 );
shader->SetUniform( "u_UseTexture0", true );
}
}

m_pkVertexBuffer->SetupAttributes( shader );
}

// render vertices
m_pkVertexBuffer->Render( false );

shader->SetUniform( "u_Wireframe", true );

m_pkVertexBuffer->Render( true );
}thanks.. and merry christmas

Komat
12-24-2006, 07:28 AM
The specification states that one program object can not have two different sampler types (2D and RECT in your case) pointing to the same texture image unit (0 in your case). Rendering with such program will fail with the INVALID_OPERATION error.

The only way you can have the type selection in one program object is if you use different texture image units for the u_Texture0 and u_TextureRectangle0 samplers.

Komat
12-24-2006, 07:41 AM
One more thing. The sampler2DRect sampler types are used to sample from textures bound to the GL_TEXTURE_RECTANGLE_ARB target not the GL_TEXTURE_2D.

If NPOT textures are supported using the GL_TEXTURE_2D target (OGL2.0 or ARB_texture_non_power_of_two extension), the sampler2D should be used to sample them.

Vexator
12-25-2006, 01:52 AM
ah now i see :)
jep using another texture unit helped.


The sampler2DRect sampler types are used to sample from textures bound to the GL_TEXTURE_RECTANGLE_ARB target not the GL_TEXTURE_2D.i know, my renderer automatically creates texture rectangles for the gui if the dimensions are no powers of two.

thanks :D

Komat
12-25-2006, 04:13 AM
Originally posted by Vexator:
i know, my renderer automatically creates texture rectangles for the gui if the dimensions are no powers of two.
I based my comment on following part of the program code:

glBindTexture( GL_TEXTURE_2D, m_pkTexture->GetId());

// set uniforms
if( m_pkTexture->IsNPOT() )because it uses fixed target to bind the texture.