GL4Java-TextureLoader

I’m having a problem with my texture loader. I have a sphere and I bind a texture, the texture appears, but it is upside down. I don’t know what to do to remedy this problem. Please Help. I have attached a code that has texture loader and sphere.

//Java classes
import java.awt.;
import java.awt.event.
;

// GL4Java classes
import gl4java.GLContext;
import gl4java.swing.GLAnimJPanel;
import gl4java.utils.glut.;
import gl4java.utils.textures.
;

public class gl4java_3Dcode extends GLAnimJPanel{

GLUTFunc glut;
int texture[] = new int[1];
double earth_radius = 6378.2;
double camera_distance = 10000.0;

public gl4java_3Dcode() {
super(false);
}

public void init(){
glut = new GLUTFuncLightImpl(gl,glu);

if(!LoadGLTextures())
 {
   System.out.println("Failed to load Textures");
   System.exit(0);
 }

 float width = (float)getSize().width;
 float height = (float)getSize().height;

 gl.glEnable(GL_TEXTURE_2D);                  //Enable Texture Mapping
 gl.glShadeModel(GL_SMOOTH);                 //Enable Smooth Color Shading
 gl.glClearColor(0.0f,0.0f,0.0f,0.0f);      //Clears the Background to black
 gl.glClearDepth(1.0);                      //Clears the Depth Buffer
 gl.glEnable(GL_DEPTH_TEST);               // Enables Depth Testing
 gl.glDepthFunc(GL_LEQUAL);                //Type of Depth test to do
 gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  //Nice Perspective Calculations

}

public void reshape(int width, int height){
if (height ==0) height =1;
gl.glViewport(0,0,width,height); //Reset current Viewport and Perspective Transformation
gl.glMatrixMode(GL_PROJECTION); //Select the Projection Matrix
gl.glLoadIdentity(); //Reset Projection Matrix
glu.gluPerspective(60.0f,width/height,1.0f,75000.0f); //Calculate the Aspect Ratio of Window

gl.glMatrixMode(GL_MODELVIEW);  //Select the Modelview Matrix
gl.glLoadIdentity();            //Reset the Modelview Matrix

}

public void display(){

 glj.gljMakeCurrent();        //Make Sure GL is initiated properly
 solidSphere();
 //wireSphere();
 //new gl4java_gcitrajectory(gl);


 gl.glFlush();

}

//------------------------------------------------------------------------------
// Method to create the GLTextures
//------------------------------------------------------------------------------

public boolean LoadGLTextures(){
//PngTextureLoader texLoader = new PngTextureLoader(gl, glu); //Call to load a .png image file
AWTTextureLoader texLoader = new AWTTextureLoader(this,gl,glu); //Call to load a .jpg image file
texLoader.readTexture(“C:/Documents and Settings/mbutler.GEN-X/jbproject/GCI_PLOT/src/gci_plot/earth.jpg”);
if(texLoader.isOk())
{
//Create the texture
gl.glGenTextures(1,texture);
gl.glBindTexture(GL_TEXTURE_2D, texture[0]);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set Up Sphere Mapping
gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
gl.glEnable(GL_BLEND);
gl.glEnable(GL_TEXTURE_GEN_S); // Enable Sphere Mapping
gl.glEnable(GL_TEXTURE_GEN_T);
gl.glTexImage2D(GL_TEXTURE_2D,0,3,texLoader.getImageWidth(),texLoader.getImageHeight(),0,GL_RGB,GL_UNSIGNED_BYTE,texLoader.getTexture());

  return true;
}
return false;

}

//------------------------------------------------------------------------------
// void solidSphere() Method that defines solid sphere
//------------------------------------------------------------------------------

public void solidSphere(){
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear screen and buffer bit
gl.glLoadIdentity(); //Reset the view
gl.glTranslatef(0.0f,0.0f,-35000.0f);
gl.glBindTexture(GL_TEXTURE_2D, texture[0]);
glut.glutSolidSphere(earth_radius,12000,12000);
}

Actually, your whole scene is being drawn upside-down, you just didn’t realize it because an upside-down sphere still looks like a sphere. :wink:

Anyway, in your reshape method, just after your gluPerspective call, call this:

gl.glScalef(1,-1,1);

And that will fix it for you. The reason is because you are using the swing version of gl4java. If you had been using the canvas, this would have been unnecessary. The technical reason is because gl4java copies the ogl screen buffer to the swing graphics buffer, but the two buffers interpret the direction of the Y axis differently. In ogl, the +Y axis points up towards the top of your screen. In swing graphics, +Y points toward the bottom of your screen. So, an image drawn in ogl would appear upside-down when copied to swing.

-Ron