Jump to content
  • Advertisement
Sign in to follow this  

OpenGL JOGL not rendering 2D textures?

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

So... I got tired of drawing images in OpenGL with the gl.glDrawPixels() because I needed a fast way to scale and rotate them. So I tried to use 2D textures, but it doesn't seem to be working for me. Here's the complete code for a small test application, what it should do is paint a 128x128 random generated texture in the bottom corner, but it just paints a black square (or whatever i set the paint color to, actually). Any ideas why it doesn't work? Thanks in advance for any help.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Timer;

import net.java.games.jogl.GL;
import net.java.games.jogl.GLCanvas;
import net.java.games.jogl.GLCapabilities;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.GLDrawableFactory;
import net.java.games.jogl.GLEventListener;
import net.java.games.jogl.GLU;

public class OGLFrameTexTest extends Frame implements GLEventListener, ActionListener
    public static final Dimension PREFERRED_FRAME_SIZE =
        new Dimension (1024,768);
    public static final Dimension PREFERRED_FRAME_SIZE_COMPENSATED =
        new Dimension (PREFERRED_FRAME_SIZE.width+8,PREFERRED_FRAME_SIZE.height+34);
    Timer repaintTimer = new Timer(1000/25, this);
    GLDrawable gld;
	public OGLFrameTexTest()
		GLCapabilities capabilities = new GLCapabilities();
		GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(capabilities);

        add (canvas, BorderLayout.CENTER);
        //Close on default
        addWindowListener(new WindowListener()
        	public void windowDeactivated(WindowEvent e){}
        	public void windowActivated(WindowEvent e){}
        	public void windowDeiconified(WindowEvent e){}
        	public void windowIconified(WindowEvent e){}
        	public void windowOpened(WindowEvent e){}
			public void windowClosed(WindowEvent e){repaintTimer.stop();}
			public void windowClosing(WindowEvent e){dispose();}
    public Dimension getPreferredSize () 
	public static void main(String[] args)
		new OGLFrameTexTest();
	int[] tempTextures = new int[1];
	public void loadTex(GL gl)
		gl.glGenTextures(1, tempTextures);
		int width = 128;
		int height = 128;
		byte[] b = new byte[width * height * 3];
		for (int a=0;a<b.length;a++)
			//for now put random values. I can load from file later.
			b[a] = (byte)(Math.random()*256);

		gl.glBindTexture(GL.GL_TEXTURE_2D, tempTextures[0]);

		gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, width, height, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, b);
	public void actionPerformed(ActionEvent e)

	public void init(GLDrawable drawable)
		gld = drawable; //is there a better way to get the GLDrawable object?
        GL gl = drawable.getGL(); 
        // set erase color
        gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); //white 
        // set drawing color and point size
        gl.glColor3f( 0.0f, 0.0f, 0.0f ); 
        gl.glPointSize(1.0f); //a 'dot' is 1 by 1 pixels, 
        loadTex(gl); //load random texture

	public void display(GLDrawable drawable)
		gld = drawable; 
		GL gl = drawable.getGL(); 
        gl.glClear (GL.GL_COLOR_BUFFER_BIT);
        //paint texture
		gl.glBindTexture(GL.GL_TEXTURE_2D, 1);
			gl.glTexCoord2f(0f, 0f);
			gl.glVertex2f(0f, 0f);
			gl.glTexCoord2f(0f, 1.0f);
			gl.glVertex2f(0f, 128f);
			gl.glTexCoord2f(1.0f, 1.0f);
			gl.glVertex2f(128f, 128f);
			gl.glTexCoord2f(1.0f, 0f);
			gl.glVertex2f(128f, 0f);

	public void reshape(GLDrawable drawable, int i, int x, int width, int height)
		GL gl = drawable.getGL(); 
		GLU glu = drawable.getGLU(); 
		gl.glViewport( 0, 0, width, height ); 
		gl.glMatrixMode( GL.GL_PROJECTION );  
		glu.gluOrtho2D( 0.0, PREFERRED_FRAME_SIZE.getWidth(), 0.0, PREFERRED_FRAME_SIZE.getHeight());

	public void displayChanged (GLDrawable drawable, boolean modeChanged, boolean deviceChanged)

Share this post

Link to post
Share on other sites
I just took a fast look over your code, but thats what I thought of might be a problem:

in display() don't call "gl.glBindTexture(GL.GL_TEXTURE_2D, 1)" but "gl.glBindTexture(GL.GL_TEXTURE_2D, tempTextures[0])" instead, because you can't be sure that genTextures generated 1 as "name" for the texture.

Also try for debug, to use GL_REPLACE instead of GL_MODULATE for the glTexEnv, then you can at least be sure that the color of the polygon doesn't affect the texture at all.

And did you check if there is actually something "sensefull" in your array? try to write your array after you randomized it to the System.out, just to see what is inside (I am not sure how the Math.random() actually works, maybe something weired is happening there!?!?).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!