📄 renderer.java
字号:
int bytesPerPixel, // Holds Number Of Bytes Per Pixel Used In The TGA File
imageSize, // Used To Store The Image Size When Setting Aside Ram
type = GL.GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)
ReadableByteChannel file = null;
try {
file = Channels.newChannel(ResourceRetriever.getResourceAsStream(filename));
readBuffer(file, TGAcompare);
readBuffer(file, header);
for (int i = 0; i < TGAcompare.capacity(); i++) // Does The Header Match What We Want?
if (TGAcompare.get(i) != TGAheader[i]) {
throw new IOException("Invalid TGA header");
}
texture.width = header.get(1) << 8 | header.get(0); // Determine The TGA Width(highbyte*256+lowbyte)
texture.height = header.get(3) << 8 | header.get(2); // Determine The TGA Height(highbyte*256+lowbyte)
if (texture.width <= 0) {// Is The Width Less Than Or Equal To Zero
throw new IOException("Image has negative width");
}
if (texture.height <= 0) {// Is The Height Less Than Or Equal To Zero
throw new IOException("Image has negative height");
}
if (header.get(4) != 24 && header.get(4) != 32) { // Is The TGA 24 or 32 Bit?
throw new IOException("Image is not 24 or 32-bit");
}
texture.bpp = header.get(4); // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture.bpp / 8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture.width * texture.height * bytesPerPixel; // Calculate The Memory Required For The TGA Data
texture.imageData = BufferUtil.newByteBuffer(imageSize); // Reserve Memory To Hold The TGA Data
readBuffer(file, texture.imageData);
for (int i = 0; i < imageSize; i += bytesPerPixel) { // Loop Through The Image Data
// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
byte temp = texture.imageData.get(i); // Temporarily Store The Value At Image Data 'i'
texture.imageData.put(i, texture.imageData.get(i + 2)); // Set The 1st Byte To The Value Of The 3rd Byte
texture.imageData.put(i + 2, temp); // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}
// Build A Texture From The Data
gl.glGenTextures(1, texture.texID, 0); // Generate OpenGL texture IDs
gl.glBindTexture(GL.GL_TEXTURE_2D, texture.texID[0]); // Bind Our Texture
gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); // Linear Filtered
gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); // Linear Filtered
if (texture.bpp == 24) // Was The TGA 24 Bits
type = GL.GL_RGB; // If So Set The 'type' To GL_RGB
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, type, texture.width, texture.height, 0, type, GL.GL_UNSIGNED_BYTE, texture.imageData);
} finally {
if (file != null) {
try {
file.close();
} catch (IOException n) {
}
}
}
}
private static void readBuffer(ReadableByteChannel in, ByteBuffer buffer) throws IOException {
while (buffer.hasRemaining()) {
in.read(buffer);
}
buffer.flip();
}
private void buildFont(GL gl) { // Build Our Font Display List
base = gl.glGenLists(256); // Creating 256 Display Lists
for (int loop1 = 0; loop1 < 256; loop1++) { // Loop Through All 256 Lists
float cx = (float) (loop1 % 16) / 16.0f; // X Position Of Current Character
float cy = (float) (loop1 / 16) / 16.0f; // Y Position Of Current Character
gl.glNewList(base + loop1, GL.GL_COMPILE); // Start Building A List
gl.glBegin(GL.GL_QUADS); // Use A Quad For Each Character
gl.glTexCoord2f(cx, 1.0f - cy - 0.0625f); // Texture Coord (Bottom Left)
gl.glVertex2d(0, 16); // Vertex Coord (Bottom Left)
gl.glTexCoord2f(cx + 0.0625f, 1.0f - cy - 0.0625f); // Texture Coord (Bottom Right)
gl.glVertex2i(16, 16); // Vertex Coord (Bottom Right)
gl.glTexCoord2f(cx + 0.0625f, 1.0f - cy - 0.001f); // Texture Coord (Top Right)
gl.glVertex2i(16, 0); // Vertex Coord (Top Right)
gl.glTexCoord2f(cx, 1.0f - cy - 0.001f); // Texture Coord (Top Left)
gl.glVertex2i(0, 0); // Vertex Coord (Top Left)
gl.glEnd(); // Done Building Our Quad (Character)
gl.glTranslated(14, 0, 0); // Move To The Right Of The Character
gl.glEndList(); // Done Building The Display List
} // Loop Until All 256 Are Built
}
private void glPrint(GL gl, int x, int y, int set, String fmt) { // Where The Printing Happens
if (fmt == null) // If There's No Text
return;
if (set > 1) // Did User Choose An Invalid Character Set?
set = 1; // If So, Select Set 1 (Italic)
gl.glEnable(GL.GL_TEXTURE_2D); // Enable Texture Mapping
gl.glLoadIdentity(); // Reset The Modelview Matrix
gl.glTranslated(x, y, 0); // Position The Text (0,0 - Top Left)
gl.glListBase(base - 32 + (128 * set)); // Choose The Font Set (0 or 1)
gl.glScalef(1.0f, 2.0f, 1.0f); // Make The Text 2X Taller
if (stringBuffer.capacity() < fmt.length()) {
stringBuffer = BufferUtil.newByteBuffer(fmt.length());
}
stringBuffer.clear();
stringBuffer.put(fmt.getBytes());
stringBuffer.flip();
gl.glCallLists(fmt.length(), GL.GL_UNSIGNED_BYTE, stringBuffer); // Write The Text To The Screen
gl.glDisable(GL.GL_TEXTURE_2D); // Disable Texture Mapping
}
private class TextureImage { // Create A Structure
ByteBuffer imageData; // Image Data (Up To 32 Bits)
int bpp; // Image Color Depth In Bits Per Pixel.
int width; // Image Width
int height; // Image Height
int texID[] = new int[1]; // Texture ID Used To Select A Texture
public void printInfo() {
System.out.println("Byte per Pixel: " + bpp + "\n" +
"Image Width: " + width + "\n" +
"Image Height:" + height + "\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -