Jump to content
  • Advertisement
Sign in to follow this  
Tjaalie

GLSL texture help!

This topic is 3612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Dear Thread reader, Finally I have been converted to the world of shaders, my new game engine is going to supported them.I try to render my gui (and other 2d stuff) with shaders. But somehow I can't get textures to work. Here is what the code looks like: Vertex shader
//Vars
uniform float g_ScreenWidth;
uniform float g_ScreenHeight;

//Entry point
void main()
{
	//Vars
	vec4 Pos = gl_Vertex;
	
	//Calculate position
	Pos.x /= g_ScreenWidth/2;
	Pos.y /= -(g_ScreenHeight/2);
	Pos.x -= 1.0;
	Pos.y += 1.0;

	//Set output
	gl_TexCoord[0]  = gl_MultiTexCoord0;
	gl_Position     = Pos; 
}


Fragment shader
//Vars
uniform sampler2D g_Texture;

//Entry point
void main()
{
	//gl_FragColor = vec4(gl_TexCoord[0].st, 0.0, 1.0);
	gl_FragColor = texture2D(g_Texture, gl_TexCoord[0].st);
}


C++ code
//Init
glEnable(GL_TEXTURE_2D);

//Render
//Reset depth buffer
    glClear(GL_DEPTH_BUFFER_BIT);

    //Start 2d shader
    m_Shader2D->setUniformFloat("g_ScreenWidth",  m_Info.width);
    m_Shader2D->setUniformFloat("g_ScreenHeight", m_Info.height);
    m_Shader2D->activate();

    //Render stuff
    for (std::list<Render2DEntry>::iterator i=m_Render2DList.begin();i!=m_Render2DList.end();++i)
    {
        //Set texture
        glActiveTexture(GL_TEXTURE1);
        (*i).tex->activate();
        m_Shader2D->setUniformInt("g_Texture", 1);

        //Draw him
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f); glVertex3f((*i).pos.x,             (*i).pos.y,             (*i).pos.z);
            glTexCoord2f(1.0f, 0.0f); glVertex3f((*i).pos.x+(*i).size.x, (*i).pos.y,             (*i).pos.z);
            glTexCoord2f(1.0f, 1.0f); glVertex3f((*i).pos.x+(*i).size.x, (*i).pos.y+(*i).size.y, (*i).pos.z);
            glTexCoord2f(0.0f, 1.0f); glVertex3f((*i).pos.x,             (*i).pos.y+(*i).size.y, (*i).pos.z);
        glEnd();
    }


I know the shader works, if I use gl_FragColor = vec4(.....); I got a nice multicolor picture. But when I use the texture nothing gets drawn:S. I tried searching the net but nothing helpful there. Do you guys also know a good tutorial site because all the C++ function calls had to be extracted from some obscure forum posts with most of the code not working. I wasn't able to find some kind of official documentation. Thanks in advance. Tjaalie,

Share this post


Link to post
Share on other sites
Advertisement
If anyone needs more code just say it, shall I post the texture creating code?

Texture creation

//Load a texture
bool Texture::load(const char* file)
{
//Vars
Image* ImgData = NULL;
GLenum Format;

//Load image
ImgData = g_ImageManager.loadResource(file);
if (!ImgData)
{
DEBUG_PRINT("Can't load image for texture!");
return false;
}

//Alpha
m_Alpha = false;
if (ImgData->depth() == 32)
{
m_Alpha = true;
}

//Copy size
m_Width = ImgData->width();
m_Height = ImgData->height();

//Generate texture id
glGenTextures(1, &m_Id);

//Bind him and set options
glBindTexture(GL_TEXTURE_2D, m_Id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

//Image format
Format = GL_RGB;
if (ImgData->depth() == 32)
{
Format = GL_RGBA;
}

//Copy image data
glTexImage2D(GL_TEXTURE_2D, 0, ImgData->depth()/8, m_Width, m_Height, 0, Format, GL_BYTE, ImgData->data());

//Free image
g_ImageManager.freeResource(ImgData);

//Error?
if (glGetError() != GL_NO_ERROR)
{
return false;
}

//Done
return true;
}



Texture activation

void Texture::activate(int slot)
{
//Vars
GLenum Slot;

//Set slot
switch (slot)
{
case 0:
Slot = GL_TEXTURE0;
break;
case 1:
Slot = GL_TEXTURE1;
break;
case 2:
Slot = GL_TEXTURE2;
break;
case 3:
Slot = GL_TEXTURE3;
break;
default:
Slot = GL_TEXTURE0;
};

//Activate him
glActiveTexture(Slot);
glBindTexture(GL_TEXTURE_2D, m_Id);
}

Share this post


Link to post
Share on other sites
I think that the texture does not show up because You set texture coordinates for the GL_TEXTURE1, while the shader uses tex coords for GL_TEXTURE0.

Share this post


Link to post
Share on other sites
Thats not working, when I change activate to 1 or use glMultiTexCoord2f(GL_TEXTURE0, ...) its still not working. All pixels returned by the sampler are black. I don't know if this is default bevour when somethings wrong, but I don't know what is wrong!!! I used glGetTexImage(...) to read from memory and I can confirm that my pixel data is there (not all 0's)

Share this post


Link to post
Share on other sites
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, ImgData->depth()/8, m_Width, m_Height, 0, Format, GL_BYTE, ImgData->data());

You are using mipmaps but you are not uploading any data except for level 0. So all other levels are simply black. Try loading the data with gluBuild2DMipmaps .
And Texture::activate can be simplified liked this:
void Texture::activate(int slot)
{
glActiveTexture(GL_TEXTURE0+slot);
glBindTexture(GL_TEXTURE_2D, m_Id);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!