📄 renderer.java
字号:
}
private void update() {
if (increaseWidth)
outlineWidth += 1; // Increase Line Width ( NEW )
if (decreaseWidth)
outlineWidth -= 1; // Decrease Line Width ( NEW )
}
public void display(GLAutoDrawable drawable) {
update();
GL gl = drawable.getGL();
// Clear Color Buffer, Depth Buffer
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
Matrix TmpMatrix = new Matrix(); // Temporary MATRIX Structure ( NEW )
Vector TmpVector = new Vector(),
TmpNormal = new Vector(); // Temporary VECTOR Structures ( NEW )
gl.glLoadIdentity(); // Reset The Matrix
if (outlineSmooth) { // Check To See If We Want Anti-Aliased Lines ( NEW )
gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST); // Use The Good Calculations ( NEW )
gl.glEnable(GL.GL_LINE_SMOOTH); // Enable Anti-Aliasing ( NEW )
} else // We Don't Want Smooth Lines ( NEW )
gl.glDisable(GL.GL_LINE_SMOOTH); // Disable Anti-Aliasing ( NEW )
gl.glTranslatef(0.0f, 0.0f, -2.0f); // Move 2 Units Away From The Screen ( NEW )
gl.glRotatef(modelAngle, 0.0f, 1.0f, 0.0f); // Rotate The Model On It's Y-Axis ( NEW )
gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, TmpMatrix.Data, 0); // Get The Generated Matrix ( NEW )
// Cel-Shading Code //
gl.glEnable(GL.GL_TEXTURE_1D); // Enable 1D Texturing ( NEW )
gl.glBindTexture(GL.GL_TEXTURE_1D, shaderTexture[0]); // Bind Our Texture ( NEW )
gl.glColor3f(1.0f, 1.0f, 1.0f); // Set The Color Of The Model ( NEW )
gl.glBegin(GL.GL_TRIANGLES); // Tell OpenGL That We're Drawing Triangles
for (int i = 0; i < polyNum; i++) // Loop Through Each Polygon ( NEW )
for (int j = 0; j < 3; j++) { // Loop Through Each Vertex ( NEW )
TmpNormal.X = polyData[i].Verts[j].Nor.X; // Fill Up The TmpNormal Structure With
TmpNormal.Y = polyData[i].Verts[j].Nor.Y; // The Current Vertices' Normal Values ( NEW )
TmpNormal.Z = polyData[i].Verts[j].Nor.Z;
TmpMatrix.rotateVector(TmpNormal, TmpVector); // Rotate This By The Matrix ( NEW )
TmpVector.normalize(); // Normalize The New Normal ( NEW )
float TmpShade = Vector.dotProduct(TmpVector, lightAngle); // Calculate The Shade Value ( NEW )
if (TmpShade < 0.0f)
TmpShade = 0.0f; // Clamp The Value to 0 If Negative ( NEW )
gl.glTexCoord1f(TmpShade); // Set The Texture Co-ordinate As The Shade Value ( NEW )
gl.glVertex3f(polyData[i].Verts[j].Pos.X,
polyData[i].Verts[j].Pos.Y,
polyData[i].Verts[j].Pos.Z); // Send The Vertex Position ( NEW )
}
gl.glEnd(); // Tell OpenGL To Finish Drawing
gl.glDisable(GL.GL_TEXTURE_1D); // Disable 1D Textures ( NEW )
// Outline Code //
if (outlineDraw) { // Check To See If We Want To Draw The Outline ( NEW )
gl.glEnable(GL.GL_BLEND); // Enable Blending ( NEW )
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);// Set The Blend Mode ( NEW )
gl.glPolygonMode(GL.GL_BACK, GL.GL_LINE); // Draw Backfacing Polygons As Wireframes ( NEW )
gl.glLineWidth(outlineWidth); // Set The Line Width ( NEW )
gl.glCullFace(GL.GL_FRONT); // Don't Draw Any Front-Facing Polygons ( NEW )
gl.glDepthFunc(GL.GL_LEQUAL); // Change The Depth Mode ( NEW )
gl.glColor3fv(outlineColor, 0); // Set The Outline Color ( NEW )
gl.glBegin(GL.GL_TRIANGLES); // Tell OpenGL What We Want To Draw
for (int i = 0; i < polyNum; i++) // Loop Through Each Polygon ( NEW )
for (int j = 0; j < 3; j++) // Loop Through Each Vertex ( NEW )
gl.glVertex3f(polyData[i].Verts[j].Pos.X,
polyData[i].Verts[j].Pos.Y,
polyData[i].Verts[j].Pos.Z); // Send The Vertex Position ( NEW )
gl.glEnd(); // Tell OpenGL We've Finished
gl.glDepthFunc(GL.GL_LESS); // Reset The Depth-Testing Mode ( NEW )
gl.glCullFace(GL.GL_BACK); // Reset The Face To Be Culled ( NEW )
gl.glPolygonMode(GL.GL_BACK, GL.GL_FILL); // Reset Back-Facing Polygon Drawing Mode ( NEW )
gl.glDisable(GL.GL_BLEND); // Disable Blending ( NEW )
}
if (modelRotate) // Check To See If Rotation Is Enabled ( NEW )
modelAngle += .2f; // Update Angle Based On The Clock
}
public void reshape(GLAutoDrawable drawable,
int xstart,
int ystart,
int width,
int height) {
GL gl = drawable.getGL();
height = (height == 0) ? 1 : height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45, (float) width / height, 1, 1000);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable drawable,
boolean modeChanged,
boolean deviceChanged) {
}
private static class Matrix {
float Data[] = new float[16];
public void rotateVector(Vector V, Vector D) { // Rotate A Vector Using The Supplied Matrix ( NEW )
D.X = (Data[0] * V.X) + (Data[4] * V.Y) + (Data[8] * V.Z); // Rotate Around The X Axis ( NEW )
D.Y = (Data[1] * V.X) + (Data[5] * V.Y) + (Data[9] * V.Z); // Rotate Around The Y Axis ( NEW )
D.Z = (Data[2] * V.X) + (Data[6] * V.Y) + (Data[10] * V.Z); // Rotate Around The Z Axis ( NEW )
}
} // A Structure To Hold An OpenGL Matrix ( NEW )
// We Use [16] Due To OpenGL's Matrix Format ( NEW )
private static class Vector {
float X, Y, Z;
// Math Functions
public static float dotProduct(Vector V1, Vector V2) { // Calculate The Angle Between The 2 Vectors ( NEW )
return V1.X * V2.X + V1.Y * V2.Y + V1.Z * V2.Z; // Return The Angle ( NEW )
}
public float magnitude() { // Calculate The Length Of The Vector ( NEW )
return (float) Math.sqrt(X * X + Y * Y + Z * Z); // Return The Length Of The Vector ( NEW )
}
public void normalize() { // Creates A Vector With A Unit Length Of 1 ( NEW )
float M = magnitude(); // Calculate The Length Of The Vector ( NEW )
if (M != 0.0f) { // Make Sure We Don't Divide By 0 ( NEW )
X /= M; // Normalize The 3 Components ( NEW )
Y /= M;
Z /= M;
}
}
} // A Structure To Hold A Single Vector ( NEW )
private static class Vertex { // A Structure To Hold A Single Vertex ( NEW )
Vector Nor = new Vector(), // Vertex Normal ( NEW )
Pos = new Vector(); // Vertex Position ( NEW )
}
private static class Polygon { // A Structure To Hold A Single Polygon ( NEW )
Vertex Verts[] = new Vertex[3]; // Array Of 3 VERTEX Structures ( NEW )
public Polygon() {
for (int i = 0; i < 3; i++)
Verts[i] = new Vertex();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -