⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 renderer.java

📁 NeHe用java与OpenGL结合教程源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package demos.nehe.lesson37;

import demos.common.ResourceRetriever;
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 java.io.InputStream;
import java.util.StringTokenizer;
import java.nio.FloatBuffer;

import com.sun.opengl.util.BufferUtil;

class Renderer implements GLEventListener {
    private boolean outlineSmooth;                           // Flag To Anti-Alias The Lines ( NEW )
    private boolean outlineDraw = true;                      // Flag To Draw The Outline ( NEW )
    private boolean modelRotate = false;                     // Flag To Rotate The Model ( NEW )

    // User Defined Variables
    private float[] outlineColor = {0.0f, 0.0f, 0.0f};   // Color Of The Lines ( NEW )
    private float outlineWidth = 3f;                     // Width Of The Lines ( NEW )
    private float modelAngle = 0f;                     // Y-Axis Angle Of The Model ( NEW )

    private Polygon[] polyData;                              // Polygon Data ( NEW )
    private Vector lightAngle = new Vector();               // The Direction Of The Light ( NEW )

    private int polyNum = 0;                     // Number Of Polygons ( NEW )
    private int[] shaderTexture = new int[1];            // Storage For One Texture ( NEW )
    private boolean increaseWidth;
    private boolean decreaseWidth;

    private GLU glu = new GLU();

    public void toggleOutlineSmooth() {
        this.outlineSmooth = !outlineSmooth;
    }

    public void toggelOutlineDraw() {
        this.outlineDraw = !outlineDraw;
    }

    public void increaseOutlineWidth(boolean increase) {
        increaseWidth = increase;
    }

    public void decreaseOutlineWidth(boolean decrease) {
        decreaseWidth = decrease;
    }

    public void toggleModelRotation() {
        this.modelRotate = !modelRotate;
    }

    private void readMesh() throws IOException {
        InputStream in = ResourceRetriever.getResourceAsStream("demos/data/models/model.txt");

        polyNum = byteToInt(readNextFourBytes(in));
        polyData = new Polygon[polyNum];

        for (int i = 0; i < polyData.length; i++) {
            polyData[i] = new Polygon();
            for (int j = 0; j < 3; j++) {
                polyData[i].Verts[j].Nor.X = byteToFloat(readNextFourBytes(in));
                polyData[i].Verts[j].Nor.Y = byteToFloat(readNextFourBytes(in));
                polyData[i].Verts[j].Nor.Z = byteToFloat(readNextFourBytes(in));

                polyData[i].Verts[j].Pos.X = byteToFloat(readNextFourBytes(in));
                polyData[i].Verts[j].Pos.Y = byteToFloat(readNextFourBytes(in));
                polyData[i].Verts[j].Pos.Z = byteToFloat(readNextFourBytes(in));
            }
        }
    }

    private static byte[] readNextFourBytes(InputStream in) throws IOException {
        byte[] bytes = new byte[4];

        for (int i = 0; i < 4; i++)
            bytes[i] = (byte) in.read();
        return bytes;
    }

    private static int byteToInt(byte[] array) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int b = array[i];
            b &= 0xff;
            value |= (b << (i * 8));
        }
        return value;
    }

    private static float byteToFloat(byte[] array) {
        int value = 0;
        for (int i = 3; i >= 0; i--) {
            int b = array[i];
            b &= 0xff;
            value |= (b << (i * 8));
        }
        return Float.intBitsToFloat(value);
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        FloatBuffer shaderData = BufferUtil.newFloatBuffer(96);                        // Storate For The 96 Shader Values ( NEW )

        // Start Of User Initialization
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Realy Nice perspective calculations
        gl.glClearColor(0.7f, 0.7f, 0.7f, 0.0f);                    // Light Grey Background
        gl.glClearDepth(1.0f);                                      // Depth Buffer Setup

        gl.glEnable(GL.GL_DEPTH_TEST);                              // Enable Depth Testing
        gl.glDepthFunc(GL.GL_LESS);                                 // The Type Of Depth Test To Do

        gl.glShadeModel(GL.GL_SMOOTH);                              // Enables Smooth Color Shading ( NEW )
        gl.glDisable(GL.GL_LINE_SMOOTH);                            // Initially Disable Line Smoothing ( NEW )

        gl.glEnable(GL.GL_CULL_FACE);                               // Enable OpenGL Face Culling ( NEW )

        gl.glDisable(GL.GL_LIGHTING);                               // Disable OpenGL Lighting ( NEW )

        StringBuffer readShaderData = new StringBuffer();

        try {
            InputStream inputStream = ResourceRetriever.getResourceAsStream("demos/data/models/shader.txt");
            int info;
            while ((info = inputStream.read()) != -1)
                readShaderData.append((char) info);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        StringTokenizer tokenizer = new StringTokenizer(readShaderData.toString());

        while (tokenizer.hasMoreTokens()) {                                      // Loop Though The 32 Greyscale Values ( NEW )
            float value = Float.parseFloat(tokenizer.nextToken());
            shaderData.put(value);
            shaderData.put(value);
            shaderData.put(value);
        }
        shaderData.flip();

        gl.glGenTextures(1, shaderTexture, 0);                           // Get A Free Texture ID ( NEW )
        gl.glBindTexture(GL.GL_TEXTURE_1D, shaderTexture[0]);         // Bind This Texture. From Now On It Will Be 1D ( NEW )

        // For Crying Out Loud Don't Let OpenGL Use Bi/Trilinear Filtering! ( NEW )
        gl.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);

        gl.glTexImage1D(GL.GL_TEXTURE_1D, 0, GL.GL_RGB, 32, 0, GL.GL_RGB, GL.GL_FLOAT, shaderData); // Upload ( NEW )

        lightAngle.X = 0.0f;                                          // Set The X Direction ( NEW )
        lightAngle.Y = 0.0f;                                          // Set The Y Direction ( NEW )
        lightAngle.Z = 1.0f;                                          // Set The Z Direction ( NEW )
        lightAngle.normalize();

        try {
            readMesh();                                                  // Return The Value Of ReadMesh ( NEW )
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -