Sign in to follow this  
hatfarm

Binding Texture problems

Recommended Posts

hatfarm    224

So, I followed this tutorial:

 

http://open.gl/textures

 

At least, the single texture part.  So, I wanted to be able to extend it so I made everything in it's own class and I'm able to put a single texture into my program just fine, as long as I don't try to bind the texture.  If I try to bind, it just doesn't display an image.  Here's my bind code:

 

void GLTexture::bindTextureToSampler(const GLint uniLoc, const GLint texToBindTo){
   if(m_isLoaded){
        glActiveTexture(GL_TEXTURE0 + texToBindTo);
	glBindTexture(GL_TEXTURE_2D, m_tex);
        glUniform1i(uniLoc , texToBindTo); 

    }
}

 

m_tex is the texture that was generated when the texture is loaded.  uniLoc is the uniform location of the sampler in the shader.  If I don't call this function, then my image displays, so I know that the loading is correct.  However, if I call this function, and pass it valid values, I get nothing to display.  I call this function in the initialization, I tried it in the render code as well, and still have no luck.  Can anyone give me a decent idea why this would be a problem?  If you need more code, let me know.  Thanks ahead of time.

Share this post


Link to post
Share on other sites
Kaptein    2224
I don't see anything wrong with your code snippet, perhaps check for any glGetError() ?
The problem could be many things, including not binding the correct shader, and thus you would be inadvertently using fixed pipeline.

Share this post


Link to post
Share on other sites
hatfarm    224

So, I tried the glGetError and got nothing, so I'm gonna give you a bit more.  Here's my shader code:

 

// Shader sources
const char* vertexSource = " \n\
    #version 330            \n\
	in vec2 position;       \n\
	in vec3 color;          \n\
	in vec2 texcoord;       \n\
    uniform mat4 gWorld;    \n\
	out vec3 Color;         \n\
	out vec2 Texcoord;      \n\
	void main() {           \n\
		Color = color;      \n\
		Texcoord = texcoord;\n\
		gl_Position = gWorld * vec4( position, 0.0, 1.0 );  \n\
	}";

const char* fragmentSource = "  \n\
	#version 330                \n\
	in vec3 Color;              \n\
	in vec2 Texcoord;           \n\
	out vec4 outColor;          \n\
	uniform sampler2D tex;      \n\
	void main() {               \n\
		outColor = texture( tex, Texcoord ) * vec4( Color, 1.0 );   \n\
	}";

 

 

Here's the full texture code:

 

bool GLTexture::loadTexture(const std::string &tex_loc){
    glGenTextures( 1, &m_tex );

	int width, height;
	sf::Image img_data;

	if(!img_data.loadFromFile(tex_loc))
        return false;

	sf::Vector2u size = img_data.getSize();
	width = size.x;
	height = size.y;

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)img_data.getPixelsPtr());

    m_isLoaded = true;

    return true;
}

void GLTexture::setFilters(const GLint magFilter, const GLint minFilter, const GLint sWrapParam, const GLint tWrapParam) {
    if(m_isLoaded)
    {
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapParam );
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapParam );
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter );
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter );
    }
}


void GLTexture::bindTextureToSampler(const GLint uniLoc, const GLint texToBindTo){
   if(m_isLoaded){
        glActiveTexture(GL_TEXTURE0 + texToBindTo);
        if(glGetError() != GL_NO_ERROR){
            std::cout << "Active.\n";
        }
	    glBindTexture(GL_TEXTURE_2D, m_tex);
        if(glGetError() != GL_NO_ERROR){
            std::cout << "Bind.\n";
        }
        glUniform1i(uniLoc , texToBindTo); 
        if(glGetError() != GL_NO_ERROR){
            std::cout << "Uniform.\n";
        }
    }
}

void GLTexture::deleteTexture(){
    if(m_isLoaded)
        glDeleteTextures( 1, &m_tex );
}

 

I didn't include the header, but I set m_isLoaded = false;

 

Here's the code during intialization that I use:

 

glEnable(GL_TEXTURE_2D);

m_tex.loadTexture("example.png");
m_tex.setFilters();

GLint iSamplerLoc;
m_sProg.getUniformLocation("tex", iSamplerLoc);

m_tex.bindTextureToSampler(iSamplerLoc, 0);

 

Hopefully this is enough information to get some ideas.  I'm really at a loss. I'd like to be able to switch textures at some point.

Share this post


Link to post
Share on other sites
hatfarm    224

So, I went through some other tutorials and realized that I need to set an active texture when I'm loading the texture.  So, I added that and it lets me bind images.now.  Just wanted to let people know what the solution was in case they had the same issue.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this