Java 怎么渲染并且初始化OpenGL

2019年7月29日20:02:29Java 怎么渲染并且初始化OpenGL已关闭评论 443 2896字阅读9分39秒

1.简介

Java 怎么渲染并且初始化OpenGL?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import static javax.media.opengl.GL.GL_LINES;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;

public class RenderingHelper {


    public static float[] getPlaneCoordsArray(float axisX, float axisY,
            float axisZ, float width, float height) {
        float[] resultArray = new float[12];

        if (axisX != 0) {
            resultArray = new float[] { axisX, width, height, axisX, width,
                    -height, axisX, -width, -height, axisX, -width, height };
        } else if (axisY != 0) {
            resultArray = new float[] { width, axisY, -height, -width,
                    axisY, -height, -width, axisY, height, width, axisY,
                    height };
        } else if (axisZ != 0) {
            resultArray = new float[] { width, -height, axisZ, -width,
                    -height, axisZ, -width, height, axisZ, width, height,
                    axisZ };
        }

        return resultArray;
    }

    public static void drawRectanglePlane(GL2 gl, float axisX, float axisY,
            float axisZ, float width, float height) {
        float[] coordsArray = getPlaneCoordsArray(axisX, axisY, axisZ,
                width, height);

        gl.glNormal3f(axisX, axisY, axisZ);

        for (int i = 0; i < 12; i += 3) {
            gl.glVertex3f(coordsArray[i], coordsArray[i + 1],
                    coordsArray[i + 2]);
        }
    }

    public static void setLighting(GL2 gl, float[] lightPos,
            float[] lightColorAmbient, float[] lightColorSpecular) {
        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, Settings.lightPos, 0);
        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT,
                Settings.lightColorAmbient, 0);
        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR,
                Settings.lightColorSpecular, 0);
    }

    public static void setMaterialProperties(GL2 gl, float shininess,
            float[] colorAmbient, float[] colorSpecular) {
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, colorAmbient, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, colorSpecular, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, shininess);
    }

    public static void drawParallelepiped(GL2 gl, float width,
            float height, float depth) {
        drawParallelepiped(gl, width, height, depth, Settings.rgb);
    }

    public static void drawParallelepiped(GL2 gl, float width,
            float height, float depth, float[] rgb) {
        gl.glEnable(GL2.GL_LIGHT1);
        gl.glEnable(GL2.GL_LIGHTING);

        setLighting(gl, Settings.lightPos, Settings.lightColorAmbient,
                Settings.lightColorSpecular);

        setMaterialProperties(gl, Settings.shininess, rgb, rgb);

        gl.glBegin(GL2.GL_QUADS);

        gl.glColor3f(0, 1, 0); 
        drawRectanglePlane(gl, 0, height, 0, width, depth); 

        gl.glColor3f(1, 0, 0); 
        drawRectanglePlane(gl, 0, -height, 0, width, depth);

        gl.glColor3f(1, 0, 0); 
        drawRectanglePlane(gl, 0, 0, depth, width, height); 

        gl.glColor3f(1, 1, 0); 
        drawRectanglePlane(gl, 0, 0, -depth, width, height);

        gl.glColor3f(0, 0, 1); 
        drawRectanglePlane(gl, -width, 0, 0, height, depth);

        gl.glColor3f(1, 0, 1); 
        drawRectanglePlane(gl, width, 0, 0, height, depth); 

        gl.glEnd();

        gl.glDisable(GL2.GL_LIGHT1);
        gl.glDisable(GL2.GL_LIGHTING);
    }

    public static void setDefaultCamera(GL2 gl, float angleX, float angleY) {
        gl.glLoadIdentity(); 
        gl.glTranslatef(0, 0, -25);
        gl.glRotatef(angleX, 0, 1, 0);
        gl.glRotatef(angleY, 1, 0, 0);
        gl.glTranslatef(0, 8, 0);
    }

    public static void drawBoneLine(GL2 gl, float x1, float y1, float z1,
            float x2, float y2, float z2) {
        gl.glLineWidth(1.5f);
        gl.glBegin(GL_LINES);

        gl.glColor3f(0, 1.0f, 0);
        gl.glVertex3f(x1, y1, z1);
        gl.glVertex3f(x2, y2, z2);

        gl.glEnd();
    }

}

 
继续阅读
编程十万个怎么办