Advertisement Jump to content
Sign in to follow this  

OpenGL Transparent Textured Polygons Not Working

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

Hello. I'm developing a simple app for Android using Eclipse, and I'm having a difficult time doing simple see thru textured polygons. Actually what I'm doing is a fade in effect, and it works great as long as I don't load/bind the textures. So it's smooth when its just a plain white polygon with the alpha values changing over time, and I get a beautiful fade in effect. But once I load and apply that ends up being black for a little bit, and then showing up textured alright, but completely opaque. No fading at all. Whats odd is, is that if I put the loadtexture part in my onDrawFrame prior to drawing, it works but halfass, like at 1 or 2 frames a second, which obviously is not what i should be doing cause it's like reloading the same textures over and over every frame rather than actually binding them. Am I missing something here? Here's my texture part of the code, the other parts aren't that relevant, but it must be a setting:

package com.example.bosskillers;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

import android.util.Log;
//import android.widget.Toast;

public class OpenGL_Renderer implements Renderer 
	private Polygon polygon = null;
	private Context context = null;
	private float Fade;
	private float tempFade;
	private int Frame_Count;
	private int Get_Frames_Per_Second;
	private long Milliseconds;
	public OpenGL_Renderer(Context context)
		this.context = context;
		polygon = new Polygon();
	public void onDrawFrame(GL10 gl) 
			if(Intro.Blizzard_Logo.Enabled == true)
				Intro.Blizzard_Logo.Time = (double)(System.currentTimeMillis() - Intro.Blizzard_Logo.Milliseconds) / 1000.0f;
				tempFade = (float)(Intro.Blizzard_Logo.Speed * Intro.Blizzard_Logo.Time);
				if(tempFade >= 255.0f)
					tempFade = 255.0f;
					Intro.Blizzard_Logo.Enabled = false;
			Fade = tempFade / 255.0f;
			gl.glTranslatef(0.0f, 0.0f, 0.0f);
			polygon.draw(gl, 0, 1.0f, 1.0f, 1.0f, Fade);
			gl.glTranslatef(MainActivity.Height, 0.0f, 0.0f);
			polygon.draw(gl, 1, 1.0f, 1.0f, 1.0f, Fade);

			if (System.currentTimeMillis() - Milliseconds >= 1000)
				Get_Frames_Per_Second = Frame_Count;
				Log.d("test", String.valueOf(Get_Frames_Per_Second));
				//Toast.makeText(MainActivity.Get_Application_Context(), String.valueOf(Get_Frames_Per_Second), Toast.LENGTH_SHORT).show();
				Frame_Count = 0;
				Milliseconds = System.currentTimeMillis();

	public void onSurfaceChanged(GL10 gl, int width, int height) 
		if(height == 0) height = 1;
	    gl.glDisable( GL10.GL_CULL_FACE );
	    gl.glDisable( GL10.GL_DEPTH_TEST );
		gl.glViewport(0, 0, width, height);
		//GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
		GLU.gluOrtho2D(gl, (float)0, (float)width, (float)height, (float)0);

	public void onSurfaceCreated(GL10 gl, EGLConfig config) 
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glGenTextures(2, Polygon.textures_list, 0);
		polygon.loadTexture(gl, 0, this.context,R.drawable.blizzard_logo_1);
		polygon.loadTexture(gl, 1, this.context,R.drawable.blizzard_logo_2);
		Milliseconds = System.currentTimeMillis();

package com.example.bosskillers;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLUtils;

public class Polygon 
	private FloatBuffer vertexBuffer;	// buffer holding the vertices
	private FloatBuffer textureBuffer;
	public static int texture1;
	public static int texture2;
	public static int[] textures_list = new int[2];
	private Bitmap bitmap;
	public float texture[] =
		0.0f, 0.0f,
		1.0f, 0.0f,
		0.0f, 1.0f,
		1.0f, 1.0f
	public float vertices[] = 
			0.0f, 0.0f,  0.0f,		// V1 - first vertex (x,y,z)
			MainActivity.Height, 0.0f,  0.0f,	// V2 - second vertex
			0.0f, MainActivity.Height,  0.0f,	// V3 - third vertex
			MainActivity.Height, MainActivity.Height,  0.0f	// V4 - forth vertex
	public Polygon() 
		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		// allocates the memory from the byte buffer
		vertexBuffer = byteBuffer.asFloatBuffer();
		// fill the vertexBuffer with the vertices
		// set the cursor position to the beginning of the buffer
		byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
		textureBuffer = byteBuffer.asFloatBuffer();
	public void loadTexture(GL10 gl, int index, final Context context, final int resourceId)
		final Options options = new Options();
	    options.inScaled = false;
		// loading texture
		bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
		//InputStream is = context.getResources().openRawResource(R.drawable.jake);

	    //Bitmap bitmap = null;
	    //try {
	    //    bitmap = BitmapFactory.decodeStream(is);

	    //} finally {
	    //    //Always clear and close
	    //    try {
	    //        is.close();
	    //        is = null;
	    //    } catch (IOException e) {
	    //    }
		// ...and bind it to our array
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures_list[index]);
		// create nearest filtered texture
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		// Clean up
	public void draw(GL10 gl, int index, float Red, float Green, float Blue, float Alpha) 

		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		gl.glColor4f(Red, Green, Blue, Alpha);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures_list[index]);
		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		//Disable the client state before leaving
		gl.glDisableClientState (GL10.GL_VERTEX_ARRAY);

Edited by Psychopathetica

Share this post

Link to post
Share on other sites
I figured it out. It was a timing issue because i got the initial milliseconds in the MainActivity onCreate event, which was prior to loading textures. The time it took to load the textures made my timer continue to tick so it was always maxed when it came down to drawing. That why it was always opaque. So i put the time initialization in my onSurfaceCreated event and it was a flawless and beautiful fade in effect.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!