renderer.java

来自「NeHe用java与OpenGL结合教程源码」· Java 代码 · 共 465 行 · 第 1/2 页

JAVA
465
字号
    {
        float spost = 0.0f;                                      // Starting Texture Coordinate Offset
        float alpha = 0.2f;                                      // Starting Alpha Value

        // Disable AutoTexture Coordinates
        gl.glDisable(GL.GL_TEXTURE_GEN_S);
        gl.glDisable(GL.GL_TEXTURE_GEN_T);

        gl.glEnable(GL.GL_TEXTURE_2D);                           // Enable 2D Texture Mapping
        gl.glDisable(GL.GL_DEPTH_TEST);                          // Disable Depth Testing
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);              // Set Blending Mode
        gl.glEnable(GL.GL_BLEND);                                // Enable Blending
        gl.glBindTexture(GL.GL_TEXTURE_2D, blurTexture);         // Bind To The Blur Texture
        viewOrtho(gl);                                           // Switch To An Ortho View

        float alphainc = alpha / times;                          // alphainc=0.2f / Times To Render Blur

        gl.glBegin(GL.GL_QUADS);                                 // Begin Drawing Quads
        for (int num = 0; num < times; num++)                    // Number Of Times To Render Blur
        {
            gl.glColor4f(1.0f, 1.0f, 1.0f, alpha);               // Set The Alpha Value (Starts At 0.2)
            gl.glTexCoord2f(0 + spost, 1 - spost);               // Texture Coordinate	( 0, 1 )
            gl.glVertex2f(0, 0);                                 // First Vertex		(   0,   0 )

            gl.glTexCoord2f(0 + spost, 0 + spost);               // Texture Coordinate	( 0, 0 )
            gl.glVertex2f(0, 480);                               // Second Vertex	(   0, 480 )

            gl.glTexCoord2f(1 - spost, 0 + spost);               // Texture Coordinate	( 1, 0 )
            gl.glVertex2f(640, 480);                             // Third Vertex		( 640, 480 )

            gl.glTexCoord2f(1 - spost, 1 - spost);               // Texture Coordinate	( 1, 1 )
            gl.glVertex2f(640, 0);                               // Fourth Vertex	( 640,   0 )

            spost += inc;                                        // Gradually Increase spost (Zooming Closer To Texture Center)
            alpha = alpha - alphainc;                            // Gradually Decrease alpha (Gradually Fading Image Out)
        }
        gl.glEnd();                                              // Done Drawing Quads

        viewPerspective(gl);                                     // Switch To A Perspective View

        gl.glEnable(GL.GL_DEPTH_TEST);                           // Enable Depth Testing
        gl.glDisable(GL.GL_TEXTURE_2D);                          // Disable 2D Texture Mapping
        gl.glDisable(GL.GL_TEXTURE_2D);                          // Disable 2D Texture Mapping
        gl.glDisable(GL.GL_TEXTURE_2D);                          // Disable 2D Texture Mapping
        gl.glDisable(GL.GL_BLEND);                               // Disable Blending
        gl.glBindTexture(GL.GL_TEXTURE_2D, 0);                   // Unbind The Blur Texture
    }

    public void init(GLAutoDrawable drawable) {
        drawable.setGL(new DebugGL(drawable.getGL()));

        GL gl = drawable.getGL();

        // Start Of User Initialization
        angle = 0.0f;                                            // Set Starting Angle To Zero

        // Create Our Empty Texture
        blurTexture = createBlurTexture(gl);

        if (gl.isExtensionAvailable("GL_EXT_framebuffer_object")) {
            frameBufferObject = createFrameBufferObject(gl);
        } else {
            frameBufferObject = -1;
        }

        if (frameBufferObject != -1) {
            display.setTitle(display.getTitle() + " using frame buffer object");
        } else {
            display.setTitle(display.getTitle() + " using default frame buffer");
        }

        gl.glEnable(GL.GL_DEPTH_TEST);                                  // Enable Depth Testing

        float[] global_ambient = new float[]{0.2f, 0.2f, 0.2f, 1.0f};   // Set Ambient Lighting To Fairly Dark Light (No Color)
        float[] light0pos = new float[]{0.0f, 5.0f, 10.0f, 1.0f};       // Set The Light Position
        float[] light0ambient = new float[]{0.2f, 0.2f, 0.2f, 1.0f};    // More Ambient Light
        float[] light0diffuse = new float[]{0.3f, 0.3f, 0.3f, 1.0f};    // Set The Diffuse Light A Bit Brighter
        float[] light0specular = new float[]{0.8f, 0.8f, 0.8f, 1.0f};   // Fairly Bright Specular Lighting

        float[] lmodel_ambient = new float[]{0.2f, 0.2f, 0.2f, 1.0f};   // And More Ambient Light
        gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);// Set The Ambient Light Model

        gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, global_ambient, 0);// Set The Global Ambient Light Model
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light0pos, 0);       // Set The Lights Position
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, light0ambient, 0);    // Set The Ambient Light
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light0diffuse, 0);    // Set The Diffuse Light
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light0specular, 0);  // Set Up Specular Lighting
        gl.glEnable(GL.GL_LIGHTING);                                    // Enable Lighting
        gl.glEnable(GL.GL_LIGHT0);                                      // Enable Light0

        gl.glShadeModel(GL.GL_SMOOTH);                                  // Select Smooth Shading

        gl.glMateriali(GL.GL_FRONT, GL.GL_SHININESS, 128);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                        // Set The Clear Color To Black
    }

    /**
     * Creates a frame buffer object.
     * @return the newly created frame buffer object is or -1 if a frame buffer object could not be created
     */
    private int createFrameBufferObject(GL gl) {
        // Create the FBO
        int[] frameBuffer = new int[1];
        gl.glGenFramebuffersEXT(1, frameBuffer, 0);
        gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, frameBuffer[0]);

        // Create a TEXTURE_SIZE x TEXTURE_SIZE RGBA texture that will be used as color attachment
        // for the fbo.
        int[] colorBuffer = new int[1];
        gl.glGenTextures(1, colorBuffer, 0);                 // Create 1 Texture
        gl.glBindTexture(GL.GL_TEXTURE_2D, colorBuffer[0]);  // Bind The Texture
        gl.glTexImage2D(                                     // Build Texture Using Information In data
                                                             GL.GL_TEXTURE_2D,
                                                             0,
                                                             GL.GL_RGBA,
                                                             TEXTURE_SIZE,
                                                             TEXTURE_SIZE,
                                                             0,
                                                             GL.GL_RGBA,
                                                             GL.GL_UNSIGNED_BYTE,
                                                             BufferUtil.newByteBuffer(TEXTURE_SIZE * TEXTURE_SIZE * 4)
        );
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);

        // Attach the texture to the frame buffer as the color attachment. This
        // will cause the results of rendering to the FBO to be written in the blur texture.
        gl.glFramebufferTexture2DEXT(
                GL.GL_FRAMEBUFFER_EXT,
                GL.GL_COLOR_ATTACHMENT0_EXT,
                GL.GL_TEXTURE_2D,
                colorBuffer[0],
                0
        );

        gl.glBindTexture(GL.GL_TEXTURE_2D, 0);

        // Create a 24-bit TEXTURE_SIZE x TEXTURE_SIZE depth buffer for the FBO.
        // We need this to get correct rendering results.
        int[] depthBuffer = new int[1];
        gl.glGenRenderbuffersEXT(1, depthBuffer, 0);
        gl.glBindRenderbufferEXT(GL.GL_RENDERBUFFER_EXT, depthBuffer[0]);
        gl.glRenderbufferStorageEXT(GL.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT24, TEXTURE_SIZE, TEXTURE_SIZE);

        // Attach the newly created depth buffer to the FBO.
        gl.glFramebufferRenderbufferEXT(
                GL.GL_FRAMEBUFFER_EXT,
                GL.GL_DEPTH_ATTACHMENT_EXT,
                GL.GL_RENDERBUFFER_EXT,
                depthBuffer[0]
        );

        // Make sure the framebuffer object is complete (i.e. set up correctly)
        int status = gl.glCheckFramebufferStatusEXT(GL.GL_FRAMEBUFFER_EXT);
        if (status == GL.GL_FRAMEBUFFER_COMPLETE_EXT) {
            return frameBuffer[0];
        } else {
            // No matter what goes wrong, we simply delete the frame buffer object
            // This switch statement simply serves to list all possible error codes
            switch(status) {
                case GL.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
                    // One of the attachments is incomplete
                case GL.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
                    // Not all attachments have the same size
                case GL.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
                    // The desired read buffer has no attachment
                case GL.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
                    // The desired draw buffer has no attachment
                case GL.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
                    // Not all color attachments have the same internal format
                case GL.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
                    // No attachments have been attached
                case GL.GL_FRAMEBUFFER_UNSUPPORTED_EXT:
                    // The combination of internal formats is not supported
                case GL.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
                    // This value is no longer in the EXT_framebuffer_object specification
                default:
                    // Delete the color buffer texture
                    gl.glDeleteTextures(1, colorBuffer, 0);
                    // Delete the depth buffer
                    gl.glDeleteRenderbuffersEXT(1, depthBuffer, 0);
                    // Delete the FBO
                    gl.glDeleteFramebuffersEXT(1, frameBuffer, 0);
                    return -1;
            }
        }
    }

    private void update(long milliseconds) {                            // Perform Motion Updates Here
        angle += (float) (milliseconds) / 5.0f;                         // Update angle Based On The Clock
    }

    public void display(GLAutoDrawable drawable) {
        long currentTime = System.currentTimeMillis();
        update(currentTime - previousTime);
        previousTime = currentTime;

        GL gl = drawable.getGL();
        gl.glLoadIdentity();                                            // Reset The View
        renderToTexture(gl, glu);                                       // Render To A Texture

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                        // Set The Clear Color To Black
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
        processHelix(gl, glu);                                          // Draw Our Helix

        drawBlur(gl, 25, 0.02f);                                        // Draw The Blur Effect
        gl.glFlush();                                                   // Flush The 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(50, (float) width / height, 5, 2000);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void displayChanged(GLAutoDrawable drawable,
                               boolean modeChanged,
                               boolean deviceChanged) {
    }
}

⌨️ 快捷键说明

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