?? renderer.java
字號(hào):
explosions[i].alpha = 0;
explosions[i].scale = 1;
}
for (int i = 10; i < 20; i++) {
explosions[i] = new Explosion();
explosions[i].alpha = 0;
explosions[i].scale = 1;
}
}
private void update() {
if (decreaseCameraZ)
cameraPosition.z -= 10;
if (increaseCameraZ)
cameraPosition.z += 10;
if (increaseCameraRotation)
cameraRotation += 10;
if (decreaseCameraRotation)
cameraRotation -= 10;
if (increaseTimeStep)
timeStep += 0.1;
if (decreaseTimeStep)
timeStep -= 0.1;
}
public void display(GLAutoDrawable drawable) {
update();
GL gl = drawable.getGL();
int i;
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
//set camera in hookmode
if (cameraAttachedToBall)
glu.gluLookAt(arrayPos[0].x + 250, arrayPos[0].y + 250, arrayPos[0].z,
arrayPos[0].x + arrayVel[0].x, arrayPos[0].y + arrayVel[0].y, arrayPos[0].z + arrayVel[0].z
, 0, 1, 0);
else
glu.gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
cameraPosition.x + dir.x, cameraPosition.y + dir.y, cameraPosition.z + dir.z,
0, 1.0, 0.0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glRotatef(cameraRotation, 0, 1, 0);
//render balls
for (i = 0; i < nrOfBalls; i++) {
switch (i) {
case 1:
gl.glColor3f(1.0f, 1.0f, 1.0f);
break;
case 2:
gl.glColor3f(1.0f, 1.0f, 0.0f);
break;
case 3:
gl.glColor3f(0.0f, 1.0f, 1.0f);
break;
case 4:
gl.glColor3f(0.0f, 1.0f, 0.0f);
break;
case 5:
gl.glColor3f(0.0f, 0.0f, 1.0f);
break;
case 6:
gl.glColor3f(0.65f, 0.2f, 0.3f);
break;
case 7:
gl.glColor3f(1.0f, 0.0f, 1.0f);
break;
case 8:
gl.glColor3f(0.0f, 0.7f, 0.4f);
break;
default:
gl.glColor3f(1.0f, 0, 0);
}
gl.glPushMatrix();
gl.glTranslated(arrayPos[i].x, arrayPos[i].y, arrayPos[i].z);
glu.gluSphere(cylinder, 20, 20, 20);
gl.glPopMatrix();
}
gl.glEnable(GL.GL_TEXTURE_2D);
//render walls(planes) with texture
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[1][1]);
gl.glColor3f(1, 1, 1);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(320, 320, 320);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(320, -320, 320);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-320, -320, 320);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-320, 320, 320);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-320, 320, -320);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-320, -320, -320);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(320, -320, -320);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(320, 320, -320);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(320, 320, -320);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(320, -320, -320);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(320, -320, 320);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(320, 320, 320);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-320, 320, 320);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-320, -320, 320);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-320, -320, -320);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-320, 320, -320);
gl.glEnd();
//render floor (plane) with colours
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[1][0]);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-320, -320, 320);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(320, -320, 320);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(320, -320, -320);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-320, -320, -320);
gl.glEnd();
//render columns(cylinders)
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0][0]); /* choose the texture to use.*/
gl.glColor3f(.5f, .5f, .5f);
gl.glPushMatrix();
gl.glRotatef(90, 1, 0, 0);
gl.glTranslatef(0, 0, -500);
glu.gluCylinder(cylinder, 60, 60, 1000, 20, 2);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(200, -300, -500);
glu.gluCylinder(cylinder, 60, 60, 1000, 20, 2);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(-200, 0, 0);
gl.glRotatef(135, 1, 0, 0);
gl.glTranslatef(0, 0, -500);
glu.gluCylinder(cylinder, 30, 30, 1000, 20, 2);
gl.glPopMatrix();
//render/blend explosions
gl.glEnable(GL.GL_BLEND);
gl.glDepthMask(false);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0][1]);
for (i = 0; i < 20; i++) {
if (explosions[i].alpha >= 0) {
gl.glPushMatrix();
explosions[i].alpha -= 0.01f;
explosions[i].scale += 0.03f;
gl.glColor4f(1, 1, 0, explosions[i].alpha);
gl.glScalef(explosions[i].scale, explosions[i].scale, explosions[i].scale);
gl.glTranslatef((float) explosions[i].position.x / explosions[i].scale,
(float) explosions[i].position.y / explosions[i].scale,
(float) explosions[i].position.z / explosions[i].scale);
gl.glCallList(dlist);
gl.glPopMatrix();
}
}
gl.glDepthMask(true);
gl.glDisable(GL.GL_BLEND);
gl.glDisable(GL.GL_TEXTURE_2D);
idle();
}
/**
* Main loop of the simulation. Moves, finds the collisions and responses of the objects in the
* current time step.
*/
private void idle() {
Tuple3d uveloc = new Tuple3d(),
normal = new Tuple3d(),
point = new Tuple3d(),
norm = new Tuple3d(),
Pos2 = new Tuple3d(),
Nc = new Tuple3d();
double rt2,
rt4,
rt[] = {0},
lamda[] = {10000},
RestTime[] = {0},
BallTime[] = {0};
int BallNr = 0,
BallColNr1[] = {0},
BallColNr2[] = {0};
if (!cameraAttachedToBall) {
cameraRotation += 0.1f;
if (cameraRotation > 360)
cameraRotation = 0;
}
RestTime[0] = this.timeStep;
lamda[0] = 1000;
//Compute velocity for next timestep using Euler equations
for (int j = 0; j < nrOfBalls; j++)
arrayVel[j].scaleAdd(RestTime[0], accel, arrayVel[j]);
//While timestep not over
while (RestTime[0] > EPSILON) {
lamda[0] = 10000; //initialize to very large value
//For all the balls find closest intersection between balls and planes/cylinders
for (int i = 0; i < nrOfBalls; i++) {
//compute new position and distance
oldPos[i] = new Tuple3d();
oldPos[i].set(arrayPos[i]);
uveloc.set(arrayVel[i]);
uveloc.normalize();
arrayPos[i].scaleAdd(RestTime[0], arrayVel[i], arrayPos[i]);
rt2 = oldPos[i].distance(arrayPos[i]);
//Test if collision occured between ball and all 5 planes
if (TestIntersionPlane(pl1, oldPos[i], uveloc, rt, norm)) {
//Find intersection time
rt4 = rt[0] * RestTime[0] / rt2;
//if smaller than the one already stored replace and in timestep
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
if (!((rt[0] <= EPSILON) && (uveloc.dot(norm) > EPSILON))) {
normal.set(norm);
point.scaleAdd(rt[0], uveloc, oldPos[i]);
lamda[0] = rt4;
BallNr = i;
}
}
}
if (TestIntersionPlane(pl2, oldPos[i], uveloc, rt, norm)) {
rt4 = rt[0] * RestTime[0] / rt2;
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
if (!((rt[0] <= EPSILON) && (uveloc.dot(norm) > EPSILON))) {
normal.set(norm);
point.scaleAdd(rt[0], uveloc, oldPos[i]);
lamda[0] = rt4;
BallNr = i;
}
}
}
if (TestIntersionPlane(pl3, oldPos[i], uveloc, rt, norm)) {
rt4 = rt[0] * RestTime[0] / rt2;
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
if (!((rt[0] <= EPSILON) && (uveloc.dot(norm) > EPSILON))) {
normal.set(norm);
point.scaleAdd(rt[0], uveloc, oldPos[i]);
lamda[0] = rt4;
BallNr = i;
}
}
}
if (TestIntersionPlane(pl4, oldPos[i], uveloc, rt, norm)) {
rt4 = rt[0] * RestTime[0] / rt2;
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
if (!((rt[0] <= EPSILON) && (uveloc.dot(norm) > EPSILON))) {
normal.set(norm);
point.scaleAdd(rt[0], uveloc, oldPos[i]);
lamda[0] = rt4;
BallNr = i;
}
}
}
if (TestIntersionPlane(pl5, oldPos[i], uveloc, rt, norm)) {
rt4 = rt[0] * RestTime[0] / rt2;
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
if (!((rt[0] <= EPSILON) && (uveloc.dot(norm) > EPSILON))) {
normal.set(norm);
point.scaleAdd(rt[0], uveloc, oldPos[i]);
lamda[0] = rt4;
BallNr = i;
}
}
}
//Now test intersection with the 3 cylinders
if (TestIntersionCylinder(cyl1, oldPos[i], uveloc, rt, norm, Nc)) {
rt4 = rt[0] * RestTime[0] / rt2;
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
if (!((rt[0] <= EPSILON) && (uveloc.dot(norm) > EPSILON))) {
normal.set(norm);
point.set(Nc);
lamda[0] = rt4;
BallNr = i;
}
}
}
if (TestIntersionCylinder(cyl2, oldPos[i], uveloc, rt, norm, Nc)) {
rt4 = rt[0] * RestTime[0] / rt2;
if (rt4 <= lamda[0]) {
if (rt4 <= RestTime[0] + EPSILON)
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -