Tuesday, May 5, 2009

Project 3: Boids


For project 3, I decided to implement the 3-d boids algorithm. This allows objects to simulate natural flocking behavior using the 3 rules of alignment, cohesion, and separation. My boids represent fish in a fish tank that travel as a school. I also introduced a predator to the tank which the "fish" avoid. I could not get the obj loader library from processing to work, so they are representsed as cubes for now. Here is a screen shot.

Project 2: OBJ Viewer

For project 2, we had to implement a program that would read and display an OBJ file and allow the user to control the camera using OpenGL and C++. Here are some screen shots:































Thursday, March 26, 2009

Wednesday, March 25, 2009

First OpenGL Assignment

After reading the articles posted, I love the way that OpenGL takes advantage of the graphics card by directly accessing its memory and, therefore saving the processor a good amount of work. While this may not be evident in the smaller assignments and projects that we are currently doing, any way to relieve the processor and speed up a program is certainly going to help if programming a game that must use search trees for its AI or any other very time consuming operation. Another great thing about OpenGL seems to be its versatility. It seems rather rare in my limited experience to find a library that can be used in a variety of languages as OpenGL is. Python is hopefully the language that I plan on learning next so I am somewhat excited that what I learn about OpenGL I will be able to use in Python as well. The fact that OpenGL ES can be used for mobile devices further exhibits this versatility, making it a very useful library to know since it can be used in so many platforms. From what I have read, it also appears that many of the commands are relatively straightforward and easy to use. All of this combined has made me excited to learn more.

The second part of this assignment was to create a Bezier curve in OpenGL. The curve rotates when you hit the arrow button.
















Finally, I created a multi-colored 3-d cube. This also rotates when the arrows are pushed. I plan on expanding this tomorrow, but I have this so far so might as well post it.











The basic outline of a house using 3d OpenGGL commands.

Friday, March 6, 2009

Compiling and Running glut on my PC

Here is the simple.cpp program running on my personal computer. (Proof I can get it to work I guess)

Thanks to Michael for the easy to follow step by step instructions.

Project 1: Circles


The purpose of project 1 was to replicate an image by Anthony Mattox that included circles placed within a circular border. Next we had to come up with a variation of that image of our own. The first 3 images are replications, followed by a variety of variations made by creating different color or saturation functions that changed based on the circles position in the image. It took a few days to get this posted due to trying to upload .pdfs of the images with no success, but here they are as .jpegs.







































































































import processing.pdf.*;

float borderDiam=700;
int numCircs=1000000;//change back to at least 1000000
boolean overlap;
int count=0;
Circ[] balls = new Circ[numCircs];
int diameter;
void setup(){
background(255);

size(700,700);
noLoop();
beginRecord(PDF, "T2BRainbow1.pdf");
// stroke(100);
smooth();
//ellipse(350,350,700,700);
noStroke();
int count=0;

for (int i=0;i float x=random(borderDiam);
float y=random(borderDiam);
float diameter=(random(borderDiam/10)+10);
Circ temp=new Circ(x,y,diameter);
if (inBorder(x,y,diameter)&&collide(x,y,diameter,count)){
balls[count]=temp;
count++;
}
}
for (int i=count;i float x=random(borderDiam);
float y=random(borderDiam);
float diameter=(random(borderDiam/20)+10);
Circ temp=new Circ(x,y,diameter);
if (inBorder(x,y,diameter)&&collide(x,y,diameter,count)){
balls[count]=temp;
count++;
}
}
for (int i=count;i float x=random(borderDiam);
float y=random(borderDiam);
float diameter=(random(borderDiam/30)+5);
Circ temp=new Circ(x,y,diameter);
if (inBorder(x,y,diameter)&&collide(x,y,diameter,count)){
balls[count]=temp;
count++;
}
}
for (int i=count;i float x=random(borderDiam);
float y=random(borderDiam);
float diameter=(random(borderDiam/30)+2);
Circ temp=new Circ(x,y,diameter);
if (inBorder(x,y,diameter)&&collide(x,y,diameter,count)){
balls[count]=temp;
count++;
}
}

System.out.println(count);
for (int i = 0; i < count; i++){
balls[i].display();
}
endRecord();

}
void draw(){

}
boolean inBorder(float x, float y, float diameter){
float distToCenter=dist(x,y,borderDiam/2,borderDiam/2);
float distToEdge= distToCenter+diameter/2;
if (distToEdge>borderDiam/2){
return false;
}
else return true;


}
boolean collide(float x, float y, float d, int numCircles){
int diameter=(int)d;
for (int i = 0; i < numCircles; i++){
float distance = dist(x, y, balls[i].x, balls[i].y);
float minDist = balls[i].diameter/2 + diameter/2;
if (distance < minDist){
return false;
}

}
return true;
}

class Circ{
float x, y;
float diameter;
int id;

boolean display = true;
boolean inBorder=true;
float col;
float sat;
float bright;
float gray;
Circ(float xin, float yin, float diameter){
x = xin;
y = yin;
this.diameter = diameter;
//blue 195
//cool green 145
//pink 300
//red 340
//yellow green 60
//0 red orange
//for a rainbow coloring
//col=((dist(borderDiam/2,borderDiam/2,x,y))/(borderDiam/720));
//col= ((borderDiam/2-dist(x,y,borderDiam/2,borderDiam/2))/(borderDiam/720));
//col=+(random(20));
col=y/(borderDiam/360)+random(40);
sat=((dist(borderDiam/2,borderDiam/2,x,y))/(borderDiam/200));
bright=((borderDiam/2-dist(x,y,borderDiam/2,borderDiam/2))/(borderDiam/100));
gray=(borderDiam/2-dist(x,y,borderDiam/2,borderDiam/2))/1.2;

}

void display(){
colorMode(HSB,360,100,100);
fill(col,sat,100);
//fill(gray);
ellipse(x, y, diameter, diameter);
}
}









Tuesday, February 24, 2009

Rotating Triangles

The final part of this assignment was to use the pushMatrix() and popMatrix() commands along with shape translations to create a design.











void setup(){
size(720, 480);
background(0);
stroke(255);
//top
fill(230,245,255);
triangle(0,0,720/4,0,720/2,480/2);
fill(175,225,255);
triangle(720/2,0,720/4,0,720/2,480/2);
fill(80,195,255);
triangle(720/2,0,(720/4)*3,0,720/2,480/2);
fill(20,175,255);
triangle(720,0,(720/4)*3,0,720/2,480/2);
//bottom
fill(5,5,5);
triangle(0,480,720/4,480,720/2,480/2);
fill(30,40,45);
triangle(720/2,480,720/4,480,720/2,480/2);
fill(30,60,70);
triangle(720/2,480,(720/4)*3,480,720/2,480/2);
fill(25,65,85);
triangle(720,480,(720/4)*3,480,720/2,480/2);
//right--need to change stroke
fill(25,70,100);
triangle(720,0,720,480/4,720/2,480/2);
triangle(720,480/2,720,480/4,720/2,480/2);
triangle(720,480/2,720,(480/4)*3,720/2,480/2);
triangle(720,480,720,(480/4)*3,720/2,480/2);
//left--need to change stroke
triangle(0,0,0,480/4,720/2,480/2);
triangle(0,480/2,0,480/4,720/2,480/2);
triangle(0,480/2,0,(480/4)*3,720/2,480/2);
triangle(0,480,0,(480/4)*3,720/2,480/2);
pushMatrix();
for (int i=0;i<30;i++){
pushMatrix();
fill(random(100),random(200),255);
translate(random(150),random(100));
scale(.9);
ellipse(2,2,100,100);
}
for (int i=0;i<30;i++)
popMatrix();
pushMatrix();
translate(750,0);
for (int i=0;i<30;i++){
pushMatrix();
fill(random(100),random(200),255);
translate(random(150)-150,random(100));
scale(.9);
ellipse(2,2,80,80);
}
for (int i=0;i<30;i++)
popMatrix();
popMatrix();
translate(720/2,480/2);
for (int i=0;i<10;i++){


translate(2,2);
scale(1.4);
fill((i+1)*25);
rect(0,0,10,10);
rotate(PI/30);
pushMatrix();



}
for (int i=0;i<10;i++){
popMatrix();
}

translate(-10,-10);
for (int j=0;j<10;j++){


translate(-2,-6);
scale(1.3);
fill(20,random(255),255);
rect(0,0,10,10);

stroke(random(255));
scale(1.3);
fill(random(255),255,255);
translate(-2,-6);
ellipse(5,5,10,10);
rotate(PI/30);
pushMatrix();



}
for (int i=0;i<10;i++)
popMatrix();
translate(0,-20);


}
void draw(){}




