void computeNormal(float *origin, float *d1, float *d2, float *normal){ float vector1[3], vector2[3]; for(int i =0; i < 3; i++){ vector1[i] = d1[i] - origin[i]; vector2[i] = d2[i] - origin[i]; } normCrossProd(d1, d2, normal); }
void findNormal(GLfloat v[3], GLfloat v1[3], GLfloat v2[3], GLfloat out[3]) { if (v[1] == 0) { out[0] = 0; out[1] = 1.0; out[2] = 0; return; } GLfloat vect1[3] = {v1[0]-v[0], v1[1]-v[1], v1[2]-v[2]}; GLfloat vect2[3] = {v2[0]-v[0], v2[1]-v[1], v2[2]-v[2]}; normCrossProd(vect1, vect2, out); }
/* Draw a cylinder with height "h" and "c" segments and "s" size * The bottom is not drawn because it's not visible */ void almostSolidCylinder(float s, int c, float h) { GLfloat normal[3]; int i; float angle, step = 2.0f*M_PI/(float)c; GLfloat p1[3], p2[3], p3[3], p4[3], v1[3], v2[3]; /* Draw top */ glBegin(GL_POLYGON); glNormal3f(0.0f, 0.0f, 1.0f); for (i=0, angle=0.0; i<c; i++, angle+=step) glVertex3f(cos(angle)*s, sin(angle)*s, 0.0f); glEnd(); /* Draw stick */ glBegin(GL_QUADS); for (i=0, angle=0.0; i<c; i++, angle+=step) { p1[0] = p2[0] = cos(angle)*s; p1[1] = p2[1] = sin(angle)*s; p1[2] = 0.0f; p2[2] = h; p3[0] = p4[0] = cos(angle+step)*s; p3[1] = p4[1] = sin(angle+step)*s; p3[2] = 0.0f; p4[2] = h; /* Do the normal vector */ v1[0] = p1[0] - p3[0]; v1[1] = p1[1] - p3[1]; v1[2] = p1[2] - p3[2]; v2[0] = p2[0] - p3[0]; v2[1] = p2[1] - p3[1]; v2[2] = p2[2] - p3[2]; normCrossProd(v1, v2, normal); glNormal3fv(normal); /* Here's the vertexes */ glVertex3f(p2[0], p2[1], p2[2]); glVertex3f(p4[0], p4[1], p4[2]); glVertex3f(p3[0], p3[1], p3[2]); glVertex3f(p1[0], p1[1], p1[2]); } glEnd(); }
/* Since GL_QUAD is called prior to this method, it simply list the corners of the square */ void square(GLfloat va[3], GLfloat vb[3], GLfloat vc[3], GLfloat vd[3]) { /* The following lines of code are used to create a normal vector. This is mainly used to ensure that the object will have a normal for proper shading */ float v1[3], v2[3], normal[3]; v1[0] = vc[0] - va[0]; v1[1] = vc[1] - va[1]; v1[2] = vc[2] - va[2]; v2[0] = vc[0] - vb[0]; v2[1] = vc[1] - vb[1]; v2[2] = vc[2] - vb[2]; normCrossProd(v1, v2, normal); glNormal3fv(normal); glVertex3fv(va); glVertex3fv(vb); glVertex3fv(vc); glVertex3fv(vd); }