[solved] adding Texture in jogl 2 troubles

Hello world!
I am trying to add texture to the sphere, but i got an exception: Exception in thread “AWT-EventQueue-0” javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glTexParameteri( 0xDE1, 0x8191, 0x1): GL_INVALID_ENUM ( 1280 0x500),

Here is the code:


import java.awt.EventQueue;

import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import javax.swing.JFrame;

import com.sun.opengl.util.FPSAnimator;
import com.sun.opengl.util.texture.Texture;

@SuppressWarnings("serial")
public class MyGLCanvas extends GLCanvas implements GLEventListener {

	private static MyGLCanvas canvas = new MyGLCanvas(300, 300, new GLCapabilities());
	private static FPSAnimator animator;
	private GLU glu;
	private Texture earthTexture;

	public MyGLCanvas(int width, int height, GLCapabilities cap) {
		super(cap);
		setSize(width, height);

		cap.setRedBits(8);
		cap.setBlueBits(8);
		cap.setGreenBits(8);
		cap.setAlphaBits(8);
		addGLEventListener(this);
	}

	@Override
	public void display(GLAutoDrawable drawable) {
		// TODO Auto-generated method stub
		GL gl = drawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		setCamera(gl, glu, 30);

		
		// Prepare light parameters.
		float SHINE_ALL_DIRECTIONS = 1;
		float[] lightPos = { -30, 0, 0, SHINE_ALL_DIRECTIONS };
		float[] lightColorAmbient = { 0.2f, 0.2f, 0.2f, 1f };
		float[] lightColorSpecular = { 0.8f, 0.8f, 0.8f, 1f };
		// Set light parameters.
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, lightPos, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, lightColorAmbient, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, lightColorSpecular, 0);
		// Enable lighting in GL.
		gl.glEnable(GL.GL_LIGHT1);
		gl.glEnable(GL.GL_LIGHTING);
		// Set material properties.
		float[] rgba = { 1f, 1f, 1f };
		gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, rgba, 0);
		gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, rgba, 0);
		gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 0.5f);

		earthTexture.enable();
		earthTexture.bind();
//		gl.glBegin(GL.GL_QUADS);
		GLUquadric earth = glu.gluNewQuadric();

		glu.gluQuadricTexture(earth, true);
		glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
		glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
		glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);
		final float radius = 6.378f;
		final int slices = 16;
		final int stacks = 16;
		glu.gluSphere(earth, radius, slices, stacks);
		glu.gluDeleteQuadric(earth);
		
//		gl.glEnd();
		
	}

	private void setCamera(GL gl, GLU glu2, int distance) {
		// TODO Auto-generated method stub
		// Change to projection matrix.
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity(); 
		// Perspective.
		float widthHeightRatio = (float) getWidth() / (float) getHeight();
		glu.gluPerspective(45, widthHeightRatio, 1, 1000);
		
		glu.gluLookAt(0, 0, distance, 0, 0, 0, 0, 1, 0);
		// Change back to model view matrix.
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	

	@Override
	public void init(GLAutoDrawable drawable) {
		// TODO Auto-generated method stub
		GL gl = drawable.getGL();
		
		drawable.setGL(new DebugGL(gl));
		// Global settings.
		// Enable z- (depth) buffer for hidden surface removal.
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);
		// Enable smooth shading.
		gl.glShadeModel(GL.GL_SMOOTH);
		// We want a nice perspective.
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		// Define "clear" color.
		gl.glClearColor(0f, 0f, 0f, 1f);
		glu = new GLU();
		// add Texture

		earthTexture = LoadTexture.loadTexture("earthmap1k.jpg");
		
		// Start animator (which should be a field).
		animator = new FPSAnimator(this, 60);
		animator.start();
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width,
			int height) {
		// TODO Auto-generated method stub
		GL gl = drawable.getGL();
		gl.glViewport(0, 0, width, height);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame("Universe");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(canvas.getPreferredSize());
				frame.getContentPane().add(canvas);
				frame.setVisible(true);

			}
		});

		canvas.requestFocus();
	}

	@Override
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
		// TODO Auto-generated method stub
		
	}

}

Here is LoadTexture:



import java.io.File;
import java.io.IOException;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;

public class LoadTexture {
    public static Texture loadTexture (String name){
        Texture texture = null;
            try {
            	texture = TextureIO.newTexture(new File(name), true);// bool - mipmap.
            	
            	
            } catch (IOException e) {
                System.out.println("fail openning file... " + name);
                System.out.println(e);
            }
            return texture;
    }
}

Any ideas how to fix that? Thanks.

Hi!

If you want some help, please post the full stack trace and tell us more about your image.

Hi! Ok.

Here is the stack trace:


Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the 
following error codes after a call to glTexParameteri(<int> 0xDE1, <int> 0x8191, <int> 0x1): GL_INVALID_ENUM ( 1280 0x500), 
	at javax.media.opengl.DebugGL2.checkGLGetError(DebugGL2.java:32455)
	at javax.media.opengl.DebugGL2.glTexParameteri(DebugGL2.java:9843)
	at com.jogamp.opengl.util.texture.Texture.updateImage(Texture.java:642)
	at com.jogamp.opengl.util.texture.Texture.updateImage(Texture.java:421)
	at com.jogamp.opengl.util.texture.Texture.<init>(Texture.java:183)
	at com.jogamp.opengl.util.texture.TextureIO.newTexture(TextureIO.java:406)
	at com.jogamp.opengl.util.texture.TextureIO.newTexture(TextureIO.java:427)
	at LoadTexture.loadTexture(LoadTexture.java:14)
	at MyGLCanvas.init(MyGLCanvas.java:167)
	at com.jogamp.opengl.impl.GLDrawableHelper.init(GLDrawableHelper.java:153)
	at com.jogamp.opengl.impl.GLDrawableHelper.init(GLDrawableHelper.java:167)
	at javax.media.opengl.awt.GLCanvas$InitAction.run(GLCanvas.java:776)
	at com.jogamp.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:348)
	at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:711)
	at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:361)
	at javax.media.opengl.awt.GLCanvas.paint(GLCanvas.java:464)
	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
	at sun.awt.RepaintArea.paint(RepaintArea.java:224)
	at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
	at java.awt.Component.dispatchEventImpl(Component.java:4706)
	at java.awt.Component.dispatchEvent(Component.java:4460)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


The image is a .jpg with dimensions 1000500… [You can see her at http://planetpixelemporium.com/images/mappreviews/earthmapthumb.jpg]

First of all make sure you use a power of 2 texture size (for example 1024*512) :slight_smile:

Mike

Hi Mike! Thanks for answer. I set size of my picture to 1024x512, but exception is the same…

Humm… I can’t get to the jpg, I get an “Forbidden You don’t have permission to access /images/mappreviews/earthmapthumb.jpg on this server.”

Maybe upload it somewhere else?

Also, did you test the function with something else than a jpg (like a png or bmp) to see if it is only the picture?

Mike

Sure! Try this earthmap1k_1024x512.jpg
from rapidshare: earthmap1k_1024x512.jpg
Yes, i did. i try to use *.png, *.bmp but the result is the same…

That link wants to install software on my computer from some russian site :stuck_out_tongue:

Tried it with a png or bmp?

i try to use *.png, *.bmp but the result is the same…

Please rather use a recent build of JOGL 2.0 to reproduce this bug, your version seems quite old.

I download latest autobild from master folder… and all works fine!:slight_smile:

Thanks man!))

You’re welcome ;D JOGL is alive and we do our best to help developers to use it.