Archived

This topic is now archived and is closed to further replies.

The_Fallen

ortho and alpha blending

Recommended Posts

Hi, I''m currently implementing a class for drawing 2D in OpenGL. My example drawing method looks like this:
  
struct P3DPixel
{
	unsigned char red, green, blue, alpha;
};
P3DPixel m_ImageData[512*512];
GLuint m_Texture[1];

void P3DDraw2D::Draw()
{
	for (int a=0; a<512*512; a++)
	{
		m_ImageData[a].red = 255;
		m_ImageData[a].green = 0;
		m_ImageData[a].blue = 0;
		m_ImageData[a].alpha = 150;
	}

	glDeleteTextures(1, &m_Texture[0]);
	glGenTextures(1, &m_Texture[0]);

	glBindTexture (GL_TEXTURE_2D, m_Texture[0]);
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexImage2D (GL_TEXTURE_2D, 0, 4, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_ImageData);

	glMatrixMode(GL_PROJECTION);					
	glPushMatrix();							
	glLoadIdentity();								
	glOrtho(0, 512, 512, 0, 0, 1 );	
	glMatrixMode(GL_MODELVIEW);					
	glLoadIdentity();

	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);						

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glBindTexture(GL_TEXTURE_2D, m_Texture[0]);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);	glVertex2f(0, 0);
		glTexCoord2f(0.0f, 0.0f);	glVertex2f(0, 512);
		glTexCoord2f(1.0f, 0.0f);	glVertex2f(512, 512);
		glTexCoord2f(1.0f, 1.0f);	glVertex2f(512, 0);
	glEnd();

	glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);	

	glMatrixMode( GL_PROJECTION );				
	glPopMatrix();							
	glMatrixMode( GL_MODELVIEW );			
}
  
This should draw a transparent red quad in front of my scene. But now I can''t see my scene and the rendered picture is white, nothing more... What am I doing wrong? thx, fallen

Share this post


Link to post
Share on other sites
Sorry, but you haven''t understand, what I want to do!
In this example it is a static texture, of course, because it''s only for testing! But in the final code the texture is going to be dynamic, so I have to create it as often as something changes.
But that''s not the problem... Nobody here, who can help?

Share this post


Link to post
Share on other sites
First thing.. create the texture first, then delete it at the bottom. Don''t delete it at the top, because the first time the function is called, it has a random number in it. Second, are you enabled GL_TEXTURE_2D anywhere before you call this function? You must enable 2d texture mapping for it to work properly, otherwise it will draw in whatever glColor3f is set to (normally white). This could be your problem . Make sure you enable it BEFORE you load your texture.

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
And secondly.. if you want the texture to change as needed, I would recommend NOT creating a new texture, but simply over-writing the current one. That way you don''t have to keep allocating and deleting space.

Share this post


Link to post
Share on other sites
No, it''s not that simple . You have to upload it back up to the graphics card. Check out Nehe''s tutorial on playing an AVI file, it uses this type of updating to display the movie onto a quad as a texture. It''ll show you the "correct" way to use a dynamic texture.

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
No, it''s not that simple . You have to upload it back up to the graphics card. Check out Nehe''s tutorial on playing an AVI file, it uses this type of updating to display the movie onto a quad as a texture. It''ll show you the "correct" way to use a dynamic texture.

Billy - BillyB@mrsnj.com

http://nehe.gamedev.net

Share this post


Link to post
Share on other sites
Thx, works fine now. Only one problem left.
Compare these two images:
http://www.husser.de/alpha1.jpg
http://www.husser.de/alpha2.jpg
alpha1.jpg is rendered without the quad in front of the scene and alpha2.jpg is rendered with the quad and alpha set to max (255), so it should be absolutely transparent. But it isn''t! In the second image black is not really black. It is much too bright.
Any idea, what''s still going wrong?

Share this post


Link to post
Share on other sites