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.