PDA

View Full Version : OpenGL 3 and switching off texturing



Lazy Foo'
07-15-2010, 06:29 PM
I managed to get rendering of textured quads working, but I ran it a bit of snag. With the old fixed function pipeline is could just call glBindTexture( GL_TEXTURE_2D, NULL ) and I could render geometry textureless. If I do that now, my fragments don't show up.

My C++ code with a commented out block:


//Load texture
Texture img;
if( !img.load_image( "testing.png" ) )
{
return 2;
}

//Bind
glBindTexture( GL_TEXTURE_2D, NULL );

//Vertex coordinates
GLfloat vCoords[] = { 0.0f, 0.0f,
100.0f, 0.0f,
100.0f, 100.0f,
0.0f, 100.0f };

GLuint vBuf = NULL;
glGenBuffers( 1, &vBuf );
glBindBuffer( GL_ARRAY_BUFFER, vBuf );
glBufferData( GL_ARRAY_BUFFER, 4 * 2 * sizeof(GLfloat), vCoords, GL_STATIC_DRAW );

GLint verts = glGetAttribLocation( LEngine::get_shader(), "lVertex" );
glEnableVertexAttribArray( verts );
glVertexAttribPointer( verts, 2, GL_FLOAT, GL_FALSE, 0, NULL );

/*
//Texture coordinates
GLfloat tCoords[] = { 0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f };

GLuint tBuf = NULL;
glGenBuffers( 1, &tBuf );
glBindBuffer( GL_ARRAY_BUFFER, tBuf );
glBufferData( GL_ARRAY_BUFFER, 4 * 2 * sizeof(GLfloat), tCoords, GL_STATIC_DRAW );

GLint texs = glGetAttribLocation( LEngine::get_shader(), "lTexCoord0" );
glEnableVertexAttribArray( texs );
glVertexAttribPointer( texs, 2, GL_FLOAT, GL_FALSE, 0, NULL );

//Bind Texture
GLint texLoc0 = glGetUniformLocation( LEngine::get_shader(), "lTexture0" );
glUniform1i( texLoc0, 0 );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, img.get_texture() );

//Set color
GLint texColor = glGetUniformLocation( LEngine::get_shader(), "lColor" );
glUniform4f( texColor, 1.f, 0.f, 1.f, 1.f );
*/

//Render
GLuint iData[] = { 0, 1, 2, 3 };
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_INT, iData );



That commented block is what I use for texturing. It works fine, but I want be able to render geometry without texturing it. Is there an easier way than to create a second shader program what doesn't handle texture data and binding it when needed?

Here's my vertex shader


//Vertex Data
uniform mat4 LProj;
uniform mat4 LMView;
in vec2 lVertex;

//Texture Data 0
in vec2 lTexCoord0;
out vec2 tCoord0;

void main()
{
//Transform texture coordinate 0
tCoord0 = lTexCoord0;

//Tranforms vertex
gl_Position = LProj * LMView * vec4(lVertex.x, lVertex.y, 0.0, 1.0);
}


Here's my fragment shader

//Color
uniform vec4 lColor = vec4( 1.0, 1.0, 1.0, 1.0 );

//Texture 0
uniform sampler2D lTexture0;
in vec2 tCoord0;

//Final Fragment
out vec4 frag;

void main()
{
vec4 texResult = texture( lTexture0, vec2(tCoord0) );

//Set fragment
frag = vec4( texResult.r * lColor.r,
texResult.g * lColor.g,
texResult.b * lColor.b,
texResult.a * lColor.a
);
}

carsten neumann
07-16-2010, 08:30 AM
Not really, if you look at the shader code for textured vs untextured it is in fact quite different. For a textured object you pass interpolated texture coords from vertex to fragment shader, while for an untextured object you pass interpolated vertex colors.
If you don't need vertex colors you could perhaps bind a small solid color texture.

YarUnderoaker
07-16-2010, 11:02 AM
Just bind texture unit to zero, and fetching texels become black.