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

📄 renderer.java

📁 NeHe用java与OpenGL结合教程源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            reset();
            resetMaze = false;
        }

        long currentTime = System.currentTimeMillis();
        update(currentTime - previousTime);
        previousTime = currentTime;

        GL gl = drawable.getGL();

        int windowWidth = drawable.getWidth();
        int windowHeight = drawable.getHeight();

        // Update Our Texture... This Is The Key To The Programs Speed... Much Faster Than Rebuilding The Texture Each Time
        gl.glTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, MAZE_WIDTH, MAZE_HEIGHT, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, tex_data);

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);										// Clear Screen

        for (int loop = 0; loop < 4; loop++)									// Loop To Draw Our 4 Views
        {
            gl.glColor3ub(r[loop], g[loop], b[loop]);							// Assign Color To Current View

            if (loop == 0)													// If We Are Drawing The First Scene
            {
                // Set The Viewport To The Top Left.  It Will Take Up Half The Screen Width And Height
                gl.glViewport(0, windowHeight / 2, windowWidth / 2, windowHeight / 2);
                gl.glMatrixMode(GL.GL_PROJECTION);								// Select The Projection Matrix
                gl.glLoadIdentity();											// Reset The Projection Matrix
                // Set Up Ortho Mode To Fit 1/4 The Screen (Size Of A Viewport)
                glu.gluOrtho2D(0, windowWidth / 2, windowHeight / 2, 0);
            }

            if (loop == 1)													// If We Are Drawing The Second Scene
            {
                // Set The Viewport To The Top Right.  It Will Take Up Half The Screen Width And Height
                gl.glViewport(windowWidth / 2, windowHeight / 2, windowWidth / 2, windowHeight / 2);
                gl.glMatrixMode(GL.GL_PROJECTION);								// Select The Projection Matrix
                gl.glLoadIdentity();											// Reset The Projection Matrix
                // Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
                glu.gluPerspective(45.0, (float) (MAZE_WIDTH) / (float) (MAZE_HEIGHT), 0.1f, 500.0);
            }

            if (loop == 2)													// If We Are Drawing The Third Scene
            {
                // Set The Viewport To The Bottom Right.  It Will Take Up Half The Screen Width And Height
                gl.glViewport(windowWidth / 2, 0, windowWidth / 2, windowHeight / 2);
                gl.glMatrixMode(GL.GL_PROJECTION);								// Select The Projection Matrix
                gl.glLoadIdentity();											// Reset The Projection Matrix
                // Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
                glu.gluPerspective(45.0, (float) (MAZE_WIDTH) / (float) (MAZE_HEIGHT), 0.1f, 500.0);
            }

            if (loop == 3)													// If We Are Drawing The Fourth Scene
            {
                // Set The Viewport To The Bottom Left.  It Will Take Up Half The Screen Width And Height
                gl.glViewport(0, 0, windowWidth / 2, windowHeight / 2);
                gl.glMatrixMode(GL.GL_PROJECTION);								// Select The Projection Matrix
                gl.glLoadIdentity();											// Reset The Projection Matrix
                // Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
                glu.gluPerspective(45.0, (float) (MAZE_WIDTH) / (float) (MAZE_HEIGHT), 0.1f, 500.0);
            }

            gl.glMatrixMode(GL.GL_MODELVIEW);									// Select The Modelview Matrix
            gl.glLoadIdentity();												// Reset The Modelview Matrix

            gl.glClear(GL.GL_DEPTH_BUFFER_BIT);									// Clear Depth Buffer

            if (loop == 0)													// Are We Drawing The First Image?  (Original Texture... Ortho)
            {
                gl.glBegin(GL.GL_QUADS);											// Begin Drawing A Singl.gle Quad
                // We Fill The Entire 1/4 Section With A Singl.gle Textured Quad.
                gl.glTexCoord2f(1.0f, 0.0f);
                gl.glVertex2i(windowWidth / 2, 0);
                gl.glTexCoord2f(0.0f, 0.0f);
                gl.glVertex2i(0, 0);
                gl.glTexCoord2f(0.0f, 1.0f);
                gl.glVertex2i(0, windowHeight / 2);
                gl.glTexCoord2f(1.0f, 1.0f);
                gl.glVertex2i(windowWidth / 2, windowHeight / 2);
                gl.glEnd();													// Done Drawing The Textured Quad
            }

            if (loop == 1)													// Are We Drawing The Second Image?  (3D Texture Mapped Sphere... Perspective)
            {
                gl.glTranslatef(0.0f, 0.0f, -14.0f);								// Move 14 Units Into The Screen

                gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);								// Rotate By xrot On The X-Axis
                gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);								// Rotate By yrot On The Y-Axis
                gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);								// Rotate By zrot On The Z-Axis

                gl.glEnable(GL.GL_LIGHTING);										// Enable Lighting
                glu.gluSphere(quadric, 4.0f, 32, 32);								// Draw A Sphere
                gl.glDisable(GL.GL_LIGHTING);										// Disable Lighting
            }

            if (loop == 2)													// Are We Drawing The Third Image?  (Texture At An Angl.gle... Perspective)
            {
                gl.glTranslatef(0.0f, 0.0f, -2.0f);								// Move 2 Units Into The Screen
                gl.glRotatef(-45.0f, 1.0f, 0.0f, 0.0f);							// Tilt The Quad Below Back 45 Degrees.
                gl.glRotatef(zrot / 1.5f, 0.0f, 0.0f, 1.0f);						// Rotate By zrot/1.5 On The Z-Axis

                gl.glBegin(GL.GL_QUADS);											// Begin Drawing A Singl.gle Quad
                gl.glTexCoord2f(1.0f, 1.0f);
                gl.glVertex3f(1.0f, 1.0f, 0.0f);
                gl.glTexCoord2f(0.0f, 1.0f);
                gl.glVertex3f(-1.0f, 1.0f, 0.0f);
                gl.glTexCoord2f(0.0f, 0.0f);
                gl.glVertex3f(-1.0f, -1.0f, 0.0f);
                gl.glTexCoord2f(1.0f, 0.0f);
                gl.glVertex3f(1.0f, -1.0f, 0.0f);
                gl.glEnd();													// Done Drawing The Textured Quad
            }

            if (loop == 3)													// Are We Drawing The Fourth Image?  (3D Texture Mapped Cylinder... Perspective)
            {
                gl.glTranslatef(0.0f, 0.0f, -7.0f);								// Move 7 Units Into The Screen
                gl.glRotatef(-xrot / 2, 1.0f, 0.0f, 0.0f);							// Rotate By -xrot/2 On The X-Axis
                gl.glRotatef(-yrot / 2, 0.0f, 1.0f, 0.0f);							// Rotate By -yrot/2 On The Y-Axis
                gl.glRotatef(-zrot / 2, 0.0f, 0.0f, 1.0f);							// Rotate By -zrot/2 On The Z-Axis

                gl.glEnable(GL.GL_LIGHTING);										// Enable Lighting
                gl.glTranslatef(0.0f, 0.0f, -2.0f);								// Translate -2 On The Z-Axis (To Rotate Cylinder Around The Center, Not An End)
                glu.gluCylinder(quadric, 1.5f, 1.5f, 4.0f, 32, 16);					// Draw A Cylinder
                gl.glDisable(GL.GL_LIGHTING);										// Disable Lighting
            }
        }

        gl.glFlush();															// Flush The GL.GL Rendering Pipeline
    }

    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) {
    }
}

⌨️ 快捷键说明

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