Hi, ai am having problems with performance, from time to time (almost each 10 seconds) the CPU usage goes to 100% on the applet and then it stops responfing for 5 or 6 seconds, and then continues on the same way. Any ideas?
I am using a Centrino 1.6, 1Gb RAM, ATIX600, and I am running a GL Canvas inside an applet.
The code is quite simple, The class that does all the Rendering is:
/*
* RenderThread.java
*
* Created on 1 de noviembre de 2005, 22:32
*/
package visor;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.DataInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Interact2D;
import com.threed.jpct.Loader;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureInfo;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.KeyMapper;
import com.threed.jpct.util.KeyState;
/**
*
* @author Gold Oozaru
* @version
*/
public class RenderThread extends Thread implements MouseListener{
static boolean OGL;
World mundo;
FrameBuffer buffer;
KeyMapper mapaTeclas;
KeyState estadoTeclas;
TextureManager manejadorTexturas;
Texture texturaActual;
boolean izquierda;
boolean derecha;
boolean adelante;
boolean atras;
boolean luces;
boolean salir;
ArrayList paredes;
ArrayList techos;
ArrayList pisos;
Object3D vidrios;
Object3D otros;
Object3D inicio;
Canvas canvas;
URL docBase;
public Graphics g;
String url="";
String mapa="";
Object3D seleccionado=null;
/** Creates new RenderThread */
public RenderThread(World am, FrameBuffer ab, KeyMapper ak, Canvas ac, String nombreMapa, String aurl) {
//---------------------Hacer el renderThread
OGL=false;
mundo=am;
buffer=ab;
mapaTeclas=ak;
canvas=ac;
url=aurl;
mapa=nombreMapa;
texturas ();
//texturasList ();
pisos=new ArrayList ();
techos=new ArrayList ();
paredes=new ArrayList ();
vidrios=new Object3D (0);
otros=new Object3D (0);
inicio=new Object3D (0);
cargar ("mapa.3DS", 0.3f);
mundo.addObject (otros);
mundo.addObject (vidrios);
addObjects (pisos, mundo);
addObjects (techos, mundo);
addObjects (paredes, mundo);
vidrios.setTransparency(1);
mundo.addObject(inicio);
mundo.buildAllObjects ();
mundo.getCamera().setPosition(inicio.getCenter());
//System.out.println ("getposition ()"+mundo.getCamera().getPosition ());
mundo.removeObject(inicio);
ac.addMouseListener(this);
}
public void addObjects (ArrayList objetos, World mundo) {
for (int i=0;i<objetos.size();i++) {
Object3D temp=(Object3D) objetos.get(i);
temp.setTransparency(-1);
mundo.addObject (temp);
}
}
public void cargar (String fileName, float escala) {
try {
URL link=new URL (url+"/mapas/");
System.out.println ("url: "+link.getFile());
Object3D partes []=Loader.load3DS (link, mapa, escala);
for (int i=0;i<partes.length;i++) {
Object3D part=partes [i];
part.setCenter(SimpleVector.ORIGIN);
part.rotateX((float)-Math.PI/2);
part.rotateMesh();
part.setRotationMatrix(new Matrix());
if ((i&1)==1) part.setTransparency(0);
String p1="";
String p2="";
String p3="";
StringTokenizer separador=new StringTokenizer (part.getName(), "_");
try {
p1=separador.nextToken();
p2=separador.nextToken();
p3=separador.nextToken();
} catch (Exception E) {
E.printStackTrace();
}
if (part.getName ().startsWith ("vidrio") || part.getName ().startsWith ("vidrio") || part.getName ().startsWith ("parabrisa")) {
vidrios=Object3D.mergeObjects (vidrios, part);
} else if (p2.startsWith ("P")) {
Object3D temp=new Object3D (0);
part=Object3D.mergeObjects (part, temp);
pisos.add(part);
} else if (p2.startsWith ("T")) {
techos.add(part);
} else if (p2.startsWith ("W")) {
Object3D temp=new Object3D (0);
part=Object3D.mergeObjects (part, temp);
paredes.add(part);
System.out.println ("pared agregada: "+part.getName());
} else if (part.getName ().startsWith ("inicio")) {
inicio=Object3D.mergeObjects (inicio, part);
} else {
otros=Object3D.mergeObjects (otros, part);
}
}
} catch (Exception E) {
E.printStackTrace();
}
}
public void run () {
while (!salir) {
if (OGL){
buffer.clear ();
buffer.update ();
mundo.renderScene (buffer);
mundo.draw (buffer);
buffer.displayGLOnly ();
canvas.repaint ();
}
try {
this.sleep(40);
} catch (Exception E) {
this.stop ();
}
estadoTeclas=null;
do {
estadoTeclas=mapaTeclas.poll();
if (estadoTeclas!=KeyState.NONE) {
obtenerEventos (estadoTeclas);
}
} while (estadoTeclas!=KeyState.NONE);
hacerMovimientos ();
}
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
this.stop ();
}
public void hacerMovimientos () {
if (izquierda) {
moverIzquierda ();
}
if (derecha) {
moverDerecha ();
}
if (atras) {
Camera c=mundo.getCamera ();
c.moveCamera (Camera.CAMERA_MOVEOUT, 6f);
}
if (adelante) {
Camera c=mundo.getCamera ();
c.moveCamera (Camera.CAMERA_MOVEIN, 6f);
}
}
public void obtenerEventos (KeyState evento) {
boolean estado=evento.getState ();
if (estadoTeclas.getKeyCode ()==KeyEvent.VK_UP)
adelante=estado;
if (estadoTeclas.getKeyCode ()==KeyEvent.VK_DOWN)
atras=estado;
if (estadoTeclas.getKeyCode ()==KeyEvent.VK_LEFT)
izquierda=estado;
if (estadoTeclas.getKeyCode ()==KeyEvent.VK_RIGHT)
derecha=estado;
if (estadoTeclas.getKeyCode ()==KeyEvent.VK_ESCAPE)
salir=estado;
if (estadoTeclas.getKeyCode ()==KeyEvent.VK_SPACE)
luces=estado;
}
public String getPath () {
System.out.println (this.getClass ().getResource (""));
return (this.getClass ().getResource ("./").getFile().replace ("%20", " "));
}
public String getURLPath (String filename) {
return (this.getClass ().getResource (filename).getFile());
}
public void texturas () {
manejadorTexturas=TextureManager.getInstance ();
ArrayList materiales=texturasList ();
URL link=null;
try {
link = new URL (url+"/images/Texturas/");
} catch (Exception e) {
e.printStackTrace();
}
for (int i=0;i<materiales.size();i++) {
manejadorTexturas.addTexture (((String)materiales.get(i))+".JPG", new Texture (link, ((String)materiales.get(i))+".JPG"));
}
}
public ArrayList texturasList () {
try {
URL link=new URL (url+"/images/Texturas/props.txt");
URLConnection uc=link.openConnection();
DataInputStream dis=new DataInputStream (uc.getInputStream());
System.out.println ("TEXTURAS: ");
String texturas=dis.readLine();
StringTokenizer separadorMaterial=new StringTokenizer (texturas, "|");
ArrayList materiales=new ArrayList ();
while (separadorMaterial.hasMoreTokens()) {
String material=separadorMaterial.nextToken();
StringTokenizer separadorTextura=new StringTokenizer (material, ":");
materiales.add(separadorTextura.nextToken());
}
return materiales;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void cambiarTextura (String parte) {
}
public void moverIzquierda () {
Camera temp=mundo.getCamera ();
temp.rotateY (0.02f);
}
public void moverDerecha () {
Camera temp=mundo.getCamera ();
temp.rotateY (-0.02f);
}
public void renderAWT (Graphics g) {
buffer.clear ();
buffer.update ();
mundo.renderScene (buffer);
mundo.draw (buffer);
buffer.display(g);
try {
this.sleep(60);
} catch (Exception E) {
this.stop ();
}
}
public void enviarComentario (String mensaje, ImageIcon imagen) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent evt) {
if (seleccionado!=null)seleccionado.setAdditionalColor(Color.black);
int x=evt.getX();
int y=evt.getY();
SimpleVector ray=Interact2D.reproject2D3D(mundo.getCamera(), buffer, x, y);
int poli[]=Interact2D.pickPolygon(mundo.getVisibilityList(), ray);
Object3D temp=mundo.getObject(Interact2D.getObjectID(poli));
seleccionado=temp;
temp.setAdditionalColor(Color.RED);
}
}