Пример #1
0
        public void ApplyMaterials(ComposedShader shader)
        {
            List <Material> materials;
            Appearance      appearance;

            materials  = new List <Material>();
            appearance = this.ItemsByType <Appearance>().FirstOrDefault();

            if (appearance != null)
            {
                materials = appearance.ItemsByType <Material>();
            }

            shader.SetFieldValue("materialsEnabled", materials.Any() ? 1 : 0);

            if (materials.Any())
            {
            }
            else
            {
                //shaderMaterials.Add(new ShaderMaterial()
                //{
                //     ambientIntensity = 0.1f,
                //     diffuseColor = new Vector4(0,0,0,0),
                //     emissiveColor = new Vector4(0,0,0,0),
                //    specularColor = new Vector4(1, 1, 1, 1),
                //    transparency = 1.0f,
                //    shininess = 0.2f
                //});
            }

            shader.SetFieldValue("materialsCount", shaderMaterials.Count);

            Buffering.BufferMaterials(shader, shaderMaterials, "materials");
        }
Пример #2
0
        private void RenderShape(RenderingContext rc)
        {
            // Refactor tessellation
            var shader = CurrentShader;

            if (shader != null)
            {
                shader.Use();

                if (bbox != null)
                {
                    shader.SetFieldValue("bboxMaxWidth", bbox.Width);
                    shader.SetFieldValue("bboxMaxHeight", bbox.Height);
                    shader.SetFieldValue("bboxMaxDepth", bbox.Depth);

                    shader.SetFieldValue("bbox_x", bbox.Width);
                    shader.SetFieldValue("bbox_y", bbox.Height);
                    shader.SetFieldValue("bbox_z", bbox.Depth);
                }

                shader.SetFieldValue("coloringEnabled", coloring ? 1 : 0);

                if (typeof(ElevationGrid).IsInstanceOfType(geometry))
                {
                    shader.SetFieldValue("lightingEnabled", 0);
                    shader.SetFieldValue("texturingEnabled", 1);
                }

                Vector3 tmp = rc.matricies.Scale;
                if (typeof(Text).IsInstanceOfType(geometry))
                {
                    rc.PushMatricies();
                    rc.matricies.Scale *= 200f;
                }

                if (depthMask == false)
                {
                    //REFACTOR!!
                    Matrix4 mat4 = Matrix4.Identity;
                    //Quaternion qRotFix = QuaternionExtensions.EulerToQuat(rc.cam.calibOrient.X, rc.cam.calibOrient.Y, rc.cam.calibOrient.Z);
                    //mat4 *= Matrix4.CreateTranslation(rc.cam.calibTrans) * Matrix4.CreateFromQuaternion(qRotFix);
                    Quaternion qRotFix = QuaternionExtensions.EulerToQuat(0.15f, 3.479997f, 0f);
                    mat4 *= Matrix4.CreateTranslation(new Vector3(0f, 0f, -0.29f)) * Matrix4.CreateFromQuaternion(qRotFix);
                    // test weapon/gun rendering fixed in front of player
                    //TODO: port this to X3D

                    shader.SetFieldValue("modelview", ref mat4);
                    if (quadShader != null)
                    {
                        quadShader.SetFieldValue("modelview", ref mat4);
                    }
                    //GL.DepthMask(false);
                }

                shader.SetFieldValue("size", size);
                shader.SetFieldValue("scale", scale);

                if (loadedGeometry)
                {
                    if (shader.IsTessellator)
                    {
                        RenderTessellator(rc);
                    }
                    else
                    {
                        if (typeof(PointSet).IsInstanceOfType(geometry))
                        {
                            RenderPoints(rc);
                        }
                        else if (typeof(IndexedLineSet).IsInstanceOfType(geometry) || typeof(LineSet).IsInstanceOfType(geometry))
                        {
                            RenderLines(rc);
                        }
                        else
                        {
                            RenderTriangles(rc);
                            RenderQuads(rc);
                        }
                    }
                }


                if (depthMask == false)
                {
                    //GL.DepthMask(true);
                }

                if (typeof(Text).IsInstanceOfType(geometry))
                {
                    rc.PopMatricies();
                    rc.matricies.Scale = tmp;
                }
            }
        }
