Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Just a box in OpenGL - Processing 2.0.3

$
0
0
Hey, just a (hopefully) quick question about getting OpenGL up and running in Processing 2.0.3. Every time I update Processing I end up fighting for way too long trying to display even just a simple box with OpenGL.

So, here's a program to display a simple box. Hopefully I'm just overlooking something simple, because all that it's displaying right now is a black screen, whereas in 2.0b7 it works exactly how I want it to. Thanks for any help!

  1. import processing.opengl.*;
  2. import javax.media.opengl.GL;
  3. import javax.media.opengl.GL2;
  4. import javax.media.opengl.glu.GLU;
  5. import java.util.*;

  6. PVector[] v;
  7. ArrayList<Quad> quads;

  8. public void setup() {
  9.   size(600, 480, OPENGL);
  10.  
  11.   //verts for da cube
  12.   v = new PVector[8];
  13.   v[0] = new PVector(1, 1, 1);
  14.   v[1] = new PVector(-1, 1, 1);
  15.   v[2] = new PVector(-1, -1, 1);
  16.   v[3] = new PVector(1, -1, 1);
  17.   v[4] = new PVector(1, 1, -1);
  18.   v[5] = new PVector(-1, 1, -1);
  19.   v[6] = new PVector(-1, -1, -1);
  20.   v[7] = new PVector(1, -1, -1);
  21.  
  22.   quads = new ArrayList<Quad>();
  23.   quads.add(new Quad(v[0], v[1], v[2], v[3])); //top
  24.   quads.add(new Quad(v[4], v[5], v[6], v[7])); //bottom
  25.   quads.add(new Quad(v[3], v[2], v[6], v[7])); //front
  26.   quads.add(new Quad(v[1], v[0], v[4], v[5])); //back
  27.   quads.add(new Quad(v[2], v[1], v[5], v[6])); //left
  28.   quads.add(new Quad(v[0], v[3], v[7], v[4])); //right
  29.  
  30. }

  31. public void draw() {
  32.  
  33.   background(0, 0, 0);
  34.  
  35.   //all the OpenGL setup bullshit
  36.   PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  37.   PGL pgl = pg.beginPGL();
  38.   GL gl = pgl.gl;
  39.   GL2 gl2 = pgl.gl.getGL2();
  40.   GLU glu = pgl.glu;
  41.  
  42.   // additive blending
  43.   gl2.glEnable(GL.GL_BLEND);
  44.   gl2.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
  45.   // disable depth test
  46.   gl2.glDisable(GL.GL_DEPTH_TEST);
  47.  
  48.   //camera janks
  49.   gl2.glMatrixMode(GL2.GL_MODELVIEW);
  50.   gl2.glLoadIdentity();
  51.   glu.gluPerspective(45.0, width/height, .1, 1000.0 );
  52.   glu.gluLookAt(6, 6, 6,
  53.                 0, 0, 0,
  54.                 0, 0, 1);
  55.  
  56.   //drawing the cube
  57.   gl2.glPushMatrix();
  58.   gl2.glRotatef(mouseX*2f/width * 360, 1.0, 0.0, 0.0);
  59.   gl2.glRotatef(mouseY*2f/height * 360, 0.0, 1.0, 0.0);
  60.   for (Quad q : quads) {
  61.     gl2.glBegin(gl2.GL_POLYGON);
  62.    
  63.     gl2.glColor4f(.5f, .5f, .5f, .5f);
  64.     gl2.glVertex3f(q.v1.x, q.v1.y, q.v1.z);
  65.     gl2.glVertex3f(q.v2.x, q.v2.y, q.v2.z);
  66.     gl2.glVertex3f(q.v3.x, q.v3.y, q.v3.z);
  67.     gl2.glVertex3f(q.v4.x, q.v4.y, q.v4.z);
  68.    
  69.     gl2.glEnd();
  70.   }
  71.   gl2.glPopMatrix();
  72.  
  73.   pg.endPGL();
  74. }

  75. class Quad {
  76.  
  77.   public PVector v1, v2, v3, v4;
  78.  
  79.   public Quad(PVector v1, PVector v2, PVector v3, PVector v4)
  80.   {
  81.     this.v1 = v1;
  82.     this.v2 = v2;
  83.     this.v3 = v3;
  84.     this.v4 = v4;
  85.   }
  86.  
  87. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles