📄 renderer.java
字号:
package demos.nehe.lesson44;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import java.io.IOException;
import demos.common.TextureReader;
class Renderer implements GLEventListener {
private boolean infoOn = false;
private double gCurrentTime;
private double gStartTime;
private float gFPS;
private int gFrames;
private Camera gCamera;
private Font gFont;
private boolean pitchCameraUp = false;
private boolean pitchCameraDown = false;
private boolean yawCameraLeft = false;
private boolean yawCameraRight = false;
private GLU glu = new GLU();
public void setInfoDisplayed(boolean infoOn) {
this.infoOn = infoOn;
}
public void moveCameraBackward() {
gCamera.m_ForwardVelocity = -.01f;// Start moving the camera backwards 0.01 units every frame
}
public void moveCameraForward() {
gCamera.m_ForwardVelocity = .01f; // Start moving the camera forward 0.01 units every frame
}
public void stopCamera() {
gCamera.m_ForwardVelocity = 0.0f; // Stop the camera from moving.
}
public void pitchCameraDown(boolean pitchCameraDown) {
this.pitchCameraDown = pitchCameraDown;
}
public void pitchCameraUp(boolean pitchCameraUp) {
this.pitchCameraUp = pitchCameraUp;
}
public void yawCameraLeft(boolean yawCameraLeft) {
this.yawCameraLeft = yawCameraLeft;
}
public void yawCameraRight(boolean yawCameraRight) {
this.yawCameraRight = yawCameraRight;
}
private void loadTexture(String fileName, int[] texid, GLAutoDrawable drawable) throws IOException { // Creates Texture From A Bitmap File
TextureReader.Texture texture = TextureReader.readTexture(fileName);
GL gl = drawable.getGL();
gl.glGenTextures(1, texid, 0); // Create The Texture
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 4); // Pixel Storage Mode (Word Alignment / 4 Bytes)
// Typical Texture Generation Using Data From The Bitmap
gl.glBindTexture(GL.GL_TEXTURE_2D, texid[0]); // Bind To The Texture ID
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); // Linear Min Filter
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); // Linear Mag Filter
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, texture.getWidth(), texture.getHeight(),
0, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, texture.getPixels());
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
int[] tex = {0};
gCamera = new Camera();
gFont = new Font();
gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);// Really Nice Perspective Calculations
try {
loadTexture("demos/data/images/font.bmp", tex, drawable); // Load the font texture
} catch (IOException e) {
throw new RuntimeException(e);
}
if (tex[0] != 0) { // Make sure it was loaded
gFont.setFontTexture(tex[0]); // Set the font texture
gFont.setWindowSize(1024, 768); // The font class needs to know the window size
gFont.buildFont(gl, 1.0f); // Build the font
}
gCamera.m_MaxHeadingRate = 1.0f; // Set our Maximum rates for the camera
gCamera.m_HeadingDegrees = 0.0f; // Set our Maximum rates for the camera
gCamera.m_MaxPitchRate = 1.0f; // Set our Maximum rates for the camera
// Try and load the HardGlow texture tell the user if we can't find it then quit
try {
loadTexture("demos/data/images/hardglow2.bmp", gCamera.m_GlowTexture, drawable);
} catch (IOException e) {
throw new RuntimeException(e);
}
// Try and load the BigGlow texture tell the user if we can't find it then quit
try {
loadTexture("demos/data/images/bigglow3.bmp", gCamera.m_BigGlowTexture, drawable);
} catch (IOException e) {
throw new RuntimeException(e);
}
// Try and load the Halo texture tell the user if we can't find it then quit
try {
loadTexture("demos/data/images/halo3.bmp", gCamera.m_HaloTexture, drawable);
} catch (IOException e) {
throw new RuntimeException(e);
}
// Try and load the Streaks texture tell the user if we can't find it then quit
try {
loadTexture("demos/data/images/streaks4.bmp", gCamera.m_StreakTexture, drawable);
} catch (IOException e) {
throw new RuntimeException(e);
}
gStartTime = System.currentTimeMillis();
}
private void update() {
if (pitchCameraUp) // Is the W key down?
gCamera.changePitch(-0.2f); // Pitch the camera up 0.2 degrees
if (pitchCameraDown) // Is the S key down?
gCamera.changePitch(0.2f); // Pitch the camera down 0.2 degrees
if (yawCameraLeft) // Is the D key down?
gCamera.changeHeading(0.2f); // Yaw the camera to the left
if (yawCameraRight) // Is the A key down?
gCamera.changeHeading(-0.2f); // Yaw the camera to the right
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
gl.glLoadIdentity(); // Reset The Current Modelview Matrix
// We want our light source to be 50 units if front
// of the camera all the time to make it look like
// it is infinately far away from the camera. We only
// do this to the z coordinate because we want to see
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -