Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: OpenGL Texture won't draw

  1. #1

    Default OpenGL Texture won't draw

    I have followed a tutorial on how to display an image in OpenGL, and I first had to make a square and then texture it. I have tried
    gl.glColor4f(0f,0f,0f,1f);
    and that works, I can see the square. But then when I try to texture it, the screen just stays white. What did I do wrong?

    Here is the screen with the
    gl.glColor4f(0f,0f,0f,1f);
    :

    lYaCq.jpg

    If I apply the texture (using the custom Texture class that the tutorial suggested), the screen is completely white.

    Here is my code:

    Square.java:

    package com.nivangerow.android.reference;
     
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    import java.nio.FloatBuffer;
    import java.nio.ShortBuffer;
     
    import javax.microedition.khronos.opengles.GL10;
     
    import android.content.Context;
     
    public class Square
    {
    	final int VERTEX_SIZE = (2+2) *4;
    	FloatBuffer vertices;
    	ShortBuffer indices;
     
    	Texture texture;
     
    	GL10 gl;
    	Context c;
     
    	public Square(GL10 gl, Context context)
    	{
    		this.gl = gl;
    		this.c = context;
     
    		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(VERTEX_SIZE * 4);
    		byteBuffer.order(ByteOrder.nativeOrder());
    		vertices = byteBuffer.asFloatBuffer();
    		vertices.put(new float[]{
    			10.0f, 10.0f, 0.0f, 0.0f, //bl	
    			160.0f, 10.0f, 1.0f, 0.0f, //br	
    			160.0f, 160.0f, 1.0f, 1.0f, //tr	
    			10.0f, 160.0f, 1.0f, 0.0f, //tl	
    		});
    		vertices.flip();
     
    		vertices.position(0);
     
    		byteBuffer = ByteBuffer.allocateDirect(VERTEX_SIZE * 4);
    		byteBuffer.order(ByteOrder.nativeOrder());
    		indices = byteBuffer.asShortBuffer();
    		indices.put(new short[]{
    			0, 1, 2, 2, 3, 0
    		});
    		indices.flip();
     
    		indices.position(0);
     
    		texture = new Texture("image/picture.png", c, gl);
    	}
     
    	public void draw()
    	{
    		gl.glEnable(GL10.GL_TEXTURE_2D);
    		texture.bind();
    		//gl.glColor4f(0f, 0f, 0f, 1f);
     
    		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
     
    		vertices.position(0);
    		gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
    		vertices.position(2);
    		gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
     
    		gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indices);
    	}
    }

    Texture.java

    package com.nivangerow.android.reference;
     
    import java.io.InputStream;
     
    import javax.microedition.khronos.opengles.GL10;
     
    import android.content.Context;
    import android.content.res.AssetManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.opengl.GLUtils;
     
    public class Texture {
     
    	String texturePath;
    	Context context;
    	GL10 gl;
     
    	int textureId;
    	int minFilter;
    	int magFilter;
    	Bitmap texture;
     
    	public Texture(String texturePath, Context context, GL10 gl)
    	{
    		this.gl = gl;
    		this.texturePath = texturePath;
    		this.context = context;
    	}
     
    	public void load()
    	{
    		try{
    			AssetManager assetManager = context.getAssets();
    			InputStream is = assetManager.open(texturePath);
    			texture = BitmapFactory.decodeStream(is);
    			gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
    			setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
    			gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    		}catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
     
    	public void reload()
    	{
    		load();
    		bind();
    		setFilters(minFilter, magFilter);
    		gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    	}
     
    	public void setFilters(int minFilter, int magFilter)
    	{
    		this.minFilter = minFilter;
    		this.magFilter = magFilter;
    		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, minFilter);
    		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, magFilter);
    	}
     
    	public void bind()
    	{
    		gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    	}
     
    	public void dispose()
    	{
    		gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    		int[] textureIds = {textureId};
    		gl.glDeleteTextures(1, textureIds, 0);
    	}
     
    }

    OpenGLRenderer.java

    package com.nivangerow.android.reference;
     
    import java.util.Random;
     
    import javax.microedition.khronos.egl.EGLConfig;
    import javax.microedition.khronos.opengles.GL10;
     
    import android.content.Context;
    import android.opengl.GLSurfaceView.Renderer;
     
    public class OpenGLRenderer implements Renderer 
    {
    	Random rand = new Random();
    	int mWidth = 0;
    	int mHeight = 0;
     
    	Context c;
     
    	Square square;
     
    	public OpenGLRenderer(Context c)
    	{
    		this.c = c;
    	}
     
    	@Override
    	public void onDrawFrame(GL10 gl) 
    	{
    		gl.glClearColor(1, 1, 1, 1);
    		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    		gl.glViewport(0, 0, mWidth, mHeight);
    		gl.glMatrixMode(GL10.GL_PROJECTION);
    		gl.glLoadIdentity();
    		gl.glOrthof(0, mWidth, 0, mHeight, 1, -1);
     
    		if(square != null)square.draw();
    	}
     
    	@Override
    	public void onSurfaceChanged(GL10 gl, int width, int height) 
    	{
    		mWidth = width;
    		mHeight = height;
    	}
     
    	@Override
    	public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    	{
    		square = new Square(gl, c);
    	}
     
    }

    Thanks in advance.


  2. #2

    Default Re: OpenGL Texture won't draw

    Im stupid, I forgot to call the load() method in the texture object.
    Last edited by nivangerow; November 26th, 2011 at 06:10 AM.

Similar Threads

  1. 3D picking in opengl
    By eshvartz in forum Java Theory & Questions
    Replies: 0
    Last Post: November 21st, 2011, 03:52 AM
  2. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  3. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM
  4. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM
  5. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM

Tags for this Thread