a digital scan of a 35mm film image of a processing sketch running on an LCD
Skip to Content

Triangle Soup

A quick assignment for GLART, drawing an animated field of triangles.

Here’s the code:

  1.         package mika;
  2.         import java.awt.event.MouseEvent;
  3.         import javax.media.opengl.*;
  4.         import jocode.*;
  5.  
  6.         /**
  7.          * DemoBasicGeometry.java
  8.          * <P>
  9.          * Demonstrate six types of geometry using glBegin()…glEnd()
  10.          * <P>
  11.          * napier at potatoland dot org
  12.          */
  13.  
  14.         public class Week1 extends JOApp {
  15.  
  16.           // Set the mouse position in a way that’s
  17.           // useful for translating objects at 0 Z
  18.           public float screenCursorX;
  19.           public float screenCursorY;
  20.           public float tempValue;
  21.           public float sizeMult;
  22.  
  23.           /**
  24.            * Start the application, Run() initializes the OpenGL context, calls setup(),
  25.            * handles mouse and keyboard input, and calls draw() in a loop.
  26.            */
  27.           public static void main(String args[]) {
  28.             // create the app
  29.             Week1 demo = new Week1();
  30.  
  31.             // set title, window size
  32.             windowTitle = "Hello World";
  33.             displayWidth = 1440;
  34.             displayHeight = 900;
  35.  
  36.             // start running: will call init(), setup(), draw(), mouse functions
  37.             demo.run();
  38.           }
  39.  
  40.           /**
  41.            * Initialize settings. Will be called once when app starts. Called by
  42.            * JOApp.init().
  43.            */
  44.           @Override
  45.           public void setup() {
  46.             // set a background color
  47.             gl.glClearColor(0f, 0f, 0f, 1f);
  48.  
  49.             // Move over to my second monitor for testing…
  50.             // TODO Disable this IRL
  51.             frame.setLocation(-1440, 150);
  52.           }
  53.  
  54.           /**
  55.            * Render one frame. Called by the JOApp.display() callback function.
  56.            */
  57.           @Override
  58.           public void draw() {
  59.  
  60.             // Clear screen and depth buffer
  61.             gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  62.  
  63.             // Select The Modelview Matrix (controls model orientation)
  64.             gl.glMatrixMode(GL.GL_MODELVIEW);
  65.  
  66.             gl.glEnable(GL.GL_BLEND);
  67.             gl.glBlendFunc(GL.GL_SRC0_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
  68.             // gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
  69.  
  70.             // Reset the Modelview matrix
  71.             // this resets the coordinate system to center of screen
  72.             gl.glLoadIdentity();
  73.  
  74.             // Where is the ‘eye’
  75.             glu.gluLookAt(0f, 0f, 10f, // eye position
  76.                 6f, 1f, 0f, // target to look at
  77.                 0f, 1f, 0f); // which way is up
  78.  
  79.             gl.glColor4f(1f, 1f, 1f, 0.5f);
  80.             // gl.glColor3f(1f, 1f, 1f); // color will affect all the following verts
  81.  
  82.             sizeMult = 3;
  83.             tempValue = (tempValue + .02f) % 1000;
  84.  
  85.             for (int i = 0; i < 200; i++) {
  86.               gl.glRotatef(tempValue, tempValue, tempValue, tempValue);
  87.  
  88.               gl.glTranslatef(.1f, .1f, .1f);
  89.  
  90.               gl.glBegin(GL.GL_TRIANGLES);
  91.               {
  92.                 gl.glVertex3f(0f, 0f, 0f); // top
  93.                 gl.glVertex3f(-0.5f * sizeMult, -1f * sizeMult, 0f); // lower left
  94.                 gl.glVertex3f(0.5f * sizeMult, -1f * sizeMult, 0f); // lower right
  95.               }
  96.               gl.glEnd();
  97.             }
  98.  
  99.             // reset vertex color to white
  100.             gl.glColor3f(1f, 1f, 1f);
  101.           }
  102.  
  103.           @Override
  104.           public void mouseMoved(MouseEvent _event) {
  105.             // Call the parent method since it actually gives us the
  106.             // Better just to copy the whole method?
  107.             super.mouseMoved(_event);
  108.             screenCursorX = cursorX / (displayWidth / 10f);
  109.             screenCursorY = cursorY / (displayHeight / 10f) - 3f;
  110.           }
  111.  
  112.         }
January 27 2010 at 3 PM