# LWJGL Sprite Overlay Problem (halbtransparent)



## Friedhelm (9. Aug 2010)

Hallo,

Ich komme nicht weiter. Ich bin soweit, dass ich 2 Sprites über den Bildschirm bewege, doch wenn diese sich überlappen, dann wird das Raumschiff halbtransparent.

Jedoch möchte ich gerne das das Raumschiff kurzzeitig hinter dem Trümmer verschwindet.


Meine GLinit()

GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
		GL11.glClearDepth(1.0f); // Depth Buffer Setup
		GL11.glDisable(GL11.GL_DEPTH_TEST); // Enables Depth Testing				
		GL11.glDepthMask(false); 
		GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix		GL11.glLoadIdentity(); // Reset The Projection Matrix
		GL11.glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW); 		
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);



Meine GLSpriteDraw:

	GL11.glPushMatrix();		
		GL11.glLoadIdentity();        
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[0].getTextureId() );
		GL11.glTranslatef(position.x,position.y,Screen.DEFAULT_Z);                     
		GL11.glRotatef(this.rotation,0f,0f,1f);
		GL11.glColor3f(1,1,1);

		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(textureRight,textureUp); //Upper right
			GL11.glVertex2f(width, -height);			
			GL11.glTexCoord2f(textureLeft,textureUp); //Upper left			
			GL11.glVertex2f(-width, -height);        	   
			GL11.glTexCoord2f(textureLeft,textureDown); //Lower left
			GL11.glVertex2f(-width,height);				
			GL11.glTexCoord2f(textureRight,textureDown); // Lower right
			GL11.glVertex2f(width,height);	
		}
       	GL11.glEnd();        
       	GL11.glPopMatrix();



Kann jemand einen Fehler entdecken?

Ich habe irgendwas gehört, dass die PNG-Image Lademethode unter MacOS Java 1.6 buggy sein soll. könnte es vielleicht daran liegen?


----------



## Friedhelm (10. Aug 2010)

Ich habe durch Zufall das Problem selbst gelöst. Beim stöbern durch zahlreiche Foren, habe ich einfach mal ein paar Sachen probiert. Das was funktioniert hat ist das:


GLinit:

GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
		GL11.glClearDepth(1.0f); // Depth Buffer Setup
		GL11.glDisable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
		GL11.glDepthMask(false); 
		GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
		GL11.glLoadIdentity(); // Reset The Projection Matrix
		GL11.glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW); 




RenderLoop:

// clear screen
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glLoadIdentity();



GLSpriteDraw:

		GL11.glPushMatrix();		

		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glEnable(GL11.GL_ALPHA_TEST);
		GL11.glAlphaFunc(GL11.GL_GREATER, 0);

		GL11.glEnable(GL11.GL_TEXTURE_2D);

		textures[0].bind();		

		GL11.glTranslatef(position.x,position.y,position.z);                   
		GL11.glRotatef(this.rotation,0f,0f,1f);
		GL11.glColor3f(1,1,1);

		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(textureRight,textureUp); 
			GL11.glVertex2f(width, -height);			
			GL11.glTexCoord2f(textureLeft,textureUp); 	
			GL11.glVertex2f(-width, -height);   
			GL11.glTexCoord2f(textureLeft,textureDown); 
			GL11.glVertex2f(-width,height);
			GL11.glTexCoord2f(textureRight,textureDown); 
			GL11.glVertex2f(width,height);	
		}
       	GL11.glEnd(); 

       	GL11.glDisable(GL11.GL_TEXTURE_2D);
       	GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

       	GL11.glDisable(GL11.GL_BLEND);


       	GL11.glPopMatrix();


An den PNG's hat's somit nicht gelegen    (war schon verzweifelt)


----------



## jenetic (22. Mrz 2011)

Thank you so much for this solution!! 

I've been looking for a way to draw translucent bitmaps that have a transparent background in ortho-mode for a long time now!



Thank you so much for mentioning the proper function


```
GL11.glEnable(GL11.GL_ALPHA_TEST);
    GL11.glAlphaFunc(GL11.GL_GREATER, 0);
```

I finally solved my function to draw any bitmap with a different alpha value:


```
@Override
        public void drawOrthoBitmapBytes( LibGLImage glImage, int x, int y, int alpha ) // TODO alter to float!
        {
            //prepare rendering 2D
            setOrthoOn();

            //be sure to disable texturing - bytes will not be drawn otherwise
            GL11.glDisable( GL11.GL_TEXTURE_2D );

            //blending allows transparent pixels
            GL11.glEnable(  GL11.GL_BLEND );

            //get alpha as float
            float alphaF = (float)alpha / 255;

            //disable full alpha pixels - this is the solution we have been looking for a long time :)
            GL11.glEnable( GL11.GL_ALPHA_TEST );
            GL11.glAlphaFunc( GL11.GL_GREATER, 0 );

            //blend images translucent [ why can we mix GL 1.4 and GL 1.1 here and it works ?? ]
            GL11.glBlendFunc(   GL11.GL_CONSTANT_ALPHA, GL11.GL_ONE_MINUS_CONSTANT_ALPHA );
            GL14.glBlendColor( 1.0f, 1.0f, 1.0f, alphaF );

            //set and draw pixels - this is a workaround to allow negative coordinates
            GL11.glRasterPos2f( 0, 0 );
            GL11.glBitmap( 0, 0, 0, 0, x, y, glImage.bytes );  //
            GL11.glDrawPixels( glImage.width, glImage.height, getSrcPixelFormat( glImage.srcPixelFormat ), GL11.GL_UNSIGNED_BYTE, glImage.bytes );

            //disable blending
            GL11.glDisable( GL11.GL_BLEND );

            //restore previous perspective and model views
            setOrthoOff();
        }
```

Thanks again! 

jenetic.bytemare

'real coders may cast to void but never die.'


----------

