creating git repo. Note project 9 is missing data, will have to load from backup
This commit is contained in:
parent
644759c8db
commit
061038fb5c
200
src/nehe/Lesson01.java
Normal file
200
src/nehe/Lesson01.java
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package nehe.Lesson01;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render allows java to talk to the graphics card via openGL
|
||||||
|
|
||||||
|
* Implements{@link GLEventListener}
|
||||||
|
* @author Beta
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Render implements GLEventListener{
|
||||||
|
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Added at start of tutorial two...
|
||||||
|
*/
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 1");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param p Current Instance of JPanel
|
||||||
|
* @param frame Current Instance of JFrame
|
||||||
|
* @param r Current Instance of Render
|
||||||
|
*/
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
||||||
|
/*
|
||||||
|
* JComponent.WHEN_IN_FOCUSED_WINDOW fixes a bug when you click off of the
|
||||||
|
* JPanel that you can't see but we know is their :)
|
||||||
|
*/
|
||||||
|
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 4110491783931256564L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Toggles the screen between full screen
|
||||||
|
* @param f The JFrame that holds the GLCanvus
|
||||||
|
*/
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
200
src/nehe/Lesson02.java
Normal file
200
src/nehe/Lesson02.java
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
package nehe.Lesson02;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Beta
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Render implements GLEventListener{
|
||||||
|
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
|
||||||
|
gl.glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
|
||||||
|
gl.glBegin(GL2.GL_TRIANGLES); // Drawing Using Triangl.gles
|
||||||
|
gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top
|
||||||
|
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
|
||||||
|
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
|
||||||
|
gl.glEnd(); // Finished Drawing The Triangl.gle
|
||||||
|
gl.glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
|
||||||
|
gl.glBegin(GL2.GL_QUADS); // Draw A Quad
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
|
||||||
|
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
|
||||||
|
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
|
||||||
|
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
|
||||||
|
gl.glEnd(); // Done Drawing The Quad
|
||||||
|
gl.glFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 2");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap();
|
||||||
|
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
211
src/nehe/Lesson03.java
Normal file
211
src/nehe/Lesson03.java
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
package nehe.Lesson03;
|
||||||
|
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Beta
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Render implements GLEventListener{
|
||||||
|
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
|
||||||
|
gl.glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
|
||||||
|
gl.glBegin(GL2.GL_TRIANGLES); // Drawing Using Triangl.gles
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); //Red
|
||||||
|
gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); //Green
|
||||||
|
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); //Blue
|
||||||
|
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
|
||||||
|
gl.glEnd(); // Finished Drawing The Triangl.gle
|
||||||
|
gl.glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
|
||||||
|
|
||||||
|
gl.glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
|
||||||
|
gl.glBegin(GL2.GL_QUADS); // Draw A Quad
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
|
||||||
|
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
|
||||||
|
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
|
||||||
|
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
|
||||||
|
gl.glEnd(); // Done Drawing The Quad
|
||||||
|
gl.glFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 2");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap();
|
||||||
|
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
225
src/nehe/Lesson04.java
Normal file
225
src/nehe/Lesson04.java
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
package nehe.Lesson04;
|
||||||
|
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Beta
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Render implements GLEventListener{
|
||||||
|
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
private float rquad=0.0f, rtri =0.0f;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
|
||||||
|
gl.glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
|
||||||
|
|
||||||
|
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);//(NEW!)
|
||||||
|
|
||||||
|
gl.glBegin(GL2.GL_TRIANGLES); // Drawing Using Triangl.gles
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); //Red
|
||||||
|
gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); //Green
|
||||||
|
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); //Blue
|
||||||
|
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
|
||||||
|
gl.glEnd(); // Finished Drawing The Triangl.gle
|
||||||
|
/**
|
||||||
|
* IMportant!
|
||||||
|
*/
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
gl.glTranslatef(1.5f,0.0f,-6.0f); // Move Right 3 Units
|
||||||
|
gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
gl.glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
|
||||||
|
gl.glBegin(GL2.GL_QUADS); // Draw A Quad
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
|
||||||
|
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
|
||||||
|
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
|
||||||
|
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
|
||||||
|
gl.glEnd(); // Done Drawing The Quad
|
||||||
|
gl.glFlush();
|
||||||
|
rtri +=0.2f;
|
||||||
|
rquad -=0.15f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 4");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap();
|
||||||
|
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
295
src/nehe/Lesson05.java
Normal file
295
src/nehe/Lesson05.java
Normal file
@ -0,0 +1,295 @@
|
|||||||
|
package nehe.Lesson05;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Beta
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Render implements GLEventListener{
|
||||||
|
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
private float rquad=0.0f, rtri =0.0f;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
|
||||||
|
gl.glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
|
||||||
|
|
||||||
|
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);//(NEW!)
|
||||||
|
|
||||||
|
gl.glBegin(GL2.GL_TRIANGLES); // Drawing Using Triangl.gles
|
||||||
|
/*
|
||||||
|
* Front
|
||||||
|
*/
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
|
||||||
|
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Left Of Triangle (Front)
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Right Of Triangle (Front)
|
||||||
|
/*
|
||||||
|
* Right
|
||||||
|
*/
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
|
||||||
|
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Left Of Triangle (Right)
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Right Of Triangle (Right)
|
||||||
|
/*
|
||||||
|
* Left
|
||||||
|
*/
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
|
||||||
|
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Left Of Triangle (Back)
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Right Of Triangle (Back)
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
|
||||||
|
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Left Of Triangle (Left)
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Right Of Triangle (Left)
|
||||||
|
|
||||||
|
gl.glEnd(); // Done Drawing The Pyramid
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cube
|
||||||
|
*/
|
||||||
|
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
gl.glTranslatef(1.5f, 0.0f, -7.0f); // Move Right And Into The Screen
|
||||||
|
|
||||||
|
gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f); // Rotate The Cube On X, Y & Z
|
||||||
|
|
||||||
|
gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube
|
||||||
|
|
||||||
|
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
|
||||||
|
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
|
||||||
|
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
|
||||||
|
|
||||||
|
gl.glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom)
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom)
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom)
|
||||||
|
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
|
||||||
|
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front)
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front)
|
||||||
|
|
||||||
|
gl.glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back)
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back)
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
|
||||||
|
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)
|
||||||
|
|
||||||
|
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
|
||||||
|
gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left)
|
||||||
|
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left)
|
||||||
|
|
||||||
|
gl.glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
|
||||||
|
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
|
||||||
|
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
|
||||||
|
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
|
||||||
|
gl.glEnd(); // Done Drawing The Quad
|
||||||
|
gl.glFlush();
|
||||||
|
rtri +=0.2f;
|
||||||
|
rquad -=0.15f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glShadeModel(GL2.GL_SMOOTH);
|
||||||
|
gl.glClearColor(0f, 0f, 0f, 0f);
|
||||||
|
gl.glClearDepth(1.0f);
|
||||||
|
gl.glEnable(GL2.GL_DEPTH_TEST);
|
||||||
|
gl.glDepthFunc(GL2.GL_LEQUAL);
|
||||||
|
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 5");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap();
|
||||||
|
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
254
src/nehe/Lesson06.java
Normal file
254
src/nehe/Lesson06.java
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
package nehe.Lesson06;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
import com.jogamp.opengl.util.texture.Texture;
|
||||||
|
import com.jogamp.opengl.util.texture.TextureIO;
|
||||||
|
|
||||||
|
public class Render implements GLEventListener {
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
private float xrot, yrot,zrot;
|
||||||
|
private int texture;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
gl.glTranslatef(0f, 0f, -5.0f);
|
||||||
|
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
|
||||||
|
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
|
||||||
|
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
|
||||||
|
gl.glBegin(GL2.GL_QUADS);
|
||||||
|
// Front Face
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
// Back Face
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
// Top Face
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
// Bottom Face
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
// Right face
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
// Left Face
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glEnd();
|
||||||
|
gl.glFlush();
|
||||||
|
xrot+=.3f;
|
||||||
|
yrot+=.2f;
|
||||||
|
zrot+=.4;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glShadeModel(GL2.GL_SMOOTH);
|
||||||
|
gl.glClearColor(0f, 0f, 0f, 0f);
|
||||||
|
gl.glClearDepth(1.0f);
|
||||||
|
gl.glEnable(GL2.GL_DEPTH_TEST);
|
||||||
|
gl.glDepthFunc(GL2.GL_LEQUAL);
|
||||||
|
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_TEXTURE_2D);
|
||||||
|
try{
|
||||||
|
File im = new File("data/NeHE.png");
|
||||||
|
Texture t = TextureIO.newTexture(im, true);
|
||||||
|
texture= t.getTextureObject(gl);
|
||||||
|
}catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 6");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap();
|
||||||
|
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
423
src/nehe/Lesson07.java
Normal file
423
src/nehe/Lesson07.java
Normal file
@ -0,0 +1,423 @@
|
|||||||
|
package nehe.Lesson07;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
import com.jogamp.opengl.util.texture.Texture;
|
||||||
|
import com.jogamp.opengl.util.texture.TextureIO;
|
||||||
|
|
||||||
|
public class Render implements GLEventListener {
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
private float xrot, yrot,zrot;
|
||||||
|
private int [] texture = new int [3];
|
||||||
|
/*
|
||||||
|
* New Code
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
|
||||||
|
private float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
private float[] lightPosition = { 0.0f, 0.0f, 2.0f, 1.0f };
|
||||||
|
private int filter;
|
||||||
|
private float xspeed, yspeed;
|
||||||
|
private boolean light;
|
||||||
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
gl.glTranslatef(0f, 0f, -5.0f);
|
||||||
|
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
|
||||||
|
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
|
||||||
|
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[filter]);
|
||||||
|
if(light)
|
||||||
|
gl.glEnable(GL2.GL_LIGHTING);
|
||||||
|
else
|
||||||
|
gl.glDisable(GL2.GL_LIGHTING);
|
||||||
|
|
||||||
|
gl.glBegin(GL2.GL_QUADS);
|
||||||
|
// Front Face
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
// Back Face
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
// Top Face
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
// Bottom Face
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
// Right face
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
// Left Face
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glEnd();
|
||||||
|
gl.glFlush();
|
||||||
|
xrot+=.3f;
|
||||||
|
yrot+=.2f;
|
||||||
|
zrot+=.4;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glShadeModel(GL2.GL_SMOOTH);
|
||||||
|
gl.glClearColor(0f, 0f, 0f, 0f);
|
||||||
|
gl.glClearDepth(1.0f);
|
||||||
|
gl.glEnable(GL2.GL_DEPTH_TEST);
|
||||||
|
gl.glDepthFunc(GL2.GL_LEQUAL);
|
||||||
|
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_TEXTURE_2D);
|
||||||
|
try{
|
||||||
|
File im = new File("data/crate.png");
|
||||||
|
Texture t = TextureIO.newTexture(im, true);
|
||||||
|
texture[0]= t.getTextureObject(gl);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[0]);
|
||||||
|
|
||||||
|
t = TextureIO.newTexture(im, true);
|
||||||
|
texture[1]= t.getTextureObject(gl);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[1]);
|
||||||
|
|
||||||
|
t = TextureIO.newTexture(im, true);
|
||||||
|
texture[2]= t.getTextureObject(gl);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR_MIPMAP_NEAREST);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[2]);
|
||||||
|
/**
|
||||||
|
* Lighting
|
||||||
|
*/
|
||||||
|
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, this.lightAmbient,0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, this.lightDiffuse,0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, this.lightPosition,0);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_LIGHT1);
|
||||||
|
gl.glEnable(GL2.GL_LIGHTING);
|
||||||
|
|
||||||
|
this.light=true;
|
||||||
|
}catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 6");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap();
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UP");
|
||||||
|
actionMap.put("UP", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.xspeed+=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DOWN");
|
||||||
|
actionMap.put("DOWN", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.xspeed-=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LEFT");
|
||||||
|
actionMap.put("LEFT", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.yspeed+=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RIGHT");
|
||||||
|
actionMap.put("RIGHT", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.yspeed-=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F, 0), "F");
|
||||||
|
actionMap.put("F", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.filter++;
|
||||||
|
if(r.filter>r.texture.length-1)
|
||||||
|
r.filter=0;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_L, 0), "L");
|
||||||
|
actionMap.put("L", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(r.light)
|
||||||
|
r.light=false;
|
||||||
|
else
|
||||||
|
r.light=true;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
460
src/nehe/Lesson08.java
Normal file
460
src/nehe/Lesson08.java
Normal file
@ -0,0 +1,460 @@
|
|||||||
|
package nehe.Lesson08;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
import com.jogamp.opengl.util.texture.Texture;
|
||||||
|
import com.jogamp.opengl.util.texture.TextureIO;
|
||||||
|
|
||||||
|
public class Render implements GLEventListener {
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
private float xrot, yrot,zrot;
|
||||||
|
private int [] texture = new int [3];
|
||||||
|
/*
|
||||||
|
* New Code
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
|
||||||
|
private float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
private float[] lightPosition = { 0.0f, 0.0f, 2.0f, 1.0f };
|
||||||
|
private int filter;
|
||||||
|
private float xspeed, yspeed;
|
||||||
|
private boolean light;
|
||||||
|
/*
|
||||||
|
* New Code
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private boolean blend;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
gl.glLoadIdentity(); // Reset The View
|
||||||
|
gl.glTranslatef(0f, 0f, -5.0f);
|
||||||
|
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
|
||||||
|
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
|
||||||
|
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[filter]);
|
||||||
|
if(light)
|
||||||
|
gl.glEnable(GL2.GL_LIGHTING);
|
||||||
|
else
|
||||||
|
gl.glDisable(GL2.GL_LIGHTING);
|
||||||
|
if(blend){
|
||||||
|
gl.glEnable(GL2.GL_BLEND);
|
||||||
|
gl.glDisable(GL2.GL_DEPTH_TEST);
|
||||||
|
}else{
|
||||||
|
gl.glDisable(GL2.GL_BLEND);
|
||||||
|
gl.glEnable(GL2.GL_DEPTH_TEST);
|
||||||
|
}
|
||||||
|
gl.glBegin(GL2.GL_QUADS);
|
||||||
|
// Front Face
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
// Back Face
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
// Top Face
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
// Bottom Face
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
// Right face
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
|
||||||
|
// Left Face
|
||||||
|
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
|
||||||
|
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
|
||||||
|
gl.glEnd();
|
||||||
|
gl.glFlush();
|
||||||
|
xrot+=.3f;
|
||||||
|
yrot+=.2f;
|
||||||
|
zrot+=.4;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glShadeModel(GL2.GL_SMOOTH);
|
||||||
|
gl.glClearColor(0f, 0f, 0f, 0f);
|
||||||
|
gl.glClearDepth(1.0f);
|
||||||
|
gl.glEnable(GL2.GL_DEPTH_TEST);
|
||||||
|
gl.glDepthFunc(GL2.GL_LEQUAL);
|
||||||
|
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_TEXTURE_2D);
|
||||||
|
try{
|
||||||
|
File im = new File("data/Glass.bmp");
|
||||||
|
Texture t = TextureIO.newTexture(im, true);
|
||||||
|
texture[0]= t.getTextureObject(gl);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[0]);
|
||||||
|
|
||||||
|
t = TextureIO.newTexture(im, true);
|
||||||
|
texture[1]= t.getTextureObject(gl);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[1]);
|
||||||
|
|
||||||
|
t = TextureIO.newTexture(im, true);
|
||||||
|
texture[2]= t.getTextureObject(gl);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
|
||||||
|
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR_MIPMAP_NEAREST);
|
||||||
|
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture[2]);
|
||||||
|
/**
|
||||||
|
* Lighting
|
||||||
|
*/
|
||||||
|
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, this.lightAmbient,0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, this.lightDiffuse,0);
|
||||||
|
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, this.lightPosition,0);
|
||||||
|
|
||||||
|
gl.glEnable(GL2.GL_LIGHT1);
|
||||||
|
gl.glEnable(GL2.GL_LIGHTING);
|
||||||
|
|
||||||
|
this.light=true;
|
||||||
|
|
||||||
|
gl.glColor4f(1f, 1f, 1f, 0.5f);//50% Alpha
|
||||||
|
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE);
|
||||||
|
|
||||||
|
}catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
|
||||||
|
int height) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
if(height <=0)
|
||||||
|
height =1;
|
||||||
|
final float h = (float) width / (float) height;
|
||||||
|
gl.glViewport(0, 0, width, height);
|
||||||
|
gl.glMatrixMode(GL2.GL_PROJECTION);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
glu.gluPerspective(45.0f, h, 1.0, 20.0);
|
||||||
|
gl.glMatrixMode(GL2.GL_MODELVIEW);
|
||||||
|
gl.glLoadIdentity();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// setUp open GL version 2
|
||||||
|
final GLProfile profile = GLProfile.get(GLProfile.GL2);
|
||||||
|
GLCapabilities capabilities = new GLCapabilities(profile);
|
||||||
|
|
||||||
|
// The canvas
|
||||||
|
final GLCanvas glcanvas = new GLCanvas(capabilities);
|
||||||
|
Render r = new Render();
|
||||||
|
glcanvas.addGLEventListener(r);
|
||||||
|
glcanvas.setSize(400, 400);
|
||||||
|
|
||||||
|
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true );
|
||||||
|
|
||||||
|
final JFrame frame = new JFrame ("nehe: Lesson 08");
|
||||||
|
|
||||||
|
frame.getContentPane().add(glcanvas);
|
||||||
|
|
||||||
|
//Shutdown
|
||||||
|
frame.addWindowListener(new WindowAdapter(){
|
||||||
|
public void windowClosing(WindowEvent e){
|
||||||
|
if(animator.isStarted())
|
||||||
|
animator.stop();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
frame.setSize(frame.getContentPane().getPreferredSize());
|
||||||
|
/**
|
||||||
|
* Centers the screen on start up
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
|
||||||
|
GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
|
||||||
|
|
||||||
|
dm_old = devices[0].getDisplayMode();
|
||||||
|
dm = dm_old;
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
|
||||||
|
int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
|
||||||
|
int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
|
||||||
|
|
||||||
|
frame.setLocation(windowX, windowY);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
frame.setVisible(true);
|
||||||
|
/*
|
||||||
|
* Time to add Button Control
|
||||||
|
*/
|
||||||
|
JPanel p = new JPanel();
|
||||||
|
p.setPreferredSize(new Dimension(0,0));
|
||||||
|
frame.add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
keyBindings(p, frame, r);
|
||||||
|
animator.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
|
||||||
|
|
||||||
|
ActionMap actionMap = p.getActionMap();
|
||||||
|
InputMap inputMap = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
|
||||||
|
actionMap.put("F1", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
fullScreen(frame);
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UP");
|
||||||
|
actionMap.put("UP", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.xspeed+=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DOWN");
|
||||||
|
actionMap.put("DOWN", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.xspeed-=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LEFT");
|
||||||
|
actionMap.put("LEFT", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.yspeed+=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RIGHT");
|
||||||
|
actionMap.put("RIGHT", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.yspeed-=0.1f;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F, 0), "F");
|
||||||
|
actionMap.put("F", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.filter++;
|
||||||
|
if(r.filter>r.texture.length-1)
|
||||||
|
r.filter=0;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_L, 0), "L");
|
||||||
|
actionMap.put("L", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(r.light)
|
||||||
|
r.light=false;
|
||||||
|
else
|
||||||
|
r.light=true;
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_B, 0), "B");
|
||||||
|
actionMap.put("B", new AbstractAction(){
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6576101918414437189L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
r.blend= r.blend?false:true;
|
||||||
|
|
||||||
|
}});
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void fullScreen(JFrame f) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
if(!isFullScreen){
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(true);
|
||||||
|
f.setVisible(true);
|
||||||
|
f.setResizable(false);
|
||||||
|
xgraphic = f.getSize();
|
||||||
|
point = f.getLocation();
|
||||||
|
f.setLocation(0, 0);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
|
||||||
|
isFullScreen=true;
|
||||||
|
}else{
|
||||||
|
f.dispose();
|
||||||
|
f.setUndecorated(false);
|
||||||
|
f.setResizable(true);
|
||||||
|
f.setLocation(point);
|
||||||
|
f.setSize(xgraphic);
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
isFullScreen =false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
141
src/nehe/Lesson09.java
Normal file
141
src/nehe/Lesson09.java
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package nehe.Lesson09;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.DisplayMode;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.media.opengl.GL2;
|
||||||
|
import javax.media.opengl.GLAutoDrawable;
|
||||||
|
import javax.media.opengl.GLCapabilities;
|
||||||
|
import javax.media.opengl.GLEventListener;
|
||||||
|
import javax.media.opengl.GLProfile;
|
||||||
|
import javax.media.opengl.awt.GLCanvas;
|
||||||
|
import javax.media.opengl.glu.GLU;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.ActionMap;
|
||||||
|
import javax.swing.InputMap;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
|
||||||
|
import com.jogamp.opengl.util.FPSAnimator;
|
||||||
|
import com.jogamp.opengl.util.texture.Texture;
|
||||||
|
import com.jogamp.opengl.util.texture.TextureIO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render allows java to talk to the graphics card via openGL
|
||||||
|
|
||||||
|
* Implements{@link GLEventListener}
|
||||||
|
* @author Beta
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Render implements GLEventListener{
|
||||||
|
|
||||||
|
private static GraphicsEnvironment graphicsEnviorment;
|
||||||
|
private static boolean isFullScreen = false;
|
||||||
|
public static DisplayMode dm, dm_old;
|
||||||
|
private static Dimension xgraphic;
|
||||||
|
private static Point point = new Point(0, 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Added at start of tutorial two...
|
||||||
|
*/
|
||||||
|
private GLU glu = new GLU();
|
||||||
|
|
||||||
|
private boolean twinkle;
|
||||||
|
final int num=50;
|
||||||
|
stars star[] = new stars[num];
|
||||||
|
class stars{
|
||||||
|
int r,g,b;
|
||||||
|
float dist, angle;
|
||||||
|
}
|
||||||
|
float zoom =-15.0f, tilt=90f, spin;
|
||||||
|
int [] texture = new int [1];
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
|
||||||
|
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
|
||||||
|
gl.glBindTexture(gl.GL_TEXTURE_2D, texture[0]); // Select Our Texture
|
||||||
|
|
||||||
|
for(int loop=num-1; loop>=0; loop--){ // Loop Through All The Stars
|
||||||
|
gl.glLoadIdentity(); // Reset The View Before We Draw Each Star
|
||||||
|
gl.glTranslatef(0.0f,0.0f,zoom); // Zoom Into The Screen (Using The Value In 'zoom')
|
||||||
|
gl.glRotatef(tilt,1.0f,0.0f,0.0f); // Tilt The View (Using The Value In 'tilt')
|
||||||
|
gl.glRotatef(star[loop].angle,0.0f,1.0f,0.0f); // Rotate To The Current Stars Angle
|
||||||
|
gl.glTranslatef(star[loop].dist,0.0f,0.0f); // Move Forward On The X Plane
|
||||||
|
gl.glRotatef(-star[loop].angle,0.0f,1.0f,0.0f); // Cancel The Current Stars Angle
|
||||||
|
gl.glRotatef(-tilt,1.0f,0.0f,0.0f); // Cancel The Screen Tilt
|
||||||
|
|
||||||
|
if(twinkle){
|
||||||
|
gl.glColor4ub((byte)star[(num-loop)-1].r,(byte)star[(num-loop)-1].g,(byte)star[(num-loop)-1].b,(byte)255);
|
||||||
|
gl.glBegin(gl.GL_QUADS);
|
||||||
|
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.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.glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
gl.glRotatef(spin,0.0f,0.0f,1.0f);
|
||||||
|
gl.glColor4ub((byte)star[loop].r,(byte)star[loop].g,(byte)star[loop].b,(byte)255);
|
||||||
|
gl.glBegin(gl.GL_QUADS);
|
||||||
|
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.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.glEnd();
|
||||||
|
|
||||||
|
spin+=0.01f;
|
||||||
|
star[loop].angle+=(float)loop/num;
|
||||||
|
star[loop].dist-=0.01f;
|
||||||
|
|
||||||
|
if(star[loop].dist<0.0f){
|
||||||
|
star[loop].dist+=5.0f;
|
||||||
|
star[loop].r = (int)(Math.random()*1000)%256;
|
||||||
|
star[loop].g = (int)(Math.random()*1000)%256;
|
||||||
|
star[loop].b = (int)(Math.random()*1000)%256;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(GLAutoDrawable drawable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
final GL2 gl = drawable.getGL().getGL2();
|
||||||
|
gl.glEnable(gl.GL_TEXTURE_2D); // Enable Texture Mapping
|
||||||
|
gl.glShadeModel(gl.GL_SMOOTH); // Enable Smooth Shading
|
||||||
|
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
|
||||||
|
gl.glClearDepth(1.0f); // Depth Buffer Setup
|
||||||
|
gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT,gl.GL_NICEST); // Really Nice Perspective Calculations
|
||||||
|
gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE); // Set The Blending Function For Translucency
|
||||||
|
gl.glEnable(gl.GL_BLEND);
|
||||||
|
|
||||||
|
// Not code is messing after this line
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user