コード例 #1
0
//calculate x, y and z coordinates for the mesh vertices
//y is the height
void createMesh()
{
    //set Mesh points x and z will range from [-1,1]
    float xinc = 2/(float)MH;
    float zinc = 2/ (float)MW;
    float x = -1;
    float z = -1;
    for(int i = 0; i < MH; i++)
    {
        for(int j = 0; j < MW; j++)
        {

            Mesh[i][j][0] = x;
            Mesh[i][j][2] = z;
            Mesh[i][j][1] = ((cos(x * 3) / 10.0 + sin((z+.5) * 3) / 10.0 + rand() % 5 / 1050.0) * 3) - 0.1;
			x += xinc;
        }
        z+= zinc;
        x = -1;
    }
    //calculate normals
    GLfloat pt[4][3];
    for(int i = 0; i < MH-1; i++)
    {
        for(int j = 0; j < MW-1; j++)
        {
            pt[0][0] = Mesh[i][j][0];
            pt[0][1] = Mesh[i][j][1];
            pt[0][2] = Mesh[i][j][2];
            pt[1][0] = Mesh[i+1][j][0];
            pt[1][1] = Mesh[i+1][j][1];
            pt[1][2] = Mesh[i+1][j][2];
            pt[2][0] = Mesh[i+1][j+1][0];
            pt[2][1] = Mesh[i+1][j+1][1];
            pt[2][2] = Mesh[i+1][j+1][2];
            pt[3][0] = Mesh[i][j+1][0];
            pt[3][1] = Mesh[i][j+1][1];
            pt[3][2] = Mesh[i][j+1][2];
            Normals[i][j] = newell4(pt);
        }
    }
}
コード例 #2
0
void Mesh::makePrism( PolyLine P, float H)
{
//Make a prism of height H from a polyline
//See Hill, E.F. Jr. and Kelley, S., pp. 286-289.
// Depends on Point2 and PolyLine classes (see mesh.h).

int N = P.num;
numVerts = 2*N;

// number of vertices (private)
numNormals = N+2; // number of normal (private)
numFaces = N+2;

// number of faces (private)
pt = new Point3[2*N];

// array of 2N vertices
norm = new Vector3[N + 2]; // array of N+2 normals to faces
face = new Face[N + 2];

// array of N+2 faces
// make the vertex list
for(int i = 0; i < N; i++)
{
	pt[i].x = P.pt[i].x; pt[i].y = P.pt[i].y; pt[i].z = 0;
	pt[i+N].x = P.pt[i].x; pt[i+N].y = P.pt[i].y; pt[i+N].z = H;
}

// make the face list, and construct the normals list at the same time
int indx[4]; // each face has four vertices; indx[] points to them
int next;

// next vertex = j + 1 modulo N
Vector3 fn;

// normal to one face
int normindx = 0; // index to normals

for(int f = 0; f < N; f++) // define all vertical faces (and normals)
{
	//... define the indx vector containing the index to each point in counterclockwise
	indx[0] = f;
	indx[1] = (f + 1) % N;
	indx[2] = ((f + 1) % N) + N;
	indx[3] = f + N;
	//order from the outside looking in ...
	norm[normindx] = newell4(indx); // normal to one face
	face[f].nVerts = 4; // each vertical face has four vertices
	face[f].vert = new VertexID[4];
	for(int j = 0; j < 4; j++)
	// store the four vertex indices
	{
		// and four normal indices
		face[f].vert[j].vertIndex = indx[j];
		face[f].vert[j].normIndex = normindx;
	}
	normindx = normindx + 1;
}

// define the bottom face (trace vertices CCW from outside looking in)
indx[0] = 0; indx[1] = N-1; indx[2] = N-2; indx[3] = N-3; // CCW from 0
norm[normindx] = newell4(indx); // normal to bottom face
face[N].nVerts = N; // bottom face has N vertices
face[N].vert = new VertexID[N];

//... need a loop to store the N vertex indices and N normal indices ...
for(int k = 0; k < N; k++){
	face[N].vert[k].vertIndex = (N - 1) - k;
	face[N].vert[k].normIndex = normindx;
}

normindx = normindx + 1;

// define the top face
indx[0] = N; indx[1] = N+1 ; // take first four vertices (4th = 1st)
indx[2] = N+2; indx[3] = N; // to define normal to top face
norm[normindx] = newell4(indx); // normal to top face
face[N+1].nVerts = N; // top face has N vertices
face[N+1].vert = new VertexID[N];

//... need a loop to store the N vertex indices and N normal indices ...
for(int k = 0; k < N; k++){
	face[N + 1].vert[k].vertIndex = (N - 1) - k + N;
	face[N + 1].vert[k].normIndex = normindx;
}

}