Beispiel #1
0
        public void paintObjects()
        {
            LinearGradientBrush linGrBrush = new LinearGradientBrush(
                new Point(camera.screenWidth / 2, 0),
                new Point(camera.screenWidth / 2, camera.screenHeight),
                Color.FromArgb(255, 14, 14, 14),
                Color.FromArgb(255, 66, 66, 66));
            Bitmap   gradBack = new Bitmap(camera.screenWidth, camera.screenHeight);
            Graphics graphics = Graphics.FromImage(gradBack);

            graphics.FillRectangle(linGrBrush, 0, 0, camera.screenWidth, camera.screenHeight);
            FastBitmap bitmap = new FastBitmap(gradBack);

            //FastBitmap bitmap = new FastBitmap(camera.screenWidth, camera.screenHeight, Color.FromArgb(60, 60, 60));

            double[,] buffer = new double[camera.screenWidth, camera.screenHeight];
            for (int i = 0; i < camera.screenWidth; ++i)
            {
                for (int j = 0; j < camera.screenHeight; ++j)
                {
                    buffer[i, j] = double.MaxValue;
                }
            }

            Array2D cameraMatrix = camera.getCameraMatrix();

            for (int i = 0; i < groups.Count; ++i)
            {
                Group sceneObject = groups[i];
                if (IsVisibleForCamera(sceneObject.BasePoint, sceneObject.MaxLength, cameraMatrix))
                {
                    Array2D modificationMatrix = sceneObject.GetModificationMatrix();
                    foreach (SceneObject scenePrimitive in sceneObject.groupObjects)
                    {
                        Array2D primitiveMatrix = scenePrimitive.getModificationMatrix();
                        foreach (Face face in scenePrimitive.primitive.getFaces())
                        {
                            List <Point3D> points = new List <Point3D>();
                            foreach (Point3D point in face.getPoints())
                            {
                                points.Add(point.copy());
                            }

                            if (drawMode == 0 /*|| drawMode == 2 || drawMode == 3*/)
                            {
                                //DrawLines(bitmap, obj.basePoint, points, primitiveMatrix, face.getColor());
                                DrawLines(bitmap, sceneObject.BasePoint, scenePrimitive.primitive.basePoint, points, modificationMatrix, primitiveMatrix, scenePrimitive.primitive.color);
                            }
                            if (drawMode == 1 || drawMode == 2 || drawMode == 3)
                            {
                                ConvertLocalToCamera(sceneObject.BasePoint, scenePrimitive.primitive.basePoint, points, modificationMatrix, primitiveMatrix);
                                Face     temp       = new Face(new Point3D[] { points[0], points[1], points[2] });
                                Vector3D normal     = temp.getNormalVector();
                                Vector3D light1     = new Vector3D(20, 20, 20);
                                double   brightness = Math.Abs(Vector3D.AngleCos(normal, light1.normalize()));
                                brightness = Math.Max(brightness, 0.1);
                                //brightness *= Math.Abs(Vector3D.AngleCos(normal, light2.normalize()))
                                if (light1.x == 0 && light1.y == 0 && light1.z == 0)
                                {
                                    brightness = 0.1;
                                }
                                //Таргет не можнт иметь все нули - деление на ноль
                                int alpha = 255;
                                if (drawMode == 3)
                                {
                                    alpha = 160;
                                }
                                Color color = Color.FromArgb(alpha, (int)(scenePrimitive.primitive.color.R * brightness), (int)(scenePrimitive.primitive.color.G * brightness), (int)(scenePrimitive.primitive.color.B * brightness));

                                convertCameraToScreenCoords(points);

                                Triangle(new Vector3D(points[0]), new Vector3D(points[1]), new Vector3D(points[2]), color, bitmap, buffer);
                            }
                        }
                    }
                }
            }
            projectionScreen.Image = bitmap.GetBitmap();
        }