Triangle Soup
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
- * <P>
- * Demonstrate six types of geometry using glBegin()…glEnd()
- * <P>
- * 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(0f, 0f, 0f, 1f);
- // 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(0f, 0f, 10f, // eye position
- 6f, 1f, 0f, // target to look at
- 0f, 1f, 0f); // which way is up
- gl.glColor4f(1f, 1f, 1f, 0.5f);
- // gl.glColor3f(1f, 1f, 1f); // color will affect all the following verts
- sizeMult = 3;
- tempValue = (tempValue + .02f) % 1000;
- for (int i = 0; i < 200; i++) {
- gl.glRotatef(tempValue, tempValue, tempValue, tempValue);
- gl.glTranslatef(.1f, .1f, .1f);
- gl.glBegin(GL.GL_TRIANGLES);
- {
- gl.glVertex3f(0f, 0f, 0f); // top
- gl.glVertex3f(-0.5f * sizeMult, -1f * sizeMult, 0f); // lower left
- gl.glVertex3f(0.5f * sizeMult, -1f * sizeMult, 0f); // lower right
- }
- gl.glEnd();
- }
- // reset vertex color to white
- gl.glColor3f(1f, 1f, 1f);
- }
- @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 / 10f);
- screenCursorY = cursorY / (displayHeight / 10f) - 3f;
- }
- }




Add Your Comment