The second part of assignment 4 is to use our rotating triangles function to create an interesting image using only rotating triangles.











void setup()
{
size(720, 480);
background(0);
stroke(255);
int x1=0;
int x2=0;
int x3=20;
int y1=0;
int y2=20;
int y3=0;
int x4=720;
int y4=480;
int x5=720;
int y5=460;
int x6=700;
int y6=480;
int x11=0;
int x12=0;
int x13=20;
int y11=480;
int y12=460;
int y13=480;
int x14=720;
int y14=0;
int x15=720;
int y15=20;
int x16=700;
int y16=0;
int r1=255;
int g1=255;
int b1=255;
int r2=0;
int g2=0;
int b2=0;
stroke(r1,g1,b1);
int ang1=0;
int ang2=0;
myTriangle(x1,y2,x2,y2,x3,y3);
for (int i=0;i<300;i++){
ang1=ang1+20;
ang2=ang2-20;
x1=x1+3;
y1=y1+2;
x2=x2+3;
y2=y2+2;
x3=x3+3;
y3=y3+2;
x4=x4+3;
x5=x5+3;
x6=x6+3;
y4=y4+2;
y5=y5+2;
y6=y6+2;
x11=x11-3;
y11=y11-2;
x12=x12-3;
y12=y12-2;
x13=x13-3;
y13=y13-2;
x14=x14-3;
x15=x15-3;
x16=x16-3;
y14=y14-2;
y15=y15-2;
y16=y16-2;
r1=r1-2;
g1=g1-3;
b1=b1-4;
r2=r2+4;
g2=g2+3;
b2=b2+2;
stroke(r1,g1,b1);
Point p1=new Point(x1, y1);
Point p2=new Point(x2 ,y2);

Point p3=new Point(x3 ,y3);
rotateTri(p1,p2,p3,ang1);
stroke(b1,g1,r1);
Point p4=new Point(x4, y4);
Point p5=new Point(x5 ,y5);

Point p6=new Point(x6 ,y6);
rotateTri(p3,p4,p5,ang2);
stroke(r2,g2,b2);
Point p11=new Point(x11, y11);
Point p12=new Point(x12 ,y12);

Point p13=new Point(x13 ,y13);
//rotateTri(p11,p12,p13,ang1);
stroke(b2,g2,r2);
Point p14=new Point(x14, y14);
Point p15=new Point(x15 ,y15);

Point p16=new Point(x16 ,y16);
rotateTri(p16,p14,p15,ang2);
}
}
void draw(){}
void myTriangle(int x1,int y1,int x2, int y2, int x3, int y3){
line(x1,y1, x2,y2);
line(x2,y2, x3,y3);
line(x3,y3,x1,y1);
}
void rotateTri(Point p1, Point p2, Point p3, float angle){
//draw original triangle

float rad=radians(angle);
Point center=new Point(((p1.x+p2.x+p3.x)/3),((p1.y+p2.y+p3.y)/3));
Point temp1=new Point(p1.x-center.x, p1.y-center.y);
Point temp2=new Point(p2.x-center.x, p2.y-center.y);
Point temp3=new Point(p3.x-center.x, p3.y-center.y);
p1=rotatePnt(temp1, rad);
p2=rotatePnt(temp2, rad);
p3=rotatePnt(temp3, rad);
p1=new Point(p1.x+center.x, p1.y+center.y);
p2=new Point(p2.x+center.x, p2.y+center.y);
p3=new Point(p3.x+center.x, p3.y+center.y);

myTriangle(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
}
Point rotatePnt(Point pnt, float angle)
{
float[][] homoCoord = new float[1][3];
float[][] homoCoord2= new float[1][3];
float[][] transformMat= new float[3][3];
float cosine=cos(angle);
float sine=sin(angle);
Point temp=new Point();


homoCoord[0][0]=pnt.x;
homoCoord[0][1]=pnt.y;
homoCoord[0][2]=1;

transformMat[0][0]=cosine;
transformMat[0][1]=sine;
transformMat[0][2]=0;
transformMat[1][0]=-sine;
transformMat[1][1]=cosine;
transformMat[1][2]=0;
transformMat[2][0]=0;
transformMat[2][1]=0;
transformMat[2][2]=1;

homoCoord2=multMat(homoCoord, transformMat);
temp.x=(int)homoCoord2[0][0];
temp.y=(int)homoCoord2[0][1];
return temp;

}
float[][] multMat(float[][] m1,float[][] m2){
float[][]newMatrix=new float[1][3];
for(int i = 0; i < 1; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++){
newMatrix[i][k]+=m1[i][j]*m2[j][k];
}
}
}
return newMatrix;
}




This assignment involved writing a function in processing that takes in the 3 points of a triangle and its angle of rotation and draws both the original triangle and the triangle rotated by the angle parameter. In this example, the original triangle (light gray) is rotated by 90 degrees (white).








void setup()
{
size(720, 480);
background(0);
stroke(255);
Point p1=new Point(100,240);
Point p2=new Point(500,240);
Point p3=new Point(300,100);
rotateTri(p1,p2,p3,90);
}
}
void draw(){}
void myTriangle(int x1,int y1,int x2, int y2, int x3, int y3){
line(x1,y1, x2,y2);
line(x2,y2, x3,y3);
line(x3,y3,x1,y1);
}
void rotateTri(Point p1, Point p2, Point p3, float angle){
//draw original triangle
myTriangle(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
float rad=radians(angle);
Point center=new Point(((p1.x+p2.x+p3.x)/3),((p1.y+p2.y+p3.y)/3));
Point temp1=new Point(p1.x-center.x, p1.y-center.y);
Point temp2=new Point(p2.x-center.x, p2.y-center.y);
Point temp3=new Point(p3.x-center.x, p3.y-center.y);
p1=rotatePnt(temp1, rad);
p2=rotatePnt(temp2, rad);
p3=rotatePnt(temp3, rad);
p1=new Point(p1.x+center.x, p1.y+center.y);
p2=new Point(p2.x+center.x, p2.y+center.y);
p3=new Point(p3.x+center.x, p3.y+center.y);

myTriangle(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
}
Point rotatePnt(Point pnt, float angle)
{
float[][] homoCoord = new float[1][3];
float[][] homoCoord2= new float[1][3];
float[][] transformMat= new float[3][3];
float cosine=cos(angle);
float sine=sin(angle);
Point temp=new Point();


homoCoord[0][0]=pnt.x;
homoCoord[0][1]=pnt.y;
homoCoord[0][2]=1;

transformMat[0][0]=cosine;
transformMat[0][1]=sine;
transformMat[0][2]=0;
transformMat[1][0]=-sine;
transformMat[1][1]=cosine;
transformMat[1][2]=0;
transformMat[2][0]=0;
transformMat[2][1]=0;
transformMat[2][2]=1;

homoCoord2=multMat(homoCoord, transformMat);
temp.x=(int)homoCoord2[0][0];
temp.y=(int)homoCoord2[0][1];
return temp;

}
float[][] multMat(float[][] m1,float[][] m2){
float[][]newMatrix=new float[1][3];
for(int i = 0; i < 1; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++){
newMatrix[i][k]+=m1[i][j]*m2[j][k];
}
}
}
return newMatrix;
}