Vector3D Vector3D::Cross(const Vector3D left, const Vector3D right) { Vector3D Value; Value.SetX(left.GetY() * right.GetZ() - left.GetZ() * right.GetY()); Value.SetY(left.GetZ() * right.GetX() - left.GetX() * right.GetZ()); Value.SetZ(left.GetX() * right.GetY() - left.GetY() * right.GetX()); return Value; }
Matrix3x3::Matrix3x3(Vector3D Row1, Vector3D Row2, Vector3D Row3) { m_dValues[0][0] = Row1.GetX(); m_dValues[0][1] = Row1.GetY(); m_dValues[0][2] = Row1.GetZ(); m_dValues[1][0] = Row2.GetY(); m_dValues[1][1] = Row2.GetY(); m_dValues[1][2] = Row2.GetZ(); m_dValues[2][0] = Row3.GetX(); m_dValues[2][1] = Row3.GetY(); m_dValues[2][2] = Row3.GetZ(); }
void Matrix3x3::SetRowThree(const Vector3D& row) { _indicies[6] = row.GetX(); _indicies[7] = row.GetY(); _indicies[8] = row.GetZ(); }
void Matrix3x3::SetRowTwo(const Vector3D& row) { _indicies[3] = row.GetX(); _indicies[4] = row.GetY(); _indicies[5] = row.GetZ(); }
void Matrix3x3::SetRowOne(const Vector3D& row) { _indicies[0] = row.GetX(); _indicies[1] = row.GetY(); _indicies[2] = row.GetZ(); }
Matrix3x3::Matrix3x3(const Vector3D& row_one, const Vector3D& row_two, const Vector3D& row_three) : _indicies() { _indicies[0] = row_one.GetX(); _indicies[1] = row_one.GetY(); _indicies[2] = row_one.GetZ(); _indicies[3] = row_two.GetX(); _indicies[4] = row_two.GetY(); _indicies[5] = row_two.GetZ(); _indicies[6] = row_three.GetX(); _indicies[7] = row_three.GetY(); _indicies[8] = row_three.GetZ(); }
void glTranslatev(const Vector3D& v) { glTranslated(v.GetX(), v.GetY(), v.GetZ()); }
void Matrix3x3::SetColumn(unsigned int uiColumn, Vector3D Value) { m_dValues[0][uiColumn] = Value.GetX(); m_dValues[1][uiColumn] = Value.GetY(); m_dValues[2][uiColumn] = Value.GetZ(); }
Rect Rect::operator - (const Vector3D& v) const { return Rect(left - v.GetX(), top - v.GetY(), right - v.GetX(), bottom - v.GetY()); }
Rect Rect::operator + (const Vector3D& v) const { return Rect(left + v.GetX(), top + v.GetY(), right + v.GetX(), bottom + v.GetY()); }
Vector3D Vector3D::operator - ( Vector3D _v ) const { return Vector3D( m_x - _v.GetX(), m_y - _v.GetY(), m_z - _v.GetZ() ); }
bool Vector3D::operator == ( Vector3D _v ) const { return m_x == _v.GetX() && m_y == _v.GetY() && m_z == _v.GetZ(); }
double Vector3D::operator * ( Vector3D _v ) const { return m_x * _v.GetX() + m_y * _v.GetY() + m_z * _v.GetZ(); }
void gluLookAt(const Vector3D& eye, const Vector3D& center, const Vector3D& up) { gluLookAt(eye.GetX(), eye.GetY(), eye.GetZ(), center.GetX(), center.GetY(), center.GetZ(), up.GetX(), up.GetY(), up.GetZ()); }
// Fills a polygon using a texture, gouraud shading and a normal map given 3 points and a color. void Rasterizer::FillPolygonTexturedNormalMapped(Vertex v1, Vertex v2, Vertex v3, Gdiplus::Color color, Model3D& model, std::vector<DirectionalLight*> directionalLights, std::vector<AmbientLight*> ambientLights, std::vector<PointLight*> pointLights) { ScanLine* _scanlines = new ScanLine[_height]; BYTE* texture; Gdiplus::Color* palette; BYTE* normalTexture; Gdiplus::Color* normalPalette; int textureWidth; // Get the texture properties of the model. model.GetTexture(&texture, &palette, &textureWidth); model.GetNormalMapTexture(&normalTexture, &normalPalette, &textureWidth); // Set the scanlines to very high and very low values so // they will be set on the first set of interpolation. for (unsigned int i = 0; i < _height; i++) { _scanlines[i].xStart = 99999; _scanlines[i].xEnd = -99999; } // Interpolates between each of the vertexs of the polygon and sets the start // and end values for each of the scanlines it comes in contact with. InterpolateScanline(_scanlines, v1, v2); InterpolateScanline(_scanlines, v2, v3); InterpolateScanline(_scanlines, v3, v1); // Go through each scanline and each pixel in the scanline and // sets its color. for (unsigned int y = 0; y < _height; y++) { // Work out the color and UV differences between the start and end of the scanline. float redColorDiff = (_scanlines[y].redEnd - _scanlines[y].redStart); float greenColorDiff = (_scanlines[y].greenEnd - _scanlines[y].greenStart); float blueColorDiff = (_scanlines[y].blueEnd - _scanlines[y].blueStart); float uCoordDiff = _scanlines[y].uEnd - _scanlines[y].uStart; float vCoordDiff = _scanlines[y].vEnd - _scanlines[y].vStart; float zCoordDiff = _scanlines[y].zEnd - _scanlines[y].zStart; float xNormalDiff = (_scanlines[y].xNormalEnd - _scanlines[y].xNormalStart); float yNormalDiff = (_scanlines[y].yNormalEnd - _scanlines[y].yNormalStart); float zNormalDiff = (_scanlines[y].zNormalEnd - _scanlines[y].zNormalStart); float xDiff = (_scanlines[y].pixelXEnd - _scanlines[y].pixelXStart); float yDiff = (_scanlines[y].pixelYEnd - _scanlines[y].pixelYStart); float zDiff = (_scanlines[y].pixelZEnd - _scanlines[y].pixelZStart); float diff = (_scanlines[y].xEnd - _scanlines[y].xStart) + 1; for (int x = (int)_scanlines[y].xStart; x <= (int)_scanlines[y].xEnd; x++) { if (x < 0 || x >= (int)_width) continue; int offset = (int)(x - _scanlines[y].xStart); // Work out the UV coordinate of the current pixel. float uCoord = _scanlines[y].uStart + ((uCoordDiff / diff) * offset); float vCoord = _scanlines[y].vStart + ((vCoordDiff / diff) * offset); float zCoord = _scanlines[y].zStart + ((zCoordDiff / diff) * offset); uCoord /= zCoord; vCoord /= zCoord; // Work out the normal of the pixel. float xNormal = _scanlines[y].xNormalStart + ((xNormalDiff / diff) * offset); float yNormal = _scanlines[y].yNormalStart + ((yNormalDiff / diff) * offset); float zNormal = _scanlines[y].zNormalStart + ((zNormalDiff / diff) * offset); // Work out the position of the pixel. float pixelX = _scanlines[y].pixelXStart + ((xDiff / diff) * offset); float pixelY = _scanlines[y].pixelYStart + ((yDiff / diff) * offset); float pixelZ = _scanlines[y].pixelZStart + ((zDiff / diff) * offset); // Work out the lighting colour of the current pixel. //float lightR = (_scanlines[y].redStart + ((redColorDiff / diff) * offset)) / 180.0f; //float lightG = (_scanlines[y].greenStart + ((greenColorDiff / diff) * offset)) / 180.0f; //float lightB = (_scanlines[y].blueStart + ((blueColorDiff / diff) * offset)) / 180.0f; // Using the UV coordinate work out which pixel in the texture to use to draw this pixel. int pixelIndex = (int)vCoord * textureWidth + (int)uCoord; if (pixelIndex >= textureWidth * textureWidth || pixelIndex < 0) { pixelIndex = (textureWidth * textureWidth) - 1; } int paletteOffset = texture[pixelIndex]; if (paletteOffset >= 255) paletteOffset = 255; Gdiplus::Color textureColor = palette[paletteOffset]; // Work out the pixel colour of the normalmap. pixelIndex = (int)vCoord * textureWidth + (int)uCoord; if (pixelIndex >= textureWidth * textureWidth || pixelIndex < 0) { pixelIndex = (textureWidth * textureWidth) - 1; } paletteOffset = normalTexture[pixelIndex]; if (paletteOffset >= 255) paletteOffset = 255; Gdiplus::Color normalTextureColor = normalPalette[paletteOffset]; // Calculate normal lighting for the pixel. Vector3D heightMapVector = Vector3D(normalTextureColor.GetR() / 180.0f, normalTextureColor.GetG() / 180.0f, normalTextureColor.GetB() / 180.0f); heightMapVector = Vector3D((heightMapVector.GetX() - 0.5f) * 2.0f, (heightMapVector.GetY() - 0.5f) * 2.0f, (heightMapVector.GetZ() - 0.5f) * 2.0f); // Work out he pixels normal and position. Vector3D pixelNormal = Vector3D(xNormal, yNormal, zNormal);//;Vector3D(heightMapVector.GetX(), heightMapVector.GetY(), heightMapVector.GetZ()); Vertex pixelPosition = Vertex(pixelX, pixelY, pixelZ, 1, Gdiplus::Color::White, Vector3D(0, 0, 0), 0); heightMapVector = Vector3D((pixelNormal.GetX() * heightMapVector.GetX()) , (pixelNormal.GetY() * heightMapVector.GetY()) , (pixelNormal.GetZ() * heightMapVector.GetZ()) ); // Calculate the sum dot product of all lighting vectors for this pixel and divide by the number // of lights. float lightDot = 0.0f; int count = 0; for (unsigned int j = 0; j < pointLights.size(); j++) { PointLight* light = pointLights[j]; if (light->GetEnabled() == false) continue; // Work out vector to light source. Vector3D lightVector = Vertex::GetVector(pixelPosition, light->GetPosition()); float distance = lightVector.GetLength(); lightVector.Normalize(); // Work out dot product. lightDot += Vector3D::DotProduct(heightMapVector, lightVector); count++; } for (unsigned int j = 0; j < directionalLights.size(); j++) { DirectionalLight* light = directionalLights[j]; if (light->GetEnabled() == false) continue; // Work out vector to light source. Vector3D lightVector = Vertex::GetVector(pixelPosition, light->GetPosition()); float distance = lightVector.GetLength(); lightVector.Normalize(); // Work out dot product. lightDot += Vector3D::DotProduct(heightMapVector, lightVector); count++; } lightDot /= count; // Adjust texture colour based on the lighting dot product. Gdiplus::Color pixelColor = textureColor; //pixelColor = model.CalculateLightingAmbientPerPixel(ambientLights, pixelPosition, pixelNormal, pixelColor); //pixelColor = model.CalculateLightingDirectionalPerPixel(directionalLights, pixelPosition, pixelNormal, pixelColor); //pixelColor = model.CalculateLightingPointPerPixel(pointLights, pixelPosition, pixelNormal, pixelColor); float lightR = (_scanlines[y].redStart + ((redColorDiff / diff) * offset)) / 180.0f; float lightG = (_scanlines[y].greenStart + ((greenColorDiff / diff) * offset)) / 180.0f; float lightB = (_scanlines[y].blueStart + ((blueColorDiff / diff) * offset)) / 180.0f; // Apply the lighting value to the texture colour and use the result to set the colour of the current pixel. int finalR = (int)max(0, min(255, (lightR * textureColor.GetR()) - ((lightR * textureColor.GetR()) * lightDot) )); int finalG = (int)max(0, min(255, (lightG * textureColor.GetG()) - ((lightG * textureColor.GetG()) * lightDot) )); int finalB = (int)max(0, min(255, (lightB * textureColor.GetB()) - ((lightB * textureColor.GetB()) * lightDot) )); WritePixel(x, y, Gdiplus::Color(finalR, finalG, finalB)); } } // Dispose of dynamic objects. delete[] _scanlines; _polygonsRendered++; }
void Matrix3x3::SetRow(unsigned int uiRow, Vector3D Value) { m_dValues[uiRow][0] = Value.GetX(); m_dValues[uiRow][1] = Value.GetY(); m_dValues[uiRow][2] = Value.GetZ(); }
float Vector3D::DotProduct(Vector3D &Vector){ float fResult; return fResult = (fx*Vector.GetX())+ (fy*Vector.GetY())+(fz*Vector.GetZ()); }
Vector3D Vector3D::operator-(const Vector3D &right) const { return Vector3D(m_dCoords[0] - right.GetX(), m_dCoords[1] - right.GetY(), m_dCoords[2] - right.GetZ()); }
float Vector3D::CrossProduct(Vector3D &Vector){ return (c_fx = fy*Vector.GetZ() - fz*Vector.GetNormalY()),(c_fy = fz*Vector.GetX() - fx*Vector.GetZ()),(c_fz = fx*Vector.GetNormalY() - fy*Vector.GetX()); }
double Vector3D::operator*(const Vector3D &right) const { return m_dCoords[0] * right.GetX() + m_dCoords[1] * right.GetY() + m_dCoords[2] * right.GetZ(); }
Matrix Matrix::operator / (const Vector3D& v) { return Matrix(Rows[0]/v.GetX(), Rows[1]/v.GetY(), Rows[2]/v.GetZ()); }