Here is my complete code, if you see an error even stupid one tell me ??? :-[
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
import javax.swing.*;
import java.nio.*;
import java.net.*;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import javax.imageio.ImageIO;
import java.io.IOException;
public class JoGLtest extends JPanel implements GLEventListener
{
private GLCanvas canvas;
private Animator anim;
private double eyex;
private double eyey;
private double eyez;
private double lookx;
private double looky;
private double lookz;
ByteBuffer texture;
int textures[];
int tWidth;
int tHeight;
private URL[] theurl;
private BuildingList buildList;
public JoGLtest(URL[] url)
{
super();
GLCapabilities capabilities=new GLCapabilities();
capabilities.setHardwareAccelerated(true); //We want hardware acceleration
capabilities.setDoubleBuffered(true); //And double buffering
canvas=GLDrawableFactory.getFactory().createGLCanvas(capabilities);
canvas.addGLEventListener(this);
theurl=new URL[3];
textures=new int[3];
theurl=url;
this.add(canvas);
this.setSize(800,600);
canvas.setSize(800,600);//We want the JPanel and the GLCanvas to have the same size
canvas.setVisible(true);//This is somehow necessary
eyex=-5.0;
eyey=4.0;
eyez=6.0;
/*eyex=-2.5;
eyey=1.5;
eyez=3.0;
lookx=-3.75;
looky=1.3;
lookz=-4.75;*/
lookx=-1.75;
looky=0;
lookz=-1.75;
}
public void init(GLDrawable glDrawable)
{
GL myGL=glDrawable.getGL();//Get the GL object from glDrawable
GLU myGLU=glDrawable.getGLU();
myGL.glClearColor( 0, 0, 0, 0 ); // Couleur de suppression noire
myGL.glClear(GL.GL_COLOR_BUFFER_BIT); // Supprime le buffer d'affichage boolean currentCube
myGL.glShadeModel (GL.GL_SMOOTH);
myGL.glGenTextures(3,textures);
myGL.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
loadTexture(0);
myGL.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, tWidth, tHeight,0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture);
myGL.glBindTexture(GL.GL_TEXTURE_2D, textures[1]);
loadTexture(1);
myGL.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, tWidth, tHeight,0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture);
myGL.glBindTexture(GL.GL_TEXTURE_2D, textures[2]);
loadTexture(2);
myGL.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, tWidth, tHeight,0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture);
anim = new Animator(glDrawable);
anim.start();
canvas.repaint();
}
public void update()
{
canvas.repaint();
}
public void reshape(GLDrawable glDrawable, int i, int i1, int i2, int i3)
{
GL myGL=glDrawable.getGL();
GLU glu = glDrawable.getGLU();
myGL.glViewport(0,0,i2,i3);
myGL.glMatrixMode(GL.GL_PROJECTION);
myGL.glLoadIdentity();
glu.gluPerspective(45.0,(i2*1.0)/(i3*1.0),0.5,20.0);
myGL.glMatrixMode(GL.GL_MODELVIEW);
myGL.glLoadIdentity();
}
public void display(GLDrawable glDrawable)
{
GL myGL=glDrawable.getGL();
GLU glu = glDrawable.getGLU();
GLUT glut = new GLUT();
myGL.glClearColor( 0, 0, 0, 0 ); // Couleur de suppression noire
myGL.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Supprime les buffers d'affichage et de profondeur
myGL.glShadeModel(GL.GL_SMOOTH); // rendu des surfaces lissées
myGL.glEnable(GL.GL_DEPTH_TEST); // active le Z-buffer
myGL.glEnable(GL.GL_NORMALIZE); // active la normalisation des normales declarees avec glNormal
myGL.glEnable(GL.GL_CULL_FACE); // active la differentiation des exterieurs et interieurs des surfaces
float[] lightDiffuse = {1f, 1f, 1f, 1.0f};
float[] lightAmbient = {1.0f, 1.0f, 1.0f, 0.1f};
float[] lightPosition= {2.0f, 10.0f, 2.0f, 1.0f};
//Ambient light component
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient);
//Diffuse light component
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse);
//Light position
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition);
myGL.glEnable(GL.GL_LIGHT0);
myGL.glEnable(GL.GL_LIGHTING);
myGL.glEnable(GL.GL_TEXTURE_2D);
myGL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
myGL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
myGL.glMatrixMode(GL.GL_MODELVIEW);
myGL.glLoadIdentity();
glu.gluLookAt(eyex,eyey,eyez,lookx,looky,lookz,0,1.0,0);
myGL.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
BuildingList.enceinte(myGL); //static method building walls and towers
myGL.glBindTexture(GL.GL_TEXTURE_2D, textures[1]);
//making the soil
myGL.glBegin(GL.GL_QUADS);
myGL.glNormal3f(0,1f,0);
myGL.glTexCoord2f(0,0);
myGL.glVertex3f(-20f,0,20f);
myGL.glTexCoord2f(40f,0);
myGL.glVertex3f(20f,0,20f);
myGL.glTexCoord2f(40f,40f);
myGL.glVertex3f(20f,0,-20f);
myGL.glTexCoord2f(0,40f);
myGL.glVertex3f(-20f,0,-20f);
myGL.glEnd();
myGL.glBindTexture(GL.GL_TEXTURE_2D, textures[2]);
myGL.glTranslatef(-2f,0,-2f);
//adding a medieval Oven
BuildingList.addOven(myGL,glu);
myGL.glDisable(GL.GL_TEXTURE_2D);
myGL.glFlush();
}
public void displayChanged(GLDrawable glDrawable, boolean b, boolean b1){}
public void loadTexture(int i)
{
BufferedImage buff=null;
try{buff=ImageIO.read(theurl[i]);}catch(IOException ioex){System.out.println("Oups probleme");}
// the url is an array containing urls for the image files
Raster r = buff.getRaster();
int[] img = null;
img = r.getPixels(0, 0, buff.getWidth(), buff.getHeight(), img);
texture = ByteBuffer.allocateDirect(buff.getWidth() * buff.getHeight() * 3);
for (int y = 0; y < buff.getHeight(); y++)
for (int x = 0; x < buff.getWidth(); x++) {
texture.put((byte) img[(y * buff.getWidth() + x) * 3]);
texture.put((byte) img[(y * buff.getWidth() + x) * 3 + 1]);
texture.put((byte) img[(y * buff.getWidth() + x) * 3 + 2]);
}
tWidth = buff.getWidth();
tHeight = buff.getHeight();
}