Jump to content
  • Advertisement
Sign in to follow this  
Chetanhl

OpenGL [SOLVED] How to render Transparent Texture Quads in OpenGL ?

This topic is 2719 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

[SOLVED]


I am working on a 2D game which uses Texture Quads but i can't figure out how to render Trasparent Quads.
I just started with openGL few dayz back so dunno much about it yet.
Currently I am loading my texture from a RAW file using this funtion -

<BR><BR><BR>GLuint cTexture::LoadTexture( [color="#0000ff"][color="#0000ff"]const [color="#0000ff"][color="#0000ff"]char * filename, [color="#0000ff"][color="#0000ff"]int width, [color="#0000ff"][color="#0000ff"]int height )<BR><BR>{<BR><BR>GLuint tempTexture;<BR><BR>[color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]char * data;<BR><BR>FILE * file;<BR><BR>[color="#008000"][color="#008000"]//The following code will read in our RAW file<BR><BR>file = fopen( filename, [color="#a31515"][color="#a31515"]"rb" );<BR><BR>[color="#0000ff"][color="#0000ff"]if ( file == NULL ) <BR><BR>{<BR><BR>chUtility::DebugStringA([color="#a31515"][color="#a31515"]"Cannot open TextureFile : " + (string)filename + [color="#a31515"][color="#a31515"]" \n");<BR><BR>[color="#0000ff"][color="#0000ff"]return 0;<BR><BR>}<BR><BR><BR><BR>data = ([color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]char *)malloc( width * height * 3 );<BR><BR>fread( data, width * height * 3, 1, file );<BR><BR>fclose( file );<BR><BR>glGenTextures( 1, &tempTexture ); [color="#008000"][color="#008000"]//generate the texture with the loaded data<BR><BR>glBindTexture( GL_TEXTURE_2D,tempTexture ); [color="#008000"][color="#008000"]//bind the texture to its array<BR><BR>glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); [color="#008000"][color="#008000"]//set texture environment parameters<BR><BR>[color="#008000"][color="#008000"]//here we are setting what textures to use and when. The MIN filter is which quality to show<BR><BR>[color="#008000"][color="#008000"]//when the texture is near the view, and the MAG filter is which quality to show when the texture<BR><BR>[color="#008000"][color="#008000"]//is far from the view.<BR><BR>[color="#008000"][color="#008000"]//The qualities are (in order from worst to best)<BR><BR>[color="#008000"][color="#008000"]//GL_NEAREST<BR><BR>[color="#008000"][color="#008000"]//GL_LINEAR<BR><BR>[color="#008000"][color="#008000"]//GL_LINEAR_MIPMAP_NEAREST<BR><BR>[color="#008000"][color="#008000"]//GL_LINEAR_MIPMAP_LINEAR<BR><BR>[color="#008000"][color="#008000"]//And if you go and use extensions, you can use Anisotropic filtering textures which are of an<BR><BR>[color="#008000"][color="#008000"]//even better quality, but this will do for now.<BR><BR>glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );<BR><BR>glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );<BR><BR>[color="#008000"][color="#008000"]//Here we are setting the parameter to repeat the texture instead of clamping the texture<BR><BR>[color="#008000"][color="#008000"]//to the edge of our shape. <BR><BR>glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );<BR><BR>glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );<BR><BR>[color="#008000"][color="#008000"]//Generate the texture<BR><BR>glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);<BR><BR>free( data ); [color="#008000"][color="#008000"]//free the texture<BR><BR>[color="#0000ff"][color="#0000ff"]return tempTexture; [color="#008000"][color="#008000"]//return whether it was successfull<BR><BR>}<BR><BR><BR>


Is there any way in openGL to specify color key eg = RGB(0,0,0) so that it dont render any pixel with this color value ???
or
How can we load PNG files (or any other format with alpha channel) as textures in OpenGL (in cross platform way so it works on LINUX also)???

Share this post


Link to post
Share on other sites
Advertisement
Use a PNG loading library lib SDL_image or write your own. I personally use .tga format with my own loading source. What you're trying to do by
indexing a specific color (like 0,0,0 or 255,255,255) is possible to do within a shader, or you could use masking, but alpha transparency looks so much better and
you won't get any interpolation issues.

Share this post


Link to post
Share on other sites
I made it working using .tga format. While searching i landed on an awseome tutorial and worked for me too. Unfortunately lost its link, had around 10 tabs open in Internet explorer & it crashed again :|
But heres the source code attached to it incase any1 needs it - [attachment=959:OpenGL_Tut5_(2D_Alpha).zip]

Share this post


Link to post
Share on other sites

Use a PNG loading library lib SDL_image or write your own. I personally use .tga format with my own loading source. What you're trying to do by
indexing a specific color (like 0,0,0 or 255,255,255) is possible to do within a shader, or you could use masking, but alpha transparency looks so much better and
you won't get any interpolation issues.



thnx for replying i know only fixed function pipeline in OpenGL yet, also i made .tga with alpha channels working so that colorkey thing not needed anymore.

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!