I am having a problem getting a texture to display correctly on a sphere. I have listed the class I am using below. The problem is that the texture is displaying backwards. I have tried using
glu.gluQuadricOrientation(quadric, GLU.GLU_OUTSIDE);
just after creating the quadric but that does seem to help. Any ideas or suggestions is much appreciated.
package tests;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.GLUT;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
public class SphereTest implements GLEventListener, MouseListener, MouseMotionListener
{
private GL gl;
private GLU glu;
private GLUT glut;
private Texture earthTex;
private int prevMouseX, prevMouseY;
private boolean mouseRButtonDown = false;
private float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f;
private GLUquadric quadric;
public void init(GLAutoDrawable drawable)
{
gl = drawable.getGL();
glu = new GLU();
glut = new GLUT();
System.err.println("INIT GL IS: " + gl.getClass().getName());
System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
gl.setSwapInterval(1);
float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f };
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos, 0);
gl.glEnable(GL.GL_CULL_FACE);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really Nice Perspective Calculations
// create a textured quadric
quadric = glu.gluNewQuadric();
// set normal orientation to outside
glu.gluQuadricOrientation(quadric, GLU.GLU_OUTSIDE);
// creates texture coords
glu.gluQuadricTexture(quadric, true);
loadTextures();
drawable.addMouseListener(this);
drawable.addMouseMotionListener(this);
}
public void display(GLAutoDrawable drawable)
{
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
drawSphere();
}
public void reshape(GLAutoDrawable drawable, int i, int i0, int i1, int i2)
{
}
public void displayChanged(GLAutoDrawable drawable, boolean b, boolean b0)
{
// Not used by JOGL
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
prevMouseX = e.getX();
prevMouseY = e.getY();
if ((e.getModifiers() & e.BUTTON3_MASK) != 0)
{
mouseRButtonDown = true;
}
}
public void mouseReleased(MouseEvent e)
{
if ((e.getModifiers() & e.BUTTON3_MASK) != 0)
{
mouseRButtonDown = false;
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
Dimension size = e.getComponent().getSize();
float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width);
float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height);
prevMouseX = x;
prevMouseY = y;
view_rotx += thetaX;
view_roty += thetaY;
}
public void mouseMoved(MouseEvent e)
{
}
public static void main(String[] args)
{
Frame frame = new Frame("Sphere Test");
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(new SphereTest());
frame.add(canvas);
frame.setSize(300, 300);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable()
{
public void run()
{
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
}
private void drawSphere()
{
// enable texturing and choose the 'earth' texture
gl.glEnable(GL.GL_TEXTURE_2D);
earthTex.bind();
// set how the sphere's surface responds to the light
gl.glPushMatrix();
float[] greyColor = {0.8f, 0.8f, 1.0f};
gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, greyColor, 0);
float[] whiteColor = {10.f, 1.0f, 1.0f};
gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, whiteColor, 0);
gl.glMateriali(GL.GL_FRONT, GL.GL_SHININESS, 100);
// rotate the sphere to present 'upright'
gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glu.gluSphere(quadric, 1.0f, 32, 32);
gl.glPopMatrix();
gl.glDisable(GL.GL_TEXTURE_2D);
}
private void loadTextures()
{
earthTex = loadTexture("earth.png");
}
private Texture loadTexture(String fn)
{
String fileName = "images/" + fn;
Texture tex = null;
try
{
tex = TextureIO.newTexture( new File(fileName), false);
tex.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
tex.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
}
catch (Exception e)
{
}
return tex;
}
}