1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| public class Test implements EntryPoint{
private GL2 gl;
private Surface3D surface;
private LambertianShader shader;
private float posX =0, posY=0, posZ = 10;
private float yaw, yawRate;
private float pitch, pitchRate;
private float speed;
private ArrayList<Objet3D> listeObjet= new ArrayList<Objet3D>();
private final KeyboardManager keyboardManager = new KeyboardManager();
//private boolean MouseLeftClicked = false;
/**
* This is the entry point method.
*/ public void onModuleLoad() {
// Adds a canvas to the document.
surface = new Surface3D(500, 500);
surface.setPixelSize(surface.getWidth(), surface.getHeight());
RootPanel.get("section").add(surface);
keyboardManager.manage(surface);
keyboardManager.setPreventDefault(true);
gl = surface.getGL();
if (gl == null) {
Window.alert("No WebGL context found. Exiting.");
return;
}
// Sets up the GL context.
gl.clearColor(0.7f, 0.9f, 1.0f,1.0f);
gl.clearDepth(1);
gl.viewport(0, 0, surface.getWidth(), surface.getHeight());
gl.enable(EnableCap.DEPTH_TEST);
gl.depthFunc(DepthFunction.LEQUAL);
gl.clear(ClearBufferMask.COLOR_BUFFER_BIT, ClearBufferMask.DEPTH_BUFFER_BIT);
// Creates a lambertian shader.
shader = new LambertianShader();
try {
shader.init(gl);
} catch (ShaderException e) {
Window.alert(e.getMessage());
return;
}
shader.bind();
listeObjet.add(new Cube(gl, shader, new Vector3f(0, 0, 0), 2, 2, 2, new Vector4f(.9f, .2f, .2f, 1f)));
listeObjet.add(new Cube(gl, shader, new Vector3f(0, -5, 0), 2, 2, 2, new Vector4f(.2f, .8f, .2f, 1f)));
listeObjet.add(new Cube(gl, shader, new Vector3f(0, 5, 0), 2, 2, 2, new Vector4f(0.7f, .1f, 0.7f, 1f)));
listeObjet.add(new Cube(gl, shader, new Vector3f(5, 0, 0), 2, 2, 2, new Vector4f(0.7f, .1f, 0.7f, 1f)));
listeObjet.add(new Cube(gl, shader, new Vector3f(-5, 0, 0), 2, 2, 2, new Vector4f(0.7f, .1f, 0.7f, 1f)));
shader.setDiffuseColor(.9f, .2f, .2f, 1f);
shader.setLightPosition(0, 0, 5);
PROJECTION.pushIdentity();
PROJECTION.perspective(30, 1, 1, 10000);
shader.setProjectionMatrix(PROJECTION.get());
PROJECTION.pop();
update();
FpsTimer timer = new FpsTimer(60)
{
@Override
public void update()
{
System.out.println(""+getFps());
Test.this.update();
}
};
timer.start();
}
public void update() {
handleKeys();
gl.clear(ClearBufferMask.COLOR_BUFFER_BIT, ClearBufferMask.DEPTH_BUFFER_BIT);
// System.out.println("X = "+posX+" Y = "+posY+" Z = "+posZ+" AngleX = "+pitch+" AngleY = "+yaw);
MODELVIEW.push();
MODELVIEW.rotateX((float) Math.toRadians(-pitch));
MODELVIEW.rotateY((float) Math.toRadians(-yaw));
MODELVIEW.translate(-posX, -posY, -posZ);
shader.setModelViewMatrix(MODELVIEW.get());
for(Objet3D myObjet3d:listeObjet)
{
myObjet3d.draw();
}
MODELVIEW.pop();
float elapsed = 60f;
if (speed != 0) {
posX -= (float) (Math.sin(Math.toRadians(yaw)) * speed * elapsed);
posZ -= (float) (Math.cos(Math.toRadians(yaw)) * speed * elapsed);
} |
Partager