Java examples for Media:OpenGL
rendering and opengl initialization
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 };// w w w. ja v a2 s . c om } 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); // green drawRectanglePlane(gl, 0, height, 0, width, depth); // Top-face gl.glColor3f(1, 0, 0); // orange drawRectanglePlane(gl, 0, -height, 0, width, depth);// Bottom-face gl.glColor3f(1, 0, 0); // red drawRectanglePlane(gl, 0, 0, depth, width, height); // Front-face gl.glColor3f(1, 1, 0); // yellow drawRectanglePlane(gl, 0, 0, -depth, width, height);// Back-face gl.glColor3f(0, 0, 1); // blue drawRectanglePlane(gl, -width, 0, 0, height, depth);// Left-face gl.glColor3f(1, 0, 1); // magenta drawRectanglePlane(gl, width, 0, 0, height, depth); // Right-face gl.glEnd(); gl.glDisable(GL2.GL_LIGHT1); gl.glDisable(GL2.GL_LIGHTING); } public static void setDefaultCamera(GL2 gl, float angleX, float angleY) { gl.glLoadIdentity(); // reset 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(); } }