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

📄 renderer.java

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

import com.sun.opengl.util.BufferUtil;

import javax.media.opengl.*;
import javax.media.opengl.glu.GLUquadric;
import javax.media.opengl.glu.GLU;

import java.nio.ByteBuffer;

class Renderer implements GLEventListener {
    private static final int MAZE_WIDTH = 128; // Maze Width  (Must Be A Power Of 2)
    private static final int MAZE_HEIGHT = 128; // Maze Height (Must Be A Power Of 2)

    private int mx,my;																// General Loops (Used For Seeking)
    private ByteBuffer tex_data = BufferUtil.newByteBuffer(MAZE_WIDTH * MAZE_HEIGHT * 3);
    private byte[] r = new byte[4];												// Random Colors (4 Red, 4 Green, 4 Blue)
    private byte[] g = new byte[4];
    private byte[] b = new byte[4];
    private GLU glu = new GLU();
    private GLUquadric quadric;
    private float xrot, yrot, zrot;												// Use For Rotation Of Objects

    private long previousTime = System.currentTimeMillis();

    private boolean resetMaze = false;

    public void resetMaze() {
        resetMaze = true;
    }

    private void updateTex(int dmx, int dmy)										// Update Pixel dmx, dmy On The Texture
    {
        tex_data.put(((dmx + (MAZE_WIDTH * dmy)) * 3), (byte) 255);								// Set Red Pixel To Full Bright
        tex_data.put(1 + ((dmx + (MAZE_WIDTH * dmy)) * 3), (byte) 255);								// Set Green Pixel To Full Bright
        tex_data.put(2 + ((dmx + (MAZE_WIDTH * dmy)) * 3), (byte) 255);								// Set Blue Pixel To Full Bright
    }

    private void reset()														// Reset The Maze, Colors, Start Point, Etc
    {
        tex_data.clear();
        while(tex_data.hasRemaining()) {
            tex_data.put((byte)0);
        }
        tex_data.flip();

        for (int loop = 0; loop < 4; loop++)									// Loop So We Can Assign 4 Random Colors
        {
            r[loop] = (byte) ((int) (Math.random() * Integer.MAX_VALUE) % 128 + 128);											// Pick A Random Red Color (Bright)
            g[loop] = (byte) ((int) (Math.random() * Integer.MAX_VALUE) % 128 + 128);											// Pick A Random Green Color (Bright)
            b[loop] = (byte) ((int) (Math.random() * Integer.MAX_VALUE) % 128 + 128);											// Pick A Random Blue Color (Bright)
        }

        setRandomMazePosition();
    }

    private void setRandomMazePosition() {
        mx = (int) Math.round(Math.random() * ((MAZE_WIDTH - 1) / 2)) * 2; // Pick A New Random X Position
        my = (int) Math.round(Math.random() * ((MAZE_HEIGHT - 1) / 2)) * 2; // Pick A New Random Y Position
    }

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

        reset();															// Call Reset To Build Our Initial Texture, Etc.

        // Start Of User Initialization
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
        gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, MAZE_WIDTH, MAZE_HEIGHT, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, tex_data);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);								// Black Background
        gl.glClearDepth(1.0f);												// Depth Buffer Setup

        gl.glDepthFunc(GL.GL_LEQUAL);											// The Type Of Depth Testing
        gl.glEnable(GL.GL_DEPTH_TEST);											// Enable Depth Testing

        gl.glEnable(GL.GL_COLOR_MATERIAL);										// Enable Color Material (Allows Us To Tint Textures)

        gl.glEnable(GL.GL_TEXTURE_2D);											// Enable Texture Mapping

        quadric = glu.gluNewQuadric();											// Create A Pointer To The Quadric Object
        glu.gluQuadricNormals(quadric, GLU.GLU_SMOOTH);								// Create Smooth Normals
        glu.gluQuadricTexture(quadric, true);								// Create Texture Coords

        gl.glEnable(GL.GL_LIGHT0);												// Enable Light0 (Default GL Light)
    }

    private void update(float milliseconds)										// Perform Motion Updates Here
    {
        int dir;														// Will Hold Current Direction

        xrot += milliseconds * 0.02f;									// Increase Rotation On The X-Axis
        yrot += milliseconds * 0.03f;									// Increase Rotation On The Y-Axis
        zrot += milliseconds * 0.015f;									// Increase Rotation On The Z-Axis

        // Check To Make Sure We Are Not Trapped (Nowhere Else To Move)
        if ((mx > (MAZE_WIDTH - 4) || (tex_data.get((((mx + 2) + (MAZE_WIDTH * my)) * 3)) != 0)) &&
                (mx < 2 || (tex_data.get((((mx - 2) + (MAZE_WIDTH * my)) * 3)) != 0)) &&
                (my > (MAZE_HEIGHT - 4) || (tex_data.get(((mx + (MAZE_WIDTH * (my + 2))) * 3)) != 0)) &&
                (my < 2 || (tex_data.get(((mx + (MAZE_WIDTH * (my - 2))) * 3)) != 0))
        ) {
            do																// If We Are Trapped
            {
                setRandomMazePosition();
            } while (tex_data.get(((mx + (MAZE_WIDTH * my)) * 3)) == 0);						// Keep Picking A Random Position Until We Find
        }																	// One That Has Already Been Tagged (Safe Starting Point)

        dir = (int) Math.round(Math.random() * 3f);

        if ((dir == 0) && (mx <= (MAZE_WIDTH - 4)))									// If The Direction Is 0 (Right) And We Are Not At The Far Right
        {
            if (tex_data.get((((mx + 2) + (MAZE_WIDTH * my)) * 3)) == 0)						// And If The Room To The Right Has Not Already Been Visited
            {
                updateTex(mx + 1, my);											// Update The Texture To Show Path Cut Out Between Rooms
                mx += 2;														// Move To The Right (Room To The Right)
            }
        }

        if ((dir == 1) && (my <= (MAZE_HEIGHT - 4)))									// If The Direction Is 1 (Down) And We Are Not At The Bottom
        {
            if (tex_data.get(((mx + (MAZE_WIDTH * (my + 2))) * 3)) == 0)						// And If The Room Below Has Not Already Been Visited
            {
                updateTex(mx, my + 1);											// Update The Texture To Show Path Cut Out Between Rooms
                my += 2;														// Move Down (Room Below)
            }
        }

        if ((dir == 2) && (mx >= 2))											// If The Direction Is 2 (Left) And We Are Not At The Far Left
        {
            if (tex_data.get((((mx - 2) + (MAZE_WIDTH * my)) * 3)) == 0)						// And If The Room To The Left Has Not Already Been Visited
            {
                updateTex(mx - 1, my);											// Update The Texture To Show Path Cut Out Between Rooms
                mx -= 2;														// Move To The Left (Room To The Left)
            }
        }

        if ((dir == 3) && (my >= 2))											// If The Direction Is 3 (Up) And We Are Not At The Top
        {
            if (tex_data.get(((mx + (MAZE_WIDTH * (my - 2))) * 3)) == 0)						// And If The Room Above Has Not Already Been Visited
            {
                updateTex(mx, my - 1);											// Update The Texture To Show Path Cut Out Between Rooms
                my -= 2;														// Move Up (Room Above)
            }
        }

        updateTex(mx, my);													// Update Current Room
    }

    public void display(GLAutoDrawable drawable) {
        if (resetMaze) {

⌨️ 快捷键说明

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