package com.test; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; import com.jogamp.opengl.util.awt.Overlay; import com.jogamp.opengl.util.gl2.GLUT; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import java.util.Random; import javax.swing.JFrame; /** * * @author yogesh.mevada */ public class TestRenderer extends GLCanvas implements GLEventListener, MouseListener, MouseMotionListener, MouseWheelListener, KeyListener { protected GL2 gl2; protected GLU glu; protected GLUT glut; protected Overlay overlay; protected Camera camera; protected Point lastpoint; protected int width, height; protected boolean xRotation = true; protected boolean yRotation = true; protected double x, y, z; protected static boolean lightsOn; protected static boolean spotlight = true; protected float[] mat_specular = {1.0f, 1.0f, 1.0f, 1.0f}; protected float[] mat_ambient_magenta = {1.0f, 0.0f, 1.0f, 1.0f}; protected float[] mat_shininess = {100.0f}; protected int counter = 1; protected Random random = new Random(1000); public TestRenderer(int windowWidth, int windowHeight, GLCapabilities capabilities) { super(capabilities); JFrame frame = new JFrame(); frame.setVisible(true); frame.setSize(new Dimension(windowWidth, windowHeight)); frame.getContentPane().add(this); addGLEventListener(this); setSize(800, 400); setVisible(true); addMouseListener(this); addMouseMotionListener(this); addMouseWheelListener(this); addKeyListener(this); } @Override public void init(GLAutoDrawable autoDrawable) { gl2 = autoDrawable.getGL().getGL2(); glu = new GLU(); glut = new GLUT(); gl2.glEnable(GL2.GL_LIGHTING); gl2.glEnable(GL2.GL_LIGHT0); gl2.glDepthFunc(GL2.GL_LESS); gl2.glEnable(GL2.GL_DEPTH_TEST); gl2.glShadeModel(GL2.GL_SMOOTH); gl2.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); gl2.glClearColor(0.0f, 0.0f, 0.0f, 0f); setCamera(); overlay = new Overlay(autoDrawable); final FPSAnimator fPSAnimator = new FPSAnimator(this, 15, true); fPSAnimator.start(); } @Override public void dispose(GLAutoDrawable autoDrawable) { } @Override public void display(GLAutoDrawable autoDrawable) { clearCanvas(); boolean boxTexture = false; camera.updateViewLocation(new double[]{x, y, z}); camera.resetCamera(glu); float[] light_ambient = {1.0f, 1.0f, 1.0f, 1.0f}; float[] light_diffuse = {1.0f, 1.0f, 1.0f, 1.0f}; float[] light_specular = {1.0f, 1.0f, 1.0f, 1.0f}; float[] light_position = {5.0f, 0.0f, 0.0f, 1.0f}; float[] direction = {0.0f, -1.0f, 0.0f, 0.0f}; float angle = 30f; setLightSpot(light_ambient, light_diffuse, light_specular, light_position, direction, angle); drawAxis(new int[]{150, 150, 150}, new int[]{0, 0, 0}); drawBox(new float[]{0.1f, 0.1f, 0.1f}, light_position); int number = random.nextInt(counter++) + 1; Graphics2D g2d = overlay.createGraphics(); overlay.beginRendering(); g2d.setColor(Color.RED); g2d.drawRect(number % width, number % height, 50, 50); overlay.markDirty(0, 0, width, height); overlay.draw(0, 0, width, height); overlay.endRendering(); g2d.dispose(); } @Override public void reshape(GLAutoDrawable autoDrawable, int x, int y, int width, int height) { gl2.glMatrixMode(GL2.GL_PROJECTION); gl2.glLoadIdentity(); glu.gluPerspective(45, (float) width / (float) height, 0.1f, 100); gl2.glMatrixMode(GL2.GL_MODELVIEW); gl2.glLoadIdentity(); this.width = width; this.height = height; } @Override public void mouseDragged(MouseEvent e) { Point ms = e.getPoint(); float dx = getCameraAngle(xRotation, ms.x, lastpoint.x, width); float dy = getCameraAngle(yRotation, ms.y, lastpoint.y, height); if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { camera.updateThetaPhi(-dx * 2.0f, dy * 2.0f); } lastpoint = ms; } @Override public void mouseMoved(MouseEvent e) { lastpoint = e.getPoint(); } @Override public void mouseWheelMoved(MouseWheelEvent e) { camera.updateRadius(e.getWheelRotation()); } @Override public void mouseClicked(MouseEvent e) { if (e.getModifiers() == MouseEvent.BUTTON2_MASK) { lightsOn = !lightsOn; } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void keyTyped(KeyEvent event) { } @Override public void keyPressed(KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_W) { z -= 1; } else if (event.getKeyCode() == KeyEvent.VK_S) { z += 1; } else if (event.getKeyCode() == KeyEvent.VK_A) { x -= 1; } else if (event.getKeyCode() == KeyEvent.VK_D) { x += 1; } } @Override public void keyReleased(KeyEvent event) { } private void setCamera() { float radius = 10.0f; double theta = Math.toRadians(-45); double phi = Math.toRadians(45); camera = new Camera(radius, theta, phi); } public void clearCanvas() { gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl2.glLoadIdentity(); } private void setLightSpot(float[] light_ambient, float[] light_diffuse, float[] light_specular, float[] light_position, float[] direction, float angle) { if (lightsOn) { gl2.glPushMatrix(); gl2.glEnable(GL2.GL_LIGHTING); gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_ambient, 0); gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0); gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0); gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0); if (spotlight) { gl2.glLightf(GL2.GL_LIGHT0, GL2.GL_SPOT_CUTOFF, angle); gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPOT_DIRECTION, direction, 0); } gl2.glPopMatrix(); } else { gl2.glDisable(GL2.GL_LIGHTING); } } private void drawAxis(int[] size, int[] location) { gl2.glBegin(GL2.GL_LINE_LOOP); for (int i = 0; i < axis3DQuads.length; i++) { gl2.glColor3f(axisColorQuads[i][0], axisColorQuads[i][1], axisColorQuads[i][2]); gl2.glVertex3f(axis3DQuads[i][0] * size[0] + location[0], axis3DQuads[i][1] * size[1] + location[1], axis3DQuads[i][2] * size[2] + location[2]); } gl2.glColor3f(1.0f, 1.0f, 1.0f); gl2.glEnd(); } private void drawBox(float[] size, float[] location) { gl2.glPushMatrix(); gl2.glTranslatef(location[0], location[1], location[2]); gl2.glBegin(GL2.GL_QUADS); for (int iCount = 0; iCount < cube3DQuads.length; iCount++) { gl2.glColor3f(cube3DColorCoords[iCount][0], cube3DColorCoords[iCount][1], cube3DColorCoords[iCount][2]); gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, cube3DAmbient[iCount / 4], 0); gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, cube3DSpecular[iCount / 4], 0); gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_EMISSION, cube3DEmission[iCount / 4], 0); gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_SHININESS, cube3DShininess[iCount / 4], 0); gl2.glNormal3f(cube3DNormals[iCount / 4][0], cube3DNormals[iCount / 4][1], cube3DNormals[iCount / 4][2]); gl2.glVertex3f(cube3DQuads[iCount][0] * size[0], cube3DQuads[iCount][1] * size[1], cube3DQuads[iCount][2] * size[2]); } gl2.glEnd(); gl2.glPopMatrix(); } private float getCameraAngle(boolean rotationEnabled, int currentLoc, int lastLoc, int maxValue) { if (rotationEnabled) { return (float) (currentLoc - lastLoc) / maxValue; } return 0; } static public float[][] axis3DQuads = { {1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, 0, 0}, {0, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, 0}, {0, 0, 0}, {0, 0, -1} }; static public float[][] axisColorQuads = { {1, 0, 0}, {1, 0, 0}, {1, 1, 1}, {1, 1, 1}, {0, 1, 0}, {0, 1, 0}, {1, 1, 1}, {1, 1, 1}, {0, 0, 1}, {0, 0, 1}, {1, 1, 1}, {1, 1, 1} }; static public float[][] planeQuads = { {-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0} }; static public float[][] planeColotQuads = { {0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 1, 0} }; public static float[][] planeTextureCoords = { {0, 0}, {1, 0}, {1, 1}, {0, 1} }; static public float[][] cube3DQuads = { {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1}, //front {1, -1, 1}, {1, -1, -1}, {1, 1, -1}, {1, 1, 1}, //right {-1, 1, 1}, {1, 1, 1}, {1, 1, -1}, {-1, 1, -1}, //top {-1, -1, -1}, {-1, -1, 1}, {-1, 1, 1}, {-1, 1, -1}, //left {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, //back {-1, -1, 1}, {1, -1, 1}, {1, -1, -1}, {-1, -1, -1} //bottom }; static public float[][] cube3DAmbient = { {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f, 1.0f} }; private float[][] cube3DSpecular = { {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f} }; private float[][] cube3DEmission = { {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f} }; private float[][] cube3DShininess = { {0.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f} }; static public float[][] cube3DNormals = { {0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, 0, -1}, {0, -1, 0} }; public static float[][] cubeMulti3DColorCoords = { {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} }; public static float[][] cubeSame3DColorCoords = { {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f}, {0.3f, 0.3f, 0.3f} }; public static float[][] cube3DColorCoords = { {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f}, {0.1f, 0.1f, 0.1f} }; public static void main(String[] args) { int windowWidth = 1024; int windowHeight = 768; final GLProfile profile = GLProfile.get(GLProfile.GL2); final GLCapabilities capabilities = new GLCapabilities(profile); TestRenderer testRenderer = new TestRenderer(windowWidth, windowHeight, capabilities); } }