Teaching - 2i013

Enseignants


2i013 - Groupe 5 : Course de voiture

Guide OpenGL pour LI260

Coté panel SWING :

Code de base TrackPanel3D.java :

	public TrackPanel3D() throws LWJGLException {
		super();

		this.setPreferredSize(new Dimension(1024, 768));
		toDraw = new ArrayList<Observer3D>();


		this.addKeyListener(this);

	}

	private boolean init = false;
	private void myinit() {

		GL11.glEnable(GL11.GL_TEXTURE_2D);
		// gestion de la profondeur
		GL11.glEnable(GL11.GL_DEPTH_TEST);
	}

	private void glsettings(){
		if(!init)
			myinit();
		init = true;

		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|
				GL11.GL_DEPTH_BUFFER_BIT);

		GL11.glViewport(0, 0, getWidth(), getHeight());
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		GL11.glMatrixMode(GL11.GL_PROJECTION);

		GL11.glLoadIdentity();
		GL11.glOrtho((float) -getWidth(), (float) getWidth(), (float) -getHeight(), (float) getHeight(), -2000, 2000);


		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		GL11.glLoadIdentity();                       
		GLU.gluLookAt(-300, -300, 300,
				x, y, z,
				0, 0, 1);   
	}


	public void paintGL() {
		try {
			glsettings();
			GL11.glPushMatrix();

			for(Observer3D d: toDraw){
				d.drawGL();
			}

			GL11.glPopMatrix();
			swapBuffers();
			repaint();
		} catch (LWJGLException e) {throw new RuntimeException(e);}
	}

Coté affichage texturé

la fonction draw TrackObserver3D.java:

	public void drawGL() {

		if(textureID == null){
			BufferedImage im = track.toImage();
			width = im.getWidth();
			height = im.getHeight();
			textureID = TextureLoader.loadTexture(im);
		}

		GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
		// draw a quad textured to match the sprite
		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(0, 0); GL11.glVertex3f(0, 0, 0);
			GL11.glTexCoord2f(0, 1); GL11.glVertex3f(0, height, 0);
			GL11.glTexCoord2f(1, 1); GL11.glVertex3f(width,height, 0);
			GL11.glTexCoord2f(1, 0); GL11.glVertex3f(width,0, 0);
		}
		GL11.glEnd();

les textures TextureLoader.java: