Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import com.jogamp.opengl.GL;
- 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.fixedfunc.GLMatrixFunc;
- import com.jogamp.opengl.glu.GLU;
- import com.jogamp.opengl.util.Animator;
- public class MainFrame
- extends JFrame
- implements GLEventListener
- {
- private GLCanvas canvas;
- private Animator animator;
- private GLU glu;
- // Application main entry point
- public static void main(String args[])
- {
- new MainFrame();
- }
- // Default constructor;
- public MainFrame()
- {
- super("Java OpenGL");
- // Registering a window event listener to handle the closing event.
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(800, 600);
- this.initializeJogl();
- this.setVisible(true);
- }
- private void initializeJogl()
- {
- // Creating a new GL profile.
- GLProfile glprofile = GLProfile.getDefault();
- // Creating an object to manipulate OpenGL parameters.
- GLCapabilities capabilities = new GLCapabilities(glprofile);
- // Setting some OpenGL parameters.
- capabilities.setHardwareAccelerated(true);
- capabilities.setDoubleBuffered(true);
- // Try to enable 2x anti aliasing. It should be supported on most hardware.
- capabilities.setNumSamples(2);
- capabilities.setSampleBuffers(true);
- // Creating an OpenGL display widget -- canvas.
- this.canvas = new GLCanvas(capabilities);
- // Adding the canvas in the center of the frame.
- this.getContentPane().add(this.canvas);
- // Adding an OpenGL event listener to the canvas.
- this.canvas.addGLEventListener(this);
- // Creating an animator that will redraw the scene 40 times per second.
- this.animator = new Animator(this.canvas);
- // // Registering the canvas to the animator.
- // this.animator.add(this.canvas);
- // Starting the animator.
- this.animator.start();
- }
- public void init(GLAutoDrawable canvas)
- {
- // Obtaining the GL instance associated with the canvas.
- GL2 gl = canvas.getGL().getGL2();
- // Initialize GLU. We'll need it for perspective and camera setup.
- this.glu = GLU.createGLU();
- // Setting the clear color -- the color which will be used to erase the canvas.
- gl.glClearColor(0, 0, 0, 0);
- // Selecting the modelview matrix.
- gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
- }
- public void display(GLAutoDrawable canvas)
- {
- GL2 gl = canvas.getGL().getGL2();
- // Erasing the canvas -- filling it with the clear color.
- gl.glClear(GL.GL_COLOR_BUFFER_BIT);
- gl.glLoadIdentity();
- // Add your scene code here
- // Patrat
- gl.glBegin(GL2.GL_LINES);
- gl.glColor3f(1.0f, 0.0f, 0.0f);
- gl.glVertex3f(0.2f, 0.2f, 0);
- gl.glVertex3f(0.4f, 0.2f, 0);
- gl.glVertex3f(0.2f, 0.4f, 0);
- gl.glVertex3f(0.4f, 0.4f, 0);
- gl.glVertex3f(0.2f, 0.2f, 0);
- gl.glVertex3f(0.2f, 0.4f, 0);
- gl.glVertex3f(0.4f, 0.4f, 0);
- gl.glVertex3f(0.4f, 0.2f, 0);
- // Miscator
- if (topLeftX > 0.95f) {
- pas = -pas;
- }
- if (topLeftX < 0) {
- pas = -pas;
- }
- drawSquare(gl);
- topLeftX += pas;
- gl.glEnd();
- // Forcing the scene to be rendered.
- gl.glFlush();
- }
- float topLeftX = 0.2f;
- float topLeftY = 0.4f;
- float pas = 0.1f;
- public void drawSquare(GL2 gl) {
- gl.glBegin(GL2.GL_LINES);
- gl.glVertex3f(topLeftX, topLeftY, 0);
- gl.glVertex3f(topLeftX+0.2f, topLeftY, 0);
- gl.glVertex3f(topLeftX+0.2f, topLeftY, 0);
- gl.glVertex3f(topLeftX+0.2f, topLeftY-0.2f, 0);
- gl.glVertex3f(topLeftX+0.2f, topLeftY-0.2f, 0);
- gl.glVertex3f(topLeftX, topLeftY-0.2f, 0);
- gl.glVertex3f(topLeftX, topLeftY-0.2f, 0);
- gl.glVertex3f(topLeftX, topLeftY, 0);
- gl.glEnd();
- }
- public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height)
- {
- GL2 gl = canvas.getGL().getGL2();
- // Selecting the viewport -- the display area -- to be the entire widget.
- gl.glViewport(0, 0, width, height);
- // Determining the width to height ratio of the widget.
- double ratio = (double) width / (double) height;
- // Selecting the projection matrix.
- gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
- gl.glLoadIdentity();
- glu.gluPerspective (38, ratio, 0.1, 100);
- // Selecting the modelview matrix.
- gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
- }
- public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged)
- {
- return;
- }
- @Override
- public void dispose(GLAutoDrawable arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement