Jump to content
  • Advertisement
Sign in to follow this  
Psychopathetica

OpenGL Transparent Textured Polygons Not Working

This topic is 2037 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 texture...it 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:

 

OpenGL_Renderer.java

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();
	}
	
	@Override
	public void onDrawFrame(GL10 gl) 
	{
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
			gl.glPushMatrix();
			gl.glLoadIdentity();
			
			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);
			
			gl.glPopMatrix();

			Frame_Count++;
			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();
			}
	}

	@Override
	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.glLoadIdentity();
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		//GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
		GLU.gluOrtho2D(gl, (float)0, (float)width, (float)height, (float)0);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) 
	{
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		
		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();
	}
}

Polygon.java

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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
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);
		byteBuffer.order(ByteOrder.nativeOrder());
		
		// allocates the memory from the byte buffer
		vertexBuffer = byteBuffer.asFloatBuffer();
		
		// fill the vertexBuffer with the vertices
		vertexBuffer.put(vertices);
		
		// set the cursor position to the beginning of the buffer
		vertexBuffer.position(0);
		
		
		byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuffer.asFloatBuffer();
		textureBuffer.put(texture);
		textureBuffer.position(0);
	}
	
	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
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
		
		gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// Clean up
		bitmap.recycle();
	}
	
	public void draw(GL10 gl, int index, float Red, float Green, float Blue, float Alpha) 
	{

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		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);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}
}

Edited by Psychopathetica

Share this post


Link to post
Share on other sites
Advertisement
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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!