A quick assignment for GLART, drawing an animated field of triangles.
Here’s the code:
package mika;
import java.awt.event.MouseEvent;
import javax.media.opengl.*;
import jocode.*;
/**
* DemoBasicGeometry.java
*
* Demonstrate six types of geometry using glBegin()...glEnd()
*
* napier at potatoland dot org
*/
public class Week1 extends JOApp {
// Set the mouse position in a way that's
// useful for translating objects at 0 Z
public float screenCursorX;
public float screenCursorY;
public float tempValue;
public float sizeMult;
/**
* Start the application, Run() initializes the OpenGL context, calls setup(),
* handles mouse and keyboard input, and calls draw() in a loop.
*/
public static void main(String args[]) {
// create the app
Week1 demo = new Week1();
// set title, window size
windowTitle = "Hello World";
displayWidth = 1440;
displayHeight = 900;
// start running: will call init(), setup(), draw(), mouse functions
demo.run();
}
/**
* Initialize settings. Will be called once when app starts. Called by
* JOApp.init().
*/
@Override
public void setup() {
// set a background color
gl.glClearColor(0 f, 0 f, 0 f, 1 f);
// Move over to my second monitor for testing...
// TODO Disable this IRL
frame.setLocation(-1440, 150);
}
/**
* Render one frame. Called by the JOApp.display() callback function.
*/
@Override
public void draw() {
// Clear screen and depth buffer
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Select The Modelview Matrix (controls model orientation)
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC0_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
// gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
// Reset the Modelview matrix
// this resets the coordinate system to center of screen
gl.glLoadIdentity();
// Where is the 'eye'
glu.gluLookAt(0 f, 0 f, 10 f, // eye position
6 f, 1 f, 0 f, // target to look at
0 f, 1 f, 0 f); // which way is up
// color will affect all the following verts
gl.glColor4f(1 f, 1 f, 1 f, 0.5 f);
// gl.glColor3f(1f, 1f, 1f);
sizeMult = 3;
tempValue = (tempValue + .02 f) % 1000;
for (int i = 0; i < 200; i++) {
gl.glRotatef(tempValue, tempValue, tempValue, tempValue);
gl.glTranslatef(.1 f, .1 f, .1 f);
gl.glBegin(GL.GL_TRIANGLES); {
// top
gl.glVertex3f(0 f, 0 f, 0 f);
// lower left
gl.glVertex3f(-0.5 f * sizeMult, -1 f * sizeMult, 0 f);
// lower right
gl.glVertex3f(0.5 f * sizeMult, -1 f * sizeMult, 0 f);
}
gl.glEnd();
}
// reset vertex color to white
gl.glColor3f(1 f, 1 f, 1 f);
}
@Override
public void mouseMoved(MouseEvent _event) {
// Call the parent method since it actually gives us the
// Better just to copy the whole method?
super.mouseMoved(_event);
screenCursorX = cursorX / (displayWidth / 10 f);
screenCursorY = cursorY / (displayHeight / 10 f) - 3 f;
}
}