Пример #3
0
        public override void Render(RenderingContext rc)
        {
            base.Render(rc);

            rc.PushMatricies();

            //if (!loadedGeometry) return;

            // PREPARE shape for rendering

            NormalMatrix = new Matrix3(rc.matricies.modelview); // NormalMatrix = M4GetUpper3x3(ModelviewMatrix);

            var shader = CurrentShader;

            if (shader != null)
            {
                // CurrentShader = linkedShaders;


                shader.Use();

                if (shader.IsTessellator)
                {
                    if (shader.IsBuiltIn)
                    {
                        // its a built in system shader so we are using the the fixed parameter inbuilt tesselator
                        CurrentShader.SetFieldValue("TessLevelInner", this.tessLevelInner);
                        CurrentShader.SetFieldValue("TessLevelOuter", this.tessLevelOuter);
                    }
                }

                shader.SetFieldValue("lightingEnabled", 1);
                shader.SetFieldValue("headlightEnabled", 0);
                shader.SetFieldValue("calib1", rc.cam.calibTrans);
                shader.SetFieldValue("calib2", rc.cam.calibOrient);

                if (depthMask)
                {
                    Matrix4 MVP     = ApplyGeometricTransformations(rc, shader, this);
                    Vector3 lookat  = QuaternionExtensions.Rotate(rc.cam.Orientation, Vector3.UnitZ);
                    Vector3 forward = new Vector3(lookat.X, 0, lookat.Z).Normalized();
                    Vector3 up      = Vector3.UnitY;
                    Vector3 left    = up.Cross(forward);

                    Vector2 orient;
                    Vector3 position;

                    orient   = QuaternionExtensions.ExtractPitchYawRoll(rc.cam.Orientation.Inverted()).Xy; // pitch and yaw only
                    position = rc.cam.Position;

                    shader.SetFieldValue("headlightEnabled", NavigationInfo.HeadlightEnabled ? 1 : 0);
                    shader.SetFieldValue("sceneCameraPosition", position);
                    shader.SetFieldValue("model", ref MVP);
                    shader.SetFieldValue("orientation", orient);
                    shader.SetFieldValue("lookat", rc.cam.Direction);
                    shader.SetFieldValue("forward", forward);
                    shader.SetFieldValue("up", up);
                    shader.SetFieldValue("left", left);
                }
                else
                {
                    //REFACTOR!!

                    RefreshDefaultUniforms(shader);

                    if (shader.IsTessellator)
                    {
                        RefreshTessUniforms(shader);
                    }

                    //Matrix4 MVP = rc.cam.GetWorldOrientation() ;

                    //shader.SetFieldValue("modelview", ref MVP);

                    //shader.SetFieldValue("modelview", ref MVP); //GL.UniformMatrix4(uniformModelview, false, ref rc.matricies.modelview);
                    shader.SetFieldValue("projection", ref rc.matricies.projection);
                    shader.SetFieldValue("camscale", rc.cam.Scale.X);                        //GL.Uniform1(uniformCameraScale, rc.cam.Scale.X);
                    shader.SetFieldValue("X3DScale", rc.matricies.Scale);                    //GL.Uniform3(uniformX3DScale, rc.matricies.Scale);
                    shader.SetFieldValue("coloringEnabled", coloring ? 1 : 0);
                    shader.SetFieldValue("texturingEnabled", this.texturingEnabled ? 1 : 0); //GL.Uniform1(uniforms.a_texturingEnabled, this.texturingEnabled ? 1 : 0);
                    shader.SetFieldValue("normalmatrix", ref NormalMatrix);
                }

                ApplyAppearance(rc);

                // RENDER shape
                RenderShape(rc);
            }

            if (drawBoundingBox && bbox != null)
            {
                RenderBoundingBox(rc);
            }
        }
