-
June 27th, 2007, 10:19 AM
#1
Perspective Matrix Question
Hi everyone. I have used OpenGL for sometime now but I wanted to get my hands dirty and write the Hello World perspective based wireframe cube example from scratch. I know that my code is VERY inefficient however, I am writing it this way for my own understanding. Right now, my cube projects as an orthogonal based cube. What corrections should I make to my perspective matrix to view the cube as a perspective projection?
Code:
import java.applet.*;
import java.io.*;
import java.util.*;
import Jama.*;
import java.awt.*;
public class JTest extends Applet implements Runnable{
Matrix mTriangleValues;
Matrix scaleMatrix;
Matrix rotateMatrix;
Matrix cameraMatrix;
Matrix perspectiveMatrix;
Vector3D points[] = new Vector3D[36];
double cameraValues[][] = {{3,0,0,0},{0,3,0,0},{0,0,3,0},{100,100,0,1}};
int n = 1;
int f = 100;
int t = 1;
int b = getHeight();
int l = 1;
int r = getWidth();
/*
double perspectiveValues[][] =
{{(2*n)/(r-l),0,-(r+l)/(r-l),0},
{0,(2*n)/(b-t),-(b+t)/(b-t),0},
{0,0,(f+n)/(f-n),(-2*(f*n))/(f-n)},
{0,0,1,0}};
*/
double perspectiveValues[][] =
{{1/Math.tan(45),0,0,0},
{0,1/Math.tan(45),0,0},
{0,0,(100+1)/(100-1),-2*(100*1)/(100-1)},
{0,0,1,0}};
Image imgOffscreen;
Graphics gphcBuffered;
double angle = 0.0f;
boolean bMainThreadRunning = true;
Thread thrdMain = null;
int iFPS = 0;
float x,y,z = 0.0f;
public void init()
{
this.setSize(300, 300);
scaleMatrix = makeScaleMatrix(80);
perspectiveMatrix = new Matrix(perspectiveValues);
for(int i=0; i<points.length; i++)
{
points[i] = new Vector3D();
}
// Back
points[0].setPoints(-1,0,0);
points[1].setPoints(-1,1,0);
points[2].setPoints(0,0,0);
points[3].setPoints(-1,1,0);
points[4].setPoints(0,1,0);
points[5].setPoints(0,0,0);
// Front
points[6].setPoints(-1,0,1);
points[7].setPoints(-1,1,1);
points[8].setPoints(0,0,1);
points[9].setPoints(-1,1,1);
points[10].setPoints(0,1,1);
points[11].setPoints(0,0,1);
// Left
points[12].setPoints(-1,0,0);
points[13].setPoints(-1,1,0);
points[14].setPoints(-1,0,1);
points[15].setPoints(-1,1,0);
points[16].setPoints(-1,1,1);
points[17].setPoints(-1,0,1);
// Right
points[18].setPoints(0,0,0);
points[19].setPoints(0,1,0);
points[20].setPoints(0,0,1);
points[21].setPoints(0,1,0);
points[22].setPoints(0,1,1);
points[23].setPoints(0,0,1);
// Top
points[24].setPoints(-1,1,0);
points[25].setPoints(-1,1,1);
points[26].setPoints(0,1,0);
points[27].setPoints(-1,1,1);
points[28].setPoints(0,1,1);
points[29].setPoints(0,1,0);
// Bottom
points[30].setPoints(-1,0,0);
points[31].setPoints(-1,0,1);
points[32].setPoints(0,0,0);
points[33].setPoints(-1,0,1);
points[34].setPoints(0,0,1);
points[35].setPoints(0,0,0);
imgOffscreen = createImage(this.getWidth(), this.getHeight());
gphcBuffered = imgOffscreen.getGraphics();
thrdMain = new Thread(this);
thrdMain.start();
}
public void destroy()
{
bMainThreadRunning = false;
thrdMain = null;
}
public void run()
{
while(bMainThreadRunning)
{
this.repaint();
try
{
thrdMain.sleep(20);
}
catch(Exception e)
{}
}
}
public boolean keyDown(Event evt, int key)
{
switch(key)
{
case Event.LEFT:
angle -= 2;
break;
case Event.RIGHT:
angle += 2;
break;
case Event.UP:
z -= 2;
break;
case Event.DOWN:
z += 2;
break;
}
return true;
}
public void paint(Graphics g)
{
//angle+= 1;
scaleMatrix = makeScaleMatrix(50); //10+Math.sin(angle/50)*50);
cameraMatrix = makeCameraMatrix();
Matrix calcMatrix = scaleMatrix.times(makeYRotateMatrix(angle)).times(cameraMatrix).times(perspectiveMatrix);
gphcBuffered.clearRect(0, 0, this.getWidth(), this.getHeight());
gphcBuffered.setColor(Color.BLACK);
gphcBuffered.drawString("Hello World!", 10, 10);
//mTriangleValues.
Polygon polygon = new Polygon();
int i=0;
for(i=0; i<36; i++)
{
Matrix tempMatrix1 = points[i].getMatrix().times(calcMatrix);
polygon.addPoint((int)tempMatrix1.get(0, 0), (int)tempMatrix1.get(0, 1));
if((i+1) % 6 == 0)
{
polygon = combinePolygons(polygon);
int iColor = (int)Math.random()%100;
iColor = iColor >= 100 ? iColor : 1000;
gphcBuffered.setColor(Color.BLUE);
gphcBuffered.drawPolygon(polygon);
polygon = new Polygon();
}
}
g.drawImage(imgOffscreen,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public Matrix makeZRotateMatrix(double angle)
{
double rotateValues[][] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
rotateValues[0][0] = Math.cos (((angle * Math.PI)/180));
rotateValues[0][1] = Math.sin(((angle * Math.PI)/180));
rotateValues[1][0] = -Math.sin(((angle * Math.PI)/180));
rotateValues[1][1] = Math.cos(((angle * Math.PI)/180));
return new Matrix(rotateValues);
}
public Matrix makeYRotateMatrix(double angle)
{
double rotateValues[][] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
rotateValues[0][0] = Math.cos (((angle * Math.PI)/180));
rotateValues[0][2] = -Math.sin(((angle * Math.PI)/180));
rotateValues[2][0] = Math.sin(((angle * Math.PI)/180));
rotateValues[2][2] = Math.cos(((angle * Math.PI)/180));
return new Matrix(rotateValues);
}
public Matrix makeXRotateMatrix(double angle)
{
double rotateValues[][] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
rotateValues[1][1] = Math.cos (((angle * Math.PI)/180));
rotateValues[1][2] = Math.sin(((angle * Math.PI)/180));
rotateValues[2][1] = -Math.sin(((angle * Math.PI)/180));
rotateValues[2][2] = Math.cos(((angle * Math.PI)/180));
return new Matrix(rotateValues);
}
public Matrix makeScaleMatrix(double scale)
{
double scaleValues[][] = {{scale,0,0,0},{0,scale,0,0},{0,0,scale,0},{0,0,0,1}};
return new Matrix(scaleValues);
}
public Matrix makeCameraMatrix()
{
return new Matrix(cameraValues);
}
public Matrix makeTranslateMatrix(float x, float y, float z)
{
double translateValues[][] = {{1,0,0,0},{0,1,0,0},{0,0,1,0}, {x,y,z,1}};
return new Matrix(translateValues);
}
public Polygon combinePolygons(Polygon p1)
{
Polygon p = new Polygon();
p.addPoint(p1.xpoints[0], p1.ypoints[0]);
p.addPoint(p1.xpoints[1], p1.ypoints[1]);
p.addPoint(p1.xpoints[4], p1.ypoints[4]);
p.addPoint(p1.xpoints[5], p1.ypoints[5]);
return p;
}
}
class Vector3D
{
public double dX,dY,dZ,dS;
public Vector3D()
{
dX = dY = dZ = 0.0f;
dS = 1.0f;
}
public void setPoints(double x, double y, double z)
{
dX = x;
dY = y;
dZ = z;
dS = 1.0f;
}
public void setPoints(double[] points)
{
dX = points[0];
dY = points[1];
dZ = points[2];
dS = 1.0f;
}
public Matrix getMatrix()
{
double point[] = {dX,dY,dZ};
Matrix m = new Matrix(1,4);
m.set(0, 0, dX);
m.set(0, 1, dY);
m.set(0, 2, dZ);
m.set(0, 3, dS);
return m;
}
}
Thanks in advance!
-- Brandon Fogerty
Http://www.brandonfogerty.com
"Not believing doesn't make something not true"
May GOD Bless, Strengthen, Guide, Protect, and Reveal Wisdom to you Always!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|