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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
public class Test implements EntryPoint{
/**
* The message displayed to the user when the server cannot be reached or
* returns an error.
*/
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
/**
* Create a remote service proxy to talk to the server-side Greeting service.
*/
private final GreetingServiceAsync greetingService = GWT
.create(GreetingService.class);
private StaticMesh mesh;
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 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(500, 500);
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);
gl.enableVertexAttribArray(0);
// Creates a lambertian shader.
shader = new LambertianShader();
try {
shader.init(gl);
} catch (ShaderException e) {
Window.alert(e.getMessage());
return;
}
shader.bind();
mesh = new StaticMesh(gl, PrimitiveFactory.makeBox());
mesh.setPositionIndex(shader.getAttributePosition());
mesh.setNormalIndex(shader.getAttributeNormal());
mesh.setTexCoordIndex(-1);
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();
FpsTimer timer = new FpsTimer(60)
{
@Override
public void update()
{
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);
mesh.draw();
drawTriangle(new Vector3f(0, 5, 0), new Vector3f(-5, -5, 0), new Vector3f(5, -5, 0));
shader.setModelViewMatrix(MODELVIEW.get());
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);
}
yaw += yawRate * elapsed;
pitch += pitchRate * elapsed;
}
private void handleKeys() {
if (keyboardManager.isButtonDown(KeyCodes.KEY_PAGEUP)) {
pitchRate = 0.1f;
} else if (keyboardManager.isButtonDown(KeyCodes.KEY_PAGEDOWN)) {
pitchRate = -0.1f;
} else {
pitchRate = 0;
}
if (keyboardManager.isButtonDown(KeyCodes.KEY_LEFT)
|| keyboardManager.isButtonDown((int) 'Q')) {
yawRate = 0.05f;
} else if (keyboardManager.isButtonDown(KeyCodes.KEY_RIGHT)
|| keyboardManager.isButtonDown((int) 'D')) {
yawRate = -0.05f;
} else {
yawRate = 0;
}
if (keyboardManager.isButtonDown(KeyCodes.KEY_UP)
|| keyboardManager.isButtonDown((int) 'Z')) {
speed = 0.003f;
} else if (keyboardManager.isButtonDown(KeyCodes.KEY_DOWN)
|| keyboardManager.isButtonDown((int) 'S')) {
speed = -0.003f;
} else {
speed = 0;
}
}
private void drawTriangle(Vector3f p1,Vector3f p2,Vector3f p3)
{
WebGLBuffer triangleVetexPositionBuffer = gl.createBuffer();
gl.bindBuffer(BufferTarget.ARRAY_BUFFER, triangleVetexPositionBuffer);
float[] vertices = {p1.x,p1.y,p1.z,p2.x,p2.y,p2.z,p3.x,p3.y,p3.z};
gl.bufferData(BufferTarget.ARRAY_BUFFER, Float32Array.create(vertices), BufferUsage.STATIC_DRAW);
gl.vertexAttribPointer(0, 3, DataType.FLOAT, false, 0, 0);
gl.drawArrays(BeginMode.TRIANGLES, 0, 3);
}
} |
Partager