Пример #4
0
        public Matrix4 ApplyGeometricTransformations(RenderingContext rc, ComposedShader shader, SceneGraphNode context)
        {
            RefreshDefaultUniforms(shader);
            //RefreshMaterialUniforms();

            if (shader.IsTessellator)
            {
                RefreshTessUniforms(shader);
            }


            Matrix4 view = Matrix4.LookAt(new Vector3(4, 3, 3), // Camera is at (4,3,3), in World Space
                                          new Vector3(0, 0, 0), // and looks at the origin
                                          new Vector3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
                                          );

            Matrix4 model; // applied transformation hierarchy

            SceneGraphNode transform_context = context == null ? this : context;

            List <Transform> transformationHierarchy = transform_context
                                                       .AscendantByType <Transform>()
                                                       .Select(t => (Transform)t)
                                                       .Where(t => t.Hidden == false)
                                                       .ToList();

            Matrix4 modelview = Matrix4.Identity;// * rc.matricies.worldview;

            // using Def_Use/Figure02.1Hut.x3d Cone and Cylinder
            Vector3 x3dScale = new Vector3(0.06f, 0.06f, 0.06f); // scaling down to conform with X3D standard (note this was done manually and might need tweaking)

            //x3dScale = Vector3.One;

            Quaternion modelrotation      = Quaternion.Identity;
            Matrix4    modelLocalRotation = Matrix4.Identity;


            //if (rc.cam.OrbitLocalOrientation != Vector2.Zero)
            //{
            //    // Center of Rotation based on center of bounding box
            //    Quaternion qLocal = QuaternionExtensions.EulerToQuat(0, -rc.cam.OrbitLocalOrientation.X, -rc.cam.OrbitLocalOrientation.Y);
            //    Quaternion qAdjust = QuaternionExtensions.EulerToQuat(MathHelpers.PIOver2, 0.0f, 0.0f);

            //    Matrix4 mat4CenterOfRotation = Matrix4.CreateTranslation(centerOfRotation);
            //    Matrix4 origin = Matrix4.CreateTranslation(new Vector3(0, 0, 0));

            //    modelLocalRotation = mat4CenterOfRotation * Matrix4.CreateFromQuaternion(qLocal) * Matrix4.CreateFromQuaternion(qAdjust);
            //}

            //const float bbscale = 0.0329999961f;

            Vector3 centerOffset = Vector3.Zero;

            foreach (Transform transform in transformationHierarchy)
            {
                modelview = SceneEntity.ApplyX3DTransform(centerOffset,
                                                          Vector3.Zero,
                                                          transform.Scale,
                                                          Vector3.Zero,
                                                          transform.Translation, // * x3dScale,
                                                          modelview);

                //modelview *= Matrix4.CreateTranslation(transform.Translation * x3dScale);

                //modelrotation = new Quaternion(transform.Rotation.X, transform.Rotation.Y, transform.Rotation.Z, transform.Rotation.W);
                //modelrotations *= Matrix4.CreateFromQuaternion(modelrotation);

                //modelrotations *= MathHelpers.CreateRotation(ref modelrotation);
            }

            //Vector3 center = modelview.ExtractTranslation();
            //Vector3 centerOffsetVector = center + (bbox.Maximum - bbox.Minimum);
            //Matrix4 centerOffset = Matrix4.CreateTranslation(centerOffsetVector);



            model = modelview;

            Matrix4 cameraTransl = Matrix4.CreateTranslation(rc.cam.Position);

            Quaternion q = rc.cam.Orientation;

            Matrix4 cameraRot;

            cameraRot = Matrix4.CreateFromQuaternion(q); // cameraRot = MathHelpers.CreateRotation(ref q);


            Matrix4 MVP = ((modelLocalRotation * model) * cameraTransl) * cameraRot; // position and orient the Shape relative to the world and camera



            //shader.SetFieldValue("size", new Vector3(bbox.Width, bbox.Height, bbox.Depth) * bbscale);
            shader.SetFieldValue("modelview", ref MVP);                              //GL.UniformMatrix4(uniformModelview, false, ref rc.matricies.modelview);
            shader.SetFieldValue("projection", ref rc.matricies.projection);
            shader.SetFieldValue("camscale", rc.cam.Scale.X);                        //GL.Uniform1(uniformCameraScale, rc.cam.Scale.X);
            shader.SetFieldValue("X3DScale", rc.matricies.Scale);                    //GL.Uniform3(uniformX3DScale, rc.matricies.Scale);
            shader.SetFieldValue("coloringEnabled", this.coloring ? 1 : 0);          //GL.Uniform1(uniforms.a_coloringEnabled, 0);
            shader.SetFieldValue("texturingEnabled", this.texturingEnabled ? 1 : 0); //GL.Uniform1(uniforms.a_texturingEnabled, this.texturingEnabled ? 1 : 0);
            shader.SetFieldValue("lightingEnabled", 1);

            if (shader.IsBuiltIn == false)
            {
                shader.ApplyFieldsAsUniforms(rc);
            }

            return(MVP);
        }