|
How to draw a white rectangle on a black background |
|
|
This example shows how to draw a white rectangle on a black background using JOGL. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").
-> Copyright and Permission Notice
package glredbook11;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
/**
* This is a simple, introductory OpenGL program in Java using the
* javax.media.opengl extension library.
*
* @author Kiet Le (Java conversion)
*/
public class hello
extends JFrame
implements GLEventListener
{
private GLCapabilities caps;
private GLCanvas canvas;
//
public hello()
{
super("hello");
/*
* display mode (single buffer and RGBA)
*/
caps = new GLCapabilities();
caps.setDoubleBuffered(false);
System.out.println(caps.toString());
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
//
getContentPane().add(canvas);
}
/*
* Declare initial window size, position, and set frame's close behavior. Open
* window with "hello" in its title bar. Call initialization routines.
* Register callback function to display graphics. Enter main loop and process
* events.
*/
public void run()
{
setSize(512, 256);
setLocationRelativeTo(null); // center
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new hello().run();
}
public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
/* select clearing color (background) color */
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
/* initialize viewing values */
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
/* clear all pixels */
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
/*
* draw white polygon (rectangle) with corners at (0.25, 0.25, 0.0) and
* (0.75, 0.75, 0.0)
*/
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(0.25f, 0.25f, 0.0f);
gl.glVertex3f(0.75f, 0.25f, 0.0f);
gl.glVertex3f(0.75f, 0.75f, 0.0f);
gl.glVertex3f(0.25f, 0.75f, 0.0f);
gl.glEnd();
/*
* don't wait! start processing buffered OpenGL routines
*/
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height)
{
GL gl = drawable.getGL();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged)
{
}
}
|
Source:
Kiet Le's The Red Book Examples using JOGL
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.