We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Creating a solid tetrahedron
Page Index Toggle Pages: 1
Creating a solid tetrahedron (Read 2180 times)
Creating a solid tetrahedron
Aug 11th, 2009, 2:43pm
 
How can I create a solid tetrahedron? I know the four vertices however I cannot figure out how they can connect when I place the vertex statements in beginshape and endshape. When I do this they create the correct vertices however they're not connected completely and the fill is incomplete.

Code:

int x = 0;
int y = 0;
float rotX = 0;
float rotY = 0;

void setup()
{
size(400, 400,P3D);
}

void draw()
{
background(204);

pushMatrix();
translate(width/2, height/2);
rotateX(rotX);
rotateY(rotY);

beginShape(TRIANGLE_FAN);
vertex(x, y + 44, -44);
vertex(x, y - 44, 0);
vertex(x - 50, y + 44, 44);
vertex(x + 50, y + 44, 44);
endShape(CLOSE);

popMatrix();
}

void keyPressed()
{
if (key == CODED)
{
if (keyCode == UP)
rotX += 0.05;
else if (keyCode == DOWN)
rotX -= 0.05;
else if (keyCode == LEFT)
rotY -= 0.05;
else if (keyCode == RIGHT)
rotY += 0.05;
}
}
Re: Creating a solid tetrahedron
Reply #1 - Aug 11th, 2009, 3:24pm
 
how can you draw a tetrahedron with a triangle fan? a triangle fan means all triangles have a common point.

i'd draw 4 triangles and then think about using a triangle strip (but probably wouldn't bother), but a fan's not going to work.
Re: Creating a solid tetrahedron
Reply #2 - Aug 12th, 2009, 3:04pm
 
As pointed out by koogy, you could probably do a triangle strip as follows (keeping the vertices you had in your code):

Quote:
int x = 0;
int y = 0;
float rotX = 0;
float rotY = 0;

void setup()
{
  size(400, 400,P3D);
}

void draw()
{
  background(204);
  
  pushMatrix();
  translate(width/2, height/2);
  rotateX(rotX);
  rotateY(rotY);
  
  beginShape(TRIANGLE_STRIP);
  vertex(x, y + 44, -44);  // vertex 1
  vertex(x, y - 44, 0);    // vertex 2
  vertex(x - 50, y + 44, 44);  // vertex 3
  
  vertex(x + 50, y + 44, 44);   // vertex 4
  vertex(x, y + 44, -44);  // vertex 1
  
  vertex(x, y - 44, 0);    // vertex 2
  vertex(x + 50, y + 44, 44);   // vertex 4
  
  vertex(x - 50, y + 44, 44);  // vertex 3
  vertex(x, y - 44, 0);    // vertex 2
    
  endShape(CLOSE);

  popMatrix();
}

void keyPressed()
{
  if (key == CODED)
  {
    if (keyCode == UP)
     rotX += 0.05;
    else if (keyCode == DOWN)
     rotX -= 0.05;
    else if (keyCode == LEFT)
     rotY -= 0.05;
    else if (keyCode == RIGHT)
     rotY += 0.05;
  }
}



HTH.
Re: Creating a solid tetrahedron
Reply #3 - Mar 19th, 2010, 11:10pm
 
I used OCD lib for the cam (http://www.gdsstudios.com/processing/libraries/ocd/)

The tetrahedron's cartesian coordinates are:

   ( 1,  1,  1)
   (−1, −1,  1)
   (−1,  1, −1)
   ( 1, −1, −1)


see: http://en.wikipedia.org/wiki/Tetrahedron

Code:
import damkjer.ocd.*;
import processing.opengl.*;

Camera cam;
float vSize;

void setup(){
 size(720, 480, OPENGL);
 cam = new Camera(this, 0, 0, 80);
 vSize = 20;  
}

void draw(){
 background(0);
 
 lights();
 
 cam.circle(0.02);  
 cam.feed();

 pushMatrix();
 beginShape(TRIANGLE_STRIP);
 vertex( vSize,  vSize,  vSize); // v1
 vertex(-vSize, -vSize,  vSize); // v2
 vertex(-vSize,  vSize, -vSize); // v3
 vertex( vSize, -vSize, -vSize); // v4
 
 vertex( vSize,  vSize,  vSize); //v1 again
 vertex(-vSize, -vSize,  vSize); //v2 again
 
 //NOTE THAT IF YOU WRITE IT IN "PURE" JOGL (OPENGL), YOU DON'T NEED TO PUT SAME VERTEX AGAIN.
 
 endShape();
 popMatrix();
 
}
Page Index Toggle Pages: 1