示例#1
0
        public void Update(Stopwatch gameTime, KeyboardState state, KeyboardState prevstate, ref Matrix4 cameraPosition, ref Matrix4 cameraRotation, MouseState mousestate, MouseState prevmousestate)
        {
            foreach (gameObject g in gameObjects)
            {
                g.Update(gameTime, state);
            }

            cameraheight.Y += mousestate.ScrollWheelValue - prevmousestate.ScrollWheelValue;
            Matrix4 carTransform = carbody.CalcFinalTransform();

            cameraPosition = Matrix4.Invert(carTransform.ClearRotation()) * Matrix4.CreateTranslation(cameraheight);
            cameraRotation = Matrix4.Invert(carTransform.ClearTranslation()) * Matrix4.CreateRotationY((float)Math.PI) * Matrix4.CreateRotationX((float)(0.5 * Math.PI));

            if (state.IsKeyDown(Key.H) && !prevstate.IsKeyDown(Key.H))
            {
                if (headlight)
                {
                    spotlights = spotlights.GetRange(0, spotlights.Count - 4);
                    headlight  = false;
                }
                else
                {
                    spotlights.Add(new Spotlight(new Vector4(-0.25f, 0.2f, 0.75f, 1), new Vector4(0, 0, 1, 0), 50, new Vector3(1, 1, 1), carbody, 0.8f));
                    spotlights.Add(new Spotlight(new Vector4(0.25f, 0.2f, 0.75f, 1), new Vector4(0, 0, 1, 0), 50, new Vector3(1, 1, 1), carbody, 0.8f));
                    spotlights.Add(new Spotlight(new Vector4(-0.25f, 0.2f, -0.5f, 1), new Vector4(0, 0, -1, 0), 30, new Vector3(1, 0, 0), carbody, 0.95f));
                    spotlights.Add(new Spotlight(new Vector4(0.25f, 0.2f, -0.5f, 1), new Vector4(0, 0, -1, 0), 30, new Vector3(1, 0, 0), carbody, 0.95f));
                    headlight = true;
                }
            }
        }
示例#2
0
        public void setTransform(Matrix4 transformation, int type = 0, bool parentAction = false)
        {
            processed_transform = Matrix4.Identity;

            if (parentAction)
            {
                if (parent != null)
                {
                    processed_transform = object_transform * origin_transform * parent.processed_transform;
                }
                else
                {
                    processed_transform = object_transform * origin_transform;
                }
            }
            else
            {
                if (type == 0)
                {
                    // set rotation
                    object_transform = object_transform.ClearRotation();
                    object_transform = object_transform * transformation;
                }

                else if (type == 1)
                {
                    //set translation
                    object_transform = object_transform.ClearTranslation();
                    object_transform = object_transform * transformation;
                }

                else
                {
                    //clear scale
                    object_transform = object_transform.ClearScale();
                    object_transform = object_transform * transformation;
                }



                if (parent != null)
                {
                    processed_transform = object_transform * origin_transform * parent.processed_transform;
                }
                else
                {
                    processed_transform = object_transform * origin_transform;
                }
            }

            foreach (var child in children)
            {
                child.setTransform(transformation, type, isParent());
            }
        }
示例#3
0
        public static Matrix4 RotateMatrixTo(Matrix4 sourceMatrix, Vector3 rotation)
        {
            Matrix4 xrot = Matrix4.CreateRotationX(rotation.X);
            //Matrix4 yrot = Matrix4.CreateRotationY(rotation.Y);
            //Matrix4 zrot = Matrix4.CreateRotationZ(rotation.Z);


            //Matrix4 rotationMatrix = xrot * yrot * zrot;

            Matrix4 result = xrot * sourceMatrix.ClearRotation();

            return(result);
        }
 public virtual void SetRotation(Vector3 axis, float angle)
 {
     _state_matrix = _state_matrix.ClearRotation() * Matrix4.CreateFromAxisAngle(axis, angle);
 }
示例#5
0
        public override void ApplyMaterial(Mesh mesh, Material mat, bool textured, bool shaded, bool twoSided)
        {
            RenderControl.GLError("StartMaterialMapper");
            ShaderGen.GenFlags flags = 0;
            var hasAlpha             = false;
            var hasTexture           = false;

            // note: keep this up-to-date with the code in MaterialMapper.UploadTextures()
            for (int i = 0; i < Renderer.modernGLUsedTextureTypeCount; i++)
            {
                TextureType currTextureType = (TextureType)((int)TextureType.Diffuse + i);
                GL.ActiveTexture((TextureUnit)((int)TextureUnit.Texture0 + i));
                GL.BindTexture(TextureTarget.Texture2D, Renderer.modernGLTextureType[i]); //we use own texture always, even when textures off to supply preset values
                if (textured && mat.GetMaterialTextureCount(currTextureType) > 0)
                {
                    hasTexture = true;
                    //flags |= ShaderGen.GenFlags.Texture; flag not used, we always have some texture
                    TextureSlot tex;
                    mat.GetMaterialTexture(currTextureType, 0, out tex);
                    var gtex = _scene.TextureSet.GetOriginalOrReplacement(tex.FilePath);
                    hasAlpha = hasAlpha || gtex.HasAlpha == Texture.AlphaState.HasAlpha;
                    if (gtex.State == Texture.TextureState.GlTextureCreated)
                    {
                        gtex.BindGlTexture();
                    }
                }
            }
            GL.ActiveTexture(TextureUnit.Texture0);
            RenderControl.GLError("EndTextureSettings");
            if (shaded)
            {
                flags |= ShaderGen.GenFlags.Lighting;
            }
            var hasColors = mesh != null && mesh.HasVertexColors(0);

            if (hasColors)
            {
                flags |= ShaderGen.GenFlags.VertexColor;
            }
            if (_UseSceneLights)
            {
                flags |= ShaderGen.GenFlags.PhongSpecularShading;
            }
            if ((mat.IsTwoSided) || (twoSided))
            {
                flags |= ShaderGen.GenFlags.TwoSide;
            }
            Shader shader = _shaderGen.GenerateOrGetFromCache(flags, _LightCount > 0 ? _LightCount : 1);

            shader.BindIfNecessary();
            Matrix4 curView = Matrix4.CreateScale(_scene.Scale) * _View;

            shader.SetMat4("WorldViewProjection", _World * curView * _Perspective);
            Matrix4 cameraPos      = _View.ClearRotation();
            Matrix4 cameraRotation = _View.ClearTranslation();
            Matrix4 cam            = Matrix4.Identity;

            cam = cameraPos * cameraRotation;
            Vector3 cameraPosition = -cam.ExtractTranslation() / _scene.Scale;//does not work for orbitcontroller

            //            cameraPosition = new Vector3(200,100,-100); //1m = 100units and positive
            cameraPosition.Z = -cameraPosition.Z;
            shader.SetVec3("CameraPosition", -cameraPosition);
            shader.SetMat4("World", _World);
            shader.SetMat4("WorldView", _World * curView); //_world* curView keeps light source at "fixed" position during rotating of the model
            shader.SetFloat("SceneBrightness", _SceneBrightness);
            shader.SetFloat("Material.diffuse", 0);
            shader.SetFloat("Material.ambient", 1);
            shader.SetFloat("Material.specular", 2);
            shader.SetFloat("Material.emissive", 3);
            shader.SetFloat("Material.height", 4);
            shader.SetFloat("Material.normal", 5);
            shader.SetLights(_GLLights, _LightCount);
            RenderControl.GLError("UniformSettings");

            // note: keep semantics of hasAlpha consistent with IsAlphaMaterial()
            var alpha = 1.0f;

            if (mat.HasOpacity)
            {
                alpha = mat.Opacity;
                if (alpha < AlphaSuppressionThreshold) // suppress zero opacity, this is likely wrong input data
                {
                    alpha = 1.0f;
                }
            }
            var color = new Color4(.8f, .8f, .8f, 1.0f);

            if (mat.HasColorDiffuse)
            {
                color = AssimpToOpenTk.FromColor(mat.ColorDiffuse);
                if (color.A < AlphaSuppressionThreshold) // s.a.
                {
                    color.A = 1.0f;
                }
            }
            color.A *= alpha;
            hasAlpha = hasAlpha || color.A < 1.0f;

            if (shaded)
            {
                // if the material has a texture but the diffuse color texture is all black,
                // then heuristically assume that this is an import/export flaw and substitute
                // white.
                if (hasTexture && color.R < 1e-3f && color.G < 1e-3f && color.B < 1e-3f)
                {
                    color = Color4.White;
                }
                shader.SetCol4("MaterialDiffuse_Alpha", color);

                color = new Color4(0, 0, 0, 1.0f);
                if (mat.HasColorSpecular)
                {
                    color = AssimpToOpenTk.FromColor(mat.ColorSpecular);
                }
                shader.SetCol4("MaterialSpecular", color);

                color = new Color4(.2f, .2f, .2f, 1.0f);
                if (mat.HasColorAmbient)
                {
                    color = AssimpToOpenTk.FromColor(mat.ColorAmbient);
                }
                shader.SetCol4("MaterialAmbient", color);

                color = new Color4(0, 0, 0, 1.0f);
                if (mat.HasColorEmissive)
                {
                    color = AssimpToOpenTk.FromColor(mat.ColorEmissive);
                }
                shader.SetCol4("MaterialEmissive", color);

                float shininess = 1;
                float strength  = 1;
                if (mat.HasShininess)
                {
                    shininess = mat.Shininess;
                }
                // todo: I don't even remember how shininess strength was supposed to be handled in assimp .. Scales the specular color of the material.Not implemented here.
                if (mat.HasShininessStrength)
                {
                    strength = mat.ShininessStrength;
                }
                var exp = shininess;
                if (exp >= 128.0f) // 128 is the maximum exponent as per the Gl spec
                {
                    exp = 128.0f;
                }
                shader.SetFloat("MaterialShininess", exp);
                //Shininess may be at mat.ColorSpecular.a too..?? but in FBX is
            }
            else if (!hasColors)
            {
                shader.SetCol4("MaterialDiffuse_Alpha", color);
            }

            if (hasAlpha)
            {
                GL.Enable(EnableCap.Blend);
                GL.BlendFuncSeparate(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha, BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                GL.DepthMask(false);
            }
            else
            {
                GL.DepthMask(true);
                GL.Disable(EnableCap.Blend);
            }
            RenderControl.GLError("EndMaterialMapper");
        }