void Mesh::Update(bool drawNormals)
{
	this->StartRecording();

	glBegin(GL_TRIANGLES);
		for( unsigned int i = 0; i < this->_triangles.size(); ++i )
		{
			Triangle* triangle = this->_triangles.at(i);
			if( triangle )
			{
				triangle->Execute(false, true, true);
			}
		}
	glEnd();

	if( drawNormals )
	{
		glColor4f(1.0f, 0.0f, 1.0f, 0.75f);
		glBegin(GL_LINES);
			for( unsigned int i = 0; i < this->_vertices.size(); ++i )
			{
				Vertex* vertex = this->_vertices.at(i);
				if( vertex )
				{
					glVertex3fv((float*)vertex->GetPosition());
					glVertex3fv((float*)(vertex->GetPosition()+vertex->GetNormal()));
				}
			}
		glEnd();
	}

	this->StopRecording();
}
Example #2
0
void Skin::Draw(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    for(Triangle* t : triangles){
        glBegin(GL_TRIANGLES);
        Vertex* v = updatedVertices[t->GetVertex1()];
        glNormal3f(v->GetNormal().x, v->GetNormal().y, v->GetNormal().z);
        glVertex3f(v->GetPosition().x, v->GetPosition().y, v->GetPosition().z);
        
        v = updatedVertices[t->GetVertex2()];
        glNormal3f(v->GetNormal().x, v->GetNormal().y, v->GetNormal().z);
        glVertex3f(v->GetPosition().x, v->GetPosition().y, v->GetPosition().z);
        
        v = updatedVertices[t->GetVertex3()];
        glNormal3f(v->GetNormal().x, v->GetNormal().y, v->GetNormal().z);
        glVertex3f(v->GetPosition().x, v->GetPosition().y, v->GetPosition().z);
        glEnd();
    }
}
Example #3
0
void Skin::Update(){
    
    std::vector<Matrix34 *> skinningMatrices = std::vector<Matrix34 *>(numJoints);
    std::vector<Joint *> joints = Skel->GetJoints();
    for(Joint* j : joints){

        int jointNum = j->GetJointNumber();
        Matrix34 world = j->GetWorldMatrix();
        Matrix34 binv = *bindings[jointNum];
        skinningMatrices[jointNum] = new Matrix34();
        skinningMatrices[jointNum]->Dot(world, binv);
    }

    
    for(int c = 0; c < numVerts; c++){
        Vertex* v = vertices[c];
        Vector3 vTemp = Vector3();
        Vector3 nTemp = Vector3();
        Vector3 vFinal = Vector3();
        Vector3 nFinal = Vector3();
        
        for(int i = 0; i < v->numAttachments; i++){
            int jointNumber = v->joints[i];
            float weight = v->skinweights[i];
            
            skinningMatrices[jointNumber]->Transform(v->GetPosition(), vTemp);
            vFinal += weight*vTemp;
            
            skinningMatrices[jointNumber]->Transform(v->GetNormal(), nTemp);
            nFinal += weight*vTemp;
        }
        
        updatedVertices[c]->SetPosition(vFinal);
        nFinal.Normalize();
        updatedVertices[c]->SetNormal(nFinal);
    }
}
// Interpolates between the given vertexs and sets the start and end values of each
// scanline it encounters on the way.
void Rasterizer::InterpolateScanline(ScanLine* scanlines, Vertex v1, Vertex v2)
{
	// Swap the vertexs round if we need to, to make sure
	// the first vertex is the one at the top of the screen.
	if (v2.GetY() < v1.GetY())
	{
		Vertex v3 = v2;
		v2 = v1;
		v1 = v3;
	}
	
	// Work out the difference between each vertex and the number of steps
	// we need to take to interpolate between each of the vertexs.
	float yDiff = (v2.GetY() - v1.GetY());
	float xDiff = (v2.GetX() - v1.GetX());
	float zDiff = (v2.GetPreTransformZ() - v1.GetPreTransformZ());
	float numOfPoints = v2.GetY() - v1.GetY();

	// Work out the difference in colour between each vertex.
	float redColorDiff = (float)(v2.GetColor().GetR() - v1.GetColor().GetR());
	float greenColorDiff = (float)(v2.GetColor().GetG() - v1.GetColor().GetG());
	float blueColorDiff = (float)(v2.GetColor().GetB() - v1.GetColor().GetB());

	// WOrk out the difference between the normals.
	float xNormalDiff = (float)(v2.GetNormal().GetX() - v1.GetNormal().GetX());
	float yNormalDiff = (float)(v2.GetNormal().GetY() - v1.GetNormal().GetY());
	float zNormalDiff = (float)(v2.GetNormal().GetZ() - v1.GetNormal().GetZ());
	
	// Work out the difference in uv coordinate between each vertex.
	UVCoordinate uvCoord1 = v1.GetUVCoordinate();
	UVCoordinate uvCoord2 = v2.GetUVCoordinate();

	uvCoord1.U = uvCoord1.U / v1.GetPreTransformZ();
	uvCoord1.V = uvCoord1.V / v1.GetPreTransformZ();
	uvCoord1.Z = 1.0f / v1.GetPreTransformZ();
	
	uvCoord2.U = uvCoord2.U / v2.GetPreTransformZ();
	uvCoord2.V = uvCoord2.V / v2.GetPreTransformZ();
	uvCoord2.Z = 1.0f / v2.GetPreTransformZ();

	float uCoordDiff = (uvCoord2.U - uvCoord1.U);
	float vCoordDiff = (uvCoord2.V - uvCoord1.V);
	float zCoordDiff = (uvCoord2.Z - uvCoord1.Z);

	// Go through each point in the interpolation and work out the 
	// colour and uv values for pixel along the way.
	for (int i = 0; i < numOfPoints; i++)
	{
		unsigned int scanline = (int)(v1.GetY() + i);
		if (scanline < 0 || scanline >= _height)
			continue;

		// Get the x axis value in our interpolation.
		float x = v1.GetX() + i * (xDiff / yDiff);

		// Work out the colour value of the current pixel.
		float red	= v1.GetColor().GetR() + ((redColorDiff		/ numOfPoints) * i);
		float green = v1.GetColor().GetG() + ((greenColorDiff	/ numOfPoints) * i);
		float blue	= v1.GetColor().GetB() + ((blueColorDiff	/ numOfPoints) * i);
		
		// Work out the normal value of the current pixel.
		float normalX = v1.GetNormal().GetX() + ((xNormalDiff	/ numOfPoints) * i);
		float normalY = v1.GetNormal().GetY() + ((yNormalDiff	/ numOfPoints) * i);
		float normalZ = v1.GetNormal().GetZ() + ((zNormalDiff	/ numOfPoints) * i);

		// Work out the normal value of the current pixel.
		float pixelX = v1.GetX() + ((xDiff	/ numOfPoints) * i);
		float pixelY = v1.GetY() + ((yDiff	/ numOfPoints) * i);
		float pixelZ = v1.GetPreTransformZ() + ((zDiff	/ numOfPoints) * i);

		// Work out the UV value of the current pixel.
		float u	= uvCoord1.U + ((uCoordDiff / numOfPoints) * i);
		float v	= uvCoord1.V + ((vCoordDiff / numOfPoints) * i); 
		float z	= uvCoord1.Z + ((zCoordDiff / numOfPoints) * i); 
		
		// If the x-value is below the scanline's start x-value, then 
		// set the scanlines start value to the current values.
		if (x < scanlines[scanline].xStart)
		{
			scanlines[scanline].xStart = x;
			scanlines[scanline].redStart = red;
			scanlines[scanline].greenStart = green;
			scanlines[scanline].blueStart = blue;
			scanlines[scanline].uStart = u;
			scanlines[scanline].vStart = v;
			scanlines[scanline].zStart = z;
			scanlines[scanline].xNormalStart = normalX;
			scanlines[scanline].yNormalStart = normalY;
			scanlines[scanline].zNormalStart = normalZ;
			scanlines[scanline].pixelXStart = pixelX;
			scanlines[scanline].pixelYStart = pixelY;
			scanlines[scanline].pixelZStart = pixelZ;
		}

		// If the x-value is above the scanline's end x-value, then 
		// set the scanlines end value to the current values.
		if (x > scanlines[scanline].xEnd)
		{
			scanlines[scanline].xEnd = x;
			scanlines[scanline].redEnd = red;
			scanlines[scanline].greenEnd = green;
			scanlines[scanline].blueEnd = blue;
			scanlines[scanline].uEnd = u;
			scanlines[scanline].vEnd = v;
			scanlines[scanline].zEnd = z;
			scanlines[scanline].xNormalEnd = normalX;
			scanlines[scanline].yNormalEnd = normalY;
			scanlines[scanline].zNormalEnd = normalZ;
			scanlines[scanline].pixelXEnd = pixelX;
			scanlines[scanline].pixelYEnd = pixelY;
			scanlines[scanline].pixelZEnd = pixelZ;
		}
	}
}