daimous

12-12-2009, 03:14 AM

Hi Guys hel please.. I'm new to open gl programming and my prof provide this code but im having hardtime in understanding it..Can any body please help me or at least give me idea on what subdivideCircle, normalize, and drawtriangle functions is trying to do? Thanks ina advance!

// subdivide a triangle recursively, and draw them

private void subdivideCircle(int radius, float[] v1, float[] v2, int depth)

{

float v11[] = new float[3];

float v22[] = new float[3];

float v00[] = {0, 0, 0};

float v12[] = new float[3];

if (depth==0)

{

//5. specify a color related to triangle location

gl.glColor3f(v1[0]*v1[0], v1[1]*v1[1], v1[2]*v1[2]);

for (int i = 0; i<3; i++)

{

v11[i] = v1[i]*radius;

v22[i] = v2[i]*radius;

}

drawtriangle(v11, v22, v00);

return;

}

v12[0] = v1[0]+v2[0];

v12[1] = v1[1]+v2[1];

v12[2] = v1[2]+v2[2];

normalize(v12);

// subdivide a triangle recursively, and draw them

subdivideCircle(radius, v1, v12, depth-1);

subdivideCircle(radius, v12, v2, depth-1);

}

// draw a circle with center at the origin in xy plane

public void drawCircle(int cRadius, int depth)

{

subdivideCircle(cRadius, cVdata[0], cVdata[1], depth);

subdivideCircle(cRadius, cVdata[1], cVdata[2], depth);

subdivideCircle(cRadius, cVdata[2], cVdata[3], depth);

subdivideCircle(cRadius, cVdata[3], cVdata[0], depth);

}

// normalize a 3D vector

public void normalize(float vector[])

{

float d = (float)Math.sqrt(vector[0]*vector[0]

+vector[1]*vector[1] + vector[2]*vector[2]);

if (d==0)

{

System.out.println("0 length vector: normalize().");

return;

}

vector[0] /= d;

vector[1] /= d;

vector[2] /= d;

}

public void drawtriangle(float[] v1, float[] v2, float[] v3)

{

gl.glBegin(GL.GL_TRIANGLES);

gl.glVertex3fv(v1);

gl.glVertex3fv(v2);

gl.glVertex3fv(v3);

gl.glEnd();

}

// subdivide a triangle recursively, and draw them

private void subdivideCircle(int radius, float[] v1, float[] v2, int depth)

{

float v11[] = new float[3];

float v22[] = new float[3];

float v00[] = {0, 0, 0};

float v12[] = new float[3];

if (depth==0)

{

//5. specify a color related to triangle location

gl.glColor3f(v1[0]*v1[0], v1[1]*v1[1], v1[2]*v1[2]);

for (int i = 0; i<3; i++)

{

v11[i] = v1[i]*radius;

v22[i] = v2[i]*radius;

}

drawtriangle(v11, v22, v00);

return;

}

v12[0] = v1[0]+v2[0];

v12[1] = v1[1]+v2[1];

v12[2] = v1[2]+v2[2];

normalize(v12);

// subdivide a triangle recursively, and draw them

subdivideCircle(radius, v1, v12, depth-1);

subdivideCircle(radius, v12, v2, depth-1);

}

// draw a circle with center at the origin in xy plane

public void drawCircle(int cRadius, int depth)

{

subdivideCircle(cRadius, cVdata[0], cVdata[1], depth);

subdivideCircle(cRadius, cVdata[1], cVdata[2], depth);

subdivideCircle(cRadius, cVdata[2], cVdata[3], depth);

subdivideCircle(cRadius, cVdata[3], cVdata[0], depth);

}

// normalize a 3D vector

public void normalize(float vector[])

{

float d = (float)Math.sqrt(vector[0]*vector[0]

+vector[1]*vector[1] + vector[2]*vector[2]);

if (d==0)

{

System.out.println("0 length vector: normalize().");

return;

}

vector[0] /= d;

vector[1] /= d;

vector[2] /= d;

}

public void drawtriangle(float[] v1, float[] v2, float[] v3)

{

gl.glBegin(GL.GL_TRIANGLES);

gl.glVertex3fv(v1);

gl.glVertex3fv(v2);

gl.glVertex3fv(v3);

gl.glEnd();

}