Objected Oriented Graphics Engine

This is the basic object oriented graphics engine.

It includes a Triangle object, add a Cube objects and an objects that loads data from an OBJ file using Saito’s OBJ library.

This is the shape superclass:

 
import processing.opengl.*;
import javax.media.opengl.*; 
 
 
class Shape
{
   Shape()
   {
   }
 
  void drawShape(GL gl)
  {
 
  }
 
  void draw(GL gl)
  {
 
    gl.glColor3f(1, 0, 0);
 
    drawShape(gl);
 
  }
}

This is the triangle subclass:

 
import processing.opengl.*;
import javax.media.opengl.*; 
 
 
class Triangle extends Shape
{
  PVector points[];
 Triangle(PVector p1, PVector p2, PVector p3)
 {
    points = new PVector[3];
    points[0] = p1; 
    points[1] = p2;
    points[2] = p3;
 }
 
  void setPoint(int index, PVector p)
  {
     if (index >= 0 & index < 3)
      points[index]= p; 
  }
 
  void drawShape(GL gl)
  {
 
    gl.glBegin(GL.GL_TRIANGLES);
    gl.glVertex3f(points[0].x, points[0].y, points[0].z);
    gl.glVertex3f(points[1].x, points[1].y, points[1].z);
    gl.glVertex3f(points[2].x, points[2].y, points[2].z);
    gl.glEnd();
 
  }
}

This is the main sketch file that draws it.

 
import processing.opengl.*;
import javax.media.opengl.*; 
 
Shape shapes[];
 
void setup()
{
   size(600, 600, OPENGL); 
 
  shapes = new Shape[2];
  shapes[0] = new Triangle(new PVector(100, 100, -100),new PVector(200, 100, -100),new PVector(200, 200, -100));
  shapes[1] = new Triangle(new PVector(300, 200, -100),new PVector(400, 400, -100),new PVector(300, 400, -300));
}
 
void draw()
{
   background(255);
   strokeWeight(5);
 
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;  // g may change
    GL gl = pgl.beginGL();  // always use the GL object returned by beginGL
 
  for (int i = 0; i < shapes.length; i++)
    shapes[i].draw(gl);
 
 
    pgl.endGL();
 
 
}
PDF Creator    Send article as PDF to