void Calc3DSNormals(obj3ds_type *p_object) { int i; p3d_type l_vect1,l_vect2,l_vect3,l_vect_b1,l_vect_b2,l_normal; //Some local vectors int l_connections_qty[MAX_VERTICES]; //Number of poligons around each vertex // Resetting vertices' normals... for( i=0; i<p_object->vertices_qty; ++i ) { p_object->normal[i].x = 0.0f; p_object->normal[i].y = 0.0f; p_object->normal[i].z = 0.0f; l_connections_qty[i]=0; } for (i=0; i<p_object->polygons_qty; i++) { l_vect1.x = p_object->vertex[p_object->polygon[i].a].x; l_vect1.y = p_object->vertex[p_object->polygon[i].a].y; l_vect1.z = p_object->vertex[p_object->polygon[i].a].z; l_vect2.x = p_object->vertex[p_object->polygon[i].b].x; l_vect2.y = p_object->vertex[p_object->polygon[i].b].y; l_vect2.z = p_object->vertex[p_object->polygon[i].b].z; l_vect3.x = p_object->vertex[p_object->polygon[i].c].x; l_vect3.y = p_object->vertex[p_object->polygon[i].c].y; l_vect3.z = p_object->vertex[p_object->polygon[i].c].z; // Polygon normal calculation VectCreate( &l_vect1, &l_vect2, &l_vect_b1 ); // Vector from the first vertex to the second one VectCreate ( &l_vect1, &l_vect3, &l_vect_b2 ); // Vector from the first vertex to the third one VectDotProduct( &l_vect_b1, &l_vect_b2, &l_normal ); // Dot product between the two vectors VectNormalize( &l_normal ); //Normalizing the resultant we obtain the polygon normal l_connections_qty[p_object->polygon[i].a]+=1; // For each vertex shared by this polygon we increase the number of connections l_connections_qty[p_object->polygon[i].b]+=1; l_connections_qty[p_object->polygon[i].c]+=1; p_object->normal[p_object->polygon[i].a].x+=l_normal.x; // For each vertex shared by this polygon we add the polygon normal p_object->normal[p_object->polygon[i].a].y+=l_normal.y; p_object->normal[p_object->polygon[i].a].z+=l_normal.z; p_object->normal[p_object->polygon[i].b].x+=l_normal.x; p_object->normal[p_object->polygon[i].b].y+=l_normal.y; p_object->normal[p_object->polygon[i].b].z+=l_normal.z; p_object->normal[p_object->polygon[i].c].x+=l_normal.x; p_object->normal[p_object->polygon[i].c].y+=l_normal.y; p_object->normal[p_object->polygon[i].c].z+=l_normal.z; } for (i=0; i<p_object->vertices_qty; i++) { if (l_connections_qty[i]>0) { p_object->normal[i].x /= l_connections_qty[i]; // Let's now average the polygons' normals to obtain the vertex normal! p_object->normal[i].y /= l_connections_qty[i]; p_object->normal[i].z /= l_connections_qty[i]; } } }
//Anzeigefunktionen ursprünglich void polygon::display() { //vor jeder anzeige, die normalen neu berechen: vertex v1 = VectCreate( this->points[0], this->points[1] ); vertex v2 = VectCreate( this->points[0], this->points[2] ); calculateNormal( v1, v2 ); //TEST: NORMALE ANZEIGEN // drawNormal(); //weil schön ist: MESH anzeigen drawMesh(); //normale angeben glNormal3d( this->normal.wx, this->normal.wy, this->normal.wz ); //textur festlegen glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, this->texture.xsize, this->texture.ysize, 0, GL_RGBA, GL_UNSIGNED_BYTE, this->texture.content ); //Aussehen der Textur festlegen // Farbe des Polygons festlegen glColor3d( this->v_RGBcolor->wx, this->v_RGBcolor->wy, this->v_RGBcolor->wz ); // glColor3d( this->helligkeit, this->helligkeit, this->helligkeit ); //Helligkeitswert finden //glBegin( GL_TRIANGLE_FAN ); glBegin( GL_POLYGON ); for( long i = 0; i < this->pointsCount; i++ ) { //texturCoordinaten in 2d // vertex texCoords = this->textureCoords[ i ]; // glTexCoord2d( texCoords.wx, texCoords.wy ); //position in 3D vertex pos = this->points[ i ]; glVertex3d( pos.wx, pos.wy, pos.wz ); } glEnd(); }
//funktion zum berechnen der normalen void polygon::calculateNormal( vertex p_vector1, vertex p_vector2 ) { //anwendung des Kreuzproduktes: this->normal.wx = (p_vector1.wy * p_vector2.wz) - (p_vector1.wz * p_vector2.wy); this->normal.wy = (p_vector1.wz * p_vector2.wx) - (p_vector1.wx * p_vector2.wz); this->normal.wz = (p_vector1.wx * p_vector2.wy) - (p_vector1.wy * p_vector2.wx); VectNormalize( this->normal ); vertex point_light( 10.0f, 0.0f, -1.0f); vertex point_zero( 0,0,0 ); vertex vector_light = VectCreate( point_zero, point_light ); this->helligkeit = VectScalarProduct( this->normal, vector_light ); }