Temporarily draw a textured quad in alpha

Causes sprites to also be able to fade in and out

Page 1 of 1

9 Replies - 1558 Views - Last Post: 19 January 2009 - 03:16 PM Rate Topic: -----

#1 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 789
  • View blog
  • Posts: 7,623
  • Joined: 05-May 05

Temporarily draw a textured quad in alpha

Post icon  Posted 08 January 2009 - 11:04 PM

Every single one of the little sprites already has an alpha channel on it precalulated in a program I wrote. The alpha channel is used and the sprite is drawn the way it should look normally.

The problem is, I want to sometimes say that I want to draw a sprite at a specific % of transparency during runtime. For example, the further away from the ground you are, the less opaque the shadow sprite will become (it already shrinks in size as well). I've looked all over and people keep talking about color keying and alpha mapping, but that's not what I need! I need something like this:

// Setting the transparency of EVERYTHING I draw after this point is what I need

// Select the sprite to be drawn
select_texture(texture);

// Place it
glBegin
glQuad
...
...
glEnd

// Reset transparency back to normal



I just need to know how to set the blending mode so that the entire scene I draw is as transparent as I want it to be. My game has enemies, and you fight them. The enemies are already loaded so that every green pixel will not be drawn (sometimes it has a full-blown alpha map as .BMP supports alpha maps). That's fine, but when I kill an enemy it falls over (or whatever) and then slowly fades away. Perhaps the enemy is a floating fireball and already has a flare alpha map on itself, but it will still slowly fade away when destroyed. Or it's a slime that you can see through in some parts and not in others, but it still slowly fades away when destroyed.

I'm not going to modify the individual pixels of the sprite to be effected- that's too slow to do runtime and I've already loaded the texture with it's mask by the time I want to apply an entire single transparency value to it.

Is This A Good Question/Topic? 0
  • +

Replies To: Temporarily draw a textured quad in alpha

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 05:47 AM

What about you make an extra value for when you draw each object

i dunno how your code is arranged but you should get what i mean


//pseudo code

//in your player drawing place
//Player 

Draw(int texture, bool transparent);

if transparent()
{
   // opengl transparent
}
else
{
   //opengl not transparent
}





or you could place a similar bool in your main with drawing entire sets of objects

this is where that OOP i keep harping on about would have come in handy :)

thats a very crappy solution but one that will work and then can be extended to work with your own code

this tutorial has a similar way but for drawing reflections, you may be able to adapt it as your own for transparency but it means trawling through a lot of code http://www.videotuto...b_pong/text.php

This post has been edited by stayscrisp: 11 January 2009 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 10:00 AM

Oh i think i misunderstood what you want, but it can also be used in the same way to do what you need


//pseudo code
// this instead 

int transparencyValue;

DrawObject(int textureId, int transparencyValue)
{
	// etc
	glColor(255,255,255,transparencyValue);
	glTexture2D(int textureId);
	// load model
}




so now when you draw your object


int main()
{
   // etc
   beginScene();
   DrawObject(1,1.0f); // pass in your alpha value
   // etc
   endScene();
   
}




if you object then wants to move and gradually fade out you could increase or decrease your transparencyValue


 // pseudo code

Fireball();

moveFireball()
{
	// travels along a point 
	// move to such and such point 
	// decrease alpha value incremently
}






or when you make your shadow sprite shrink just inmplement the same thing that changes its size to change the alpha value

:ph34r:

This post has been edited by stayscrisp: 11 January 2009 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 789
  • View blog
  • Posts: 7,623
  • Joined: 05-May 05

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 01:34 PM

Doesn't work, I've tried that. I already know how to implement the code and OOP makes this in particular harder to implement if I did it (and I also need speed).

The source code for my entire program is available, but here's the parts that do things:

Drawing a sprite quad
/* Draws an image */
void wsimg_draw_image(image what,s32 x,s32 y)
{
	GLfloat gx,gy;
	/* Copy to GLfloat */
	gx = (GLfloat)x;
	gy = (GLfloat)y;
	/* Is the image valid? */
	if(what != WS_INVALID_IMAGE)
	{
		/* Check the image for usage */
		if(image_use[what])
		{
			/* Select the texture */
			glBindTexture(GL_TEXTURE_2D,image_frame[what][gframe]);
			glColor3f((GLfloat)0.7,0.0,0.0);
			/* Set error color and position */
			glPushMatrix();
			glTranslatef(gx,gy,0);
			glTranslatef((GLfloat)image_w[what]/2,(GLfloat)image_h[what]/2,0);
			if(gr != 0)
				glRotatef(gr,0,0,1);
			if(gsx != 0 || gsy != 0)
				glScalef(gsx,gsy,0);
			/* Set square */
			glBegin(GL_QUADS);
			glTexCoord2f(0,0);
			glVertex3f((GLfloat)-image_w[what]/2,(GLfloat)-image_h[what]/2,0);
			glTexCoord2f(0,1);
			glVertex3f((GLfloat)-image_w[what]/2,(GLfloat)image_h[what]/2,0);
			glTexCoord2f(1,1);
			glVertex3f((GLfloat)image_w[what]/2,(GLfloat)image_h[what]/2,0);
			glTexCoord2f(1,0);
			glVertex3f((GLfloat)image_w[what]/2,(GLfloat)-image_h[what]/2,0);
			glEnd();
			glPopMatrix();
			/* Reset status */
			gr = 0;
			gsx = 0;
			gsy = 0;
			gframe = 0;
			alpha = 1;
			/* Completed */
			return;
		}
	}
	/* Invalid! Then draw a hollow rectangle where it should be */
	glColor3f(0.5,0.5,0.5);
	glPushMatrix();
	glTranslatef(gx,gy,0);
	glTranslatef(8,8,0);
	if(gr != 0)
		glRotatef(gr,0,0,1);
	if(gsx != 0 || gsy != 0)
		glScalef(gsx,gsy,0);
	glBegin(GL_LINES);
	glVertex3f(-8,-8,0);
	glVertex3f(8,-8,0);
	glVertex3f(8,-8,0);
	glVertex3f(8,8,0);
	glVertex3f(8,8,0);
	glVertex3f(-8,8,0);
	glVertex3f(-8,8,0);
	glVertex3f(-8,-8,0);
	glEnd();
	glPopMatrix();
	/* Reset status */
	gr = 0;
	gsx = 0;
	gsy = 0;
	gframe = 0;
	alpha = 1;
}



Scene entry
/* 2D begin */
void ws_2d_begin(d alpha)
{
	/* Begin 2D drawing */
	glViewport(0,0,screen_width,screen_height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,game_width,game_height,0,-1.0,1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glColor4f(1,1,1,(GLfloat)alpha);
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
}



Scene end
/* 2D end */
void ws_2d_end()
{
	/* End 2D drawing */
	glFlush();
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
}


Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 01:46 PM

Hmmm did you try something like this?


/* Draws an image */
void wsimg_draw_image(image what,s32 x,s32 y, alphaValue)
{
	// stuff
	// change this 
	//glColor3f((GLfloat)0.7,0.0,0.0);

   // to this 
   // glColor4f((GLfloat)0.7,0.0,0.0,alphaValue);

}



Was This Post Helpful? 0
  • +
  • -

#6 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 789
  • View blog
  • Posts: 7,623
  • Joined: 05-May 05

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 02:14 PM

No, and I don't need to. Here this might solve your problem (hee hee hee^^):

/* Draws an image */
void wsimg_draw_image(image what,s32 x,s32 y)
{
	GLfloat gx,gy;
	/* Copy to GLfloat */
	gx = (GLfloat)x;
	gy = (GLfloat)y;
	/* Is the image valid? */
	if(what != WS_INVALID_IMAGE)
	{
		/* Check the image for usage */
		if(image_use[what])
		{
			/* Select the texture */
			glBindTexture(GL_TEXTURE_2D,image_frame[what][gframe]);
			/* Alpha mode */
			if(alpha < 1)
			{
				glColor4f(1,1,1,alpha);
				glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
			}
			else
			{
				glColor3f((GLfloat)0.7,0,0);
			}
			/* Set error color and position */
			glPushMatrix();
			glTranslatef(gx,gy,0);
			glTranslatef((GLfloat)image_w[what]/2,(GLfloat)image_h[what]/2,0);
			if(gr != 0)
				glRotatef(gr,0,0,1);
			if(gsx != 0 || gsy != 0)
				glScalef(gsx,gsy,0);
			/* Set square */
			glBegin(GL_QUADS);
			glTexCoord2f(0,0);
			glVertex3f((GLfloat)-image_w[what]/2,(GLfloat)-image_h[what]/2,0);
			glTexCoord2f(0,1);
			glVertex3f((GLfloat)-image_w[what]/2,(GLfloat)image_h[what]/2,0);
			glTexCoord2f(1,1);
			glVertex3f((GLfloat)image_w[what]/2,(GLfloat)image_h[what]/2,0);
			glTexCoord2f(1,0);
			glVertex3f((GLfloat)image_w[what]/2,(GLfloat)-image_h[what]/2,0);
			glEnd();
			glPopMatrix();
			/* Resume */
			if(alpha < 1)
			{
				glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
			}
			/* Reset status */
			gr = 0;
			gsx = 0;
			gsy = 0;
			gframe = 0;
			alpha = 1;
			/* Completed */
			return;
		}
	}
	/* Invalid! Then draw a hollow rectangle where it should be */
	glColor3f(0.5,0.5,0.5);
	glPushMatrix();
	glTranslatef(gx,gy,0);
	glTranslatef(8,8,0);
	if(gr != 0)
		glRotatef(gr,0,0,1);
	if(gsx != 0 || gsy != 0)
		glScalef(gsx,gsy,0);
	glBegin(GL_LINES);
	glVertex3f(-8,-8,0);
	glVertex3f(8,-8,0);
	glVertex3f(8,-8,0);
	glVertex3f(8,8,0);
	glVertex3f(8,8,0);
	glVertex3f(-8,8,0);
	glVertex3f(-8,8,0);
	glVertex3f(-8,-8,0);
	glEnd();
	glPopMatrix();
	/* Reset status */
	gr = 0;
	gsx = 0;
	gsy = 0;
	gframe = 0;
	alpha = 1;
}



GL_REPLACE replaces fragment processing, it simply writes over the previous stuff, which was the darkish red color I used for error textures. GL_MODULATE combines the color and the texture in the way I want, producing the effect. This can be further used to produce color lighting effects, for example by changing the RGB values as well as alpha you can mimic the effect where the boss in Final Fantasy 6 would flash a bright white and then slowly faded away while only it's red channel was kept visible when it was destroyed.

I like how no one has ever actually given me the correct answer whenever I actually ask it in these forums, I always end up with it in moments somehow magically when I was spending lots of time trying to figure it out.

This post has been edited by WolfCoder: 11 January 2009 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 02:41 PM

View PostWolfCoder, on 11 Jan, 2009 - 01:14 PM, said:

I like how no one has ever actually given me the correct answer whenever I actually ask it in these forums


Well you seem to do things in your own way so anything anyone suggest's you are going to say is not working, What i suggested is actually how i did an effect like this and worked perfectly for my needs. Obviously your needs are different.
There is usually a lot of ways to do something so correct answers are very rare.
Was This Post Helpful? 0
  • +
  • -

#8 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 789
  • View blog
  • Posts: 7,623
  • Joined: 05-May 05

Re: Temporarily draw a textured quad in alpha

Posted 11 January 2009 - 04:00 PM

View Poststayscrisp, on 11 Jan, 2009 - 02:41 PM, said:

View PostWolfCoder, on 11 Jan, 2009 - 01:14 PM, said:

I like how no one has ever actually given me the correct answer whenever I actually ask it in these forums

...What i suggested is actually how i did an effect like this and worked perfectly for my needs. ...


How? Why?

This doesn't work properly unless you set it to GL_MODULATE. Setting an alpha color works if you're drawing lines and single-color polygons but when using texture maps, you have to also specify how it gets combined.

This post has been edited by WolfCoder: 11 January 2009 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#9 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Temporarily draw a textured quad in alpha

Posted 12 January 2009 - 03:58 AM

Oh right well i was offering design choices as it seemed that you needed help :s

but you already knew the answer, i didnt know you had to enable GL_Modulate but could you not also use GL_BLEND?
Was This Post Helpful? 0
  • +
  • -

#10 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 789
  • View blog
  • Posts: 7,623
  • Joined: 05-May 05

Re: Temporarily draw a textured quad in alpha

Posted 19 January 2009 - 03:16 PM

GL_REPLACE is what I was using, GL_BLEND is always active no matter what. GL_REPLACE/GL_MODULATE is a TextureEnv function while GL_BLEND is simply enabled.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1