예제 #1
0
파일: World.cs 프로젝트: KWEngine/KWEngine2
        /// <summary>
        /// Setzt das Modell für die First-Person-Ansicht (z.B. Hände oder Waffen)
        /// </summary>
        /// <param name="modelname">Name des 3D-Modells</param>
        /// <param name="isAffectedByLight">Gibt an, ob das Modell auf Lightquellen reagiert oder nicht</param>
        /// <param name="receiveShadows">Gibt an, ob Schatten das Modell beeinflussen oder nicht</param>
        public void SetFirstPersonObjectItemModel(string modelname, bool isAffectedByLight = true, bool receiveShadows = false)
        {
            if (KWEngine.CurrentWorld._firstPersonObject == null)
            {
                return;
            }

            if (KWEngine.CurrentWorld._firstPersonWeaponObject == null)
            {
                _firstPersonWeaponObject = new FPVModelObject();
                _firstPersonWeaponObject.SetModel(modelname, KWEngine.CurrentWorld, isAffectedByLight, receiveShadows);
            }
            else
            {
                _firstPersonWeaponObject.SetModel(modelname, KWEngine.CurrentWorld, isAffectedByLight, receiveShadows);
            }
            _firstPersonWeaponObject._fakeGameObject.CurrentWorld = this;
        }
예제 #2
0
        internal void Draw(GameObject g, ref Matrix4 viewProjection, int textureIndex, FPVModelObject fpModelObject = null)
        {
            lock (g)
            {
                GL.Uniform4(mUniform_Glow, g.Glow);
                GL.Uniform4(mUniform_Outline, g.ColorOutline);
                GL.Uniform3(mUniform_TintColor, g.Color);

                GL.Uniform1(mUniform_LightAffection, g.IsAffectedByLight ? 1 : 0);

                // Camera
                if (!CurrentWorld.IsFirstPersonMode)
                {
                    GL.Uniform3(mUniform_uCameraPos, g.CurrentWorld.GetCameraPosition().X, g.CurrentWorld.GetCameraPosition().Y, g.CurrentWorld.GetCameraPosition().Z);
                    GL.Uniform3(mUniform_uCameraDirection, g.CurrentWorld.GetCameraLookAtVector());
                }
                else
                {
                    if (fpModelObject != null)
                    {
                        GL.Uniform3(mUniform_uCameraPos, fpModelObject._tmpLAVStartPos);
                        GL.Uniform3(mUniform_uCameraDirection, fpModelObject._tmpLAV);
                    }
                    else
                    {
                        GL.Uniform3(mUniform_uCameraPos, g.CurrentWorld.GetFirstPersonObject().Position.X, g.CurrentWorld.GetFirstPersonObject().Position.Y + g.CurrentWorld.GetFirstPersonObject().FPSEyeOffset, g.CurrentWorld.GetFirstPersonObject().Position.Z);
                        GL.Uniform3(mUniform_uCameraDirection, HelperCamera.GetLookAtVector());
                    }
                }

                int index = 0;
                for (int i = 0; i < g.Model.Meshes.Keys.Count; i++)
                {
                    string meshName = g.Model.Meshes.Keys.ElementAt(i);
                    if (g.Model.IsKWCube6)
                    {
                        index = 0;
                    }

                    // Matrices:
                    try
                    {
                        Matrix4.Mult(ref g.ModelMatrixForRenderPass[index], ref viewProjection, out _modelViewProjection);
                        Matrix4.Transpose(ref g.ModelMatrixForRenderPass[index], out _normalMatrix);
                        Matrix4.Invert(ref _normalMatrix, out _normalMatrix);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    GL.UniformMatrix4(mUniform_ModelMatrix, false, ref g.ModelMatrixForRenderPass[index]);
                    GL.UniformMatrix4(mUniform_NormalMatrix, false, ref _normalMatrix);
                    GL.UniformMatrix4(mUniform_MVP, false, ref _modelViewProjection);

                    index++;

                    GL.Disable(EnableCap.Blend);
                    GeoMesh mesh = g.Model.Meshes.Values.ElementAt(i);

                    GeoMaterial meshMaterial = g._materials[i];

                    if (meshMaterial.Opacity <= 0)
                    {
                        continue;
                    }

                    if (meshMaterial.Opacity < 1 || g.Opacity < 1 || g.Model.Name == "kwquad.obj")
                    {
                        GL.Enable(EnableCap.Blend);
                    }

                    if (g.ColorEmissive.W > 0)
                    {
                        GL.Uniform4(mUniform_EmissiveColor, g.ColorEmissive);
                    }
                    else
                    {
                        GL.Uniform4(mUniform_EmissiveColor, meshMaterial.ColorEmissive.X, meshMaterial.ColorEmissive.Y, meshMaterial.ColorEmissive.Z, meshMaterial.ColorEmissive.W);
                    }
                    GL.Uniform1(mUniform_Opacity, meshMaterial.Opacity * g.Opacity);

                    if (mesh.BoneNames.Count > 0 && g.AnimationID >= 0 && g.Model.Animations != null && g.Model.Animations.Count > 0)
                    {
                        GL.Uniform1(mUniform_UseAnimations, 1);
                        for (int j = 0; j < g.BoneTranslationMatrices[meshName].Length; j++)
                        {
                            Matrix4 tmp = g.BoneTranslationMatrices[meshName][j];
                            GL.UniformMatrix4(mUniform_BoneTransforms + j, false, ref tmp);
                        }
                    }
                    else
                    {
                        GL.Uniform1(mUniform_UseAnimations, 0);
                    }

                    GL.Uniform1(mUniform_Roughness, meshMaterial.Roughness);
                    GL.Uniform1(mUniform_Metalness, meshMaterial.Metalness);
                    Vector4 texTransform = new Vector4(meshMaterial.TextureAlbedo.UVTransform.X == 0 ? 1 : meshMaterial.TextureAlbedo.UVTransform.X, meshMaterial.TextureAlbedo.UVTransform.Y == 0 ? 1 : meshMaterial.TextureAlbedo.UVTransform.Y, g.mTextureOffset.X, g.mTextureOffset.Y);
                    GL.Uniform4(mUniform_TextureTransform, texTransform);

                    // albedo map:
                    int texId = meshMaterial.TextureAlbedo.OpenGLID;
                    GL.ActiveTexture(TextureUnit.Texture0 + textureIndex);
                    if (texId > 0)
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texId);
                        GL.Uniform1(mUniform_Texture, textureIndex);
                        GL.Uniform1(mUniform_TextureUse, 1);
                        GL.Uniform3(mUniform_BaseColor, 1f, 1f, 1f);
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureWhite);
                        GL.Uniform1(mUniform_Texture, textureIndex);
                        GL.Uniform1(mUniform_TextureUse, 0);
                        GL.Uniform3(mUniform_BaseColor, meshMaterial.ColorAlbedo.X, meshMaterial.ColorAlbedo.Y, meshMaterial.ColorAlbedo.Z);
                    }

                    // normal map:
                    texId = meshMaterial.TextureNormal.OpenGLID;
                    GL.ActiveTexture(TextureUnit.Texture0 + textureIndex + 1);
                    if (texId > 0)
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texId);
                        GL.Uniform1(mUniform_TextureNormalMap, textureIndex + 1);
                        GL.Uniform1(mUniform_TextureUseNormalMap, 1);
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureWhite);
                        GL.Uniform1(mUniform_TextureNormalMap, textureIndex + 1);
                        GL.Uniform1(mUniform_TextureUseNormalMap, 0);
                    }

                    // roughness map:
                    GL.ActiveTexture(TextureUnit.Texture0 + textureIndex + 2);
                    if (meshMaterial.TextureRoughness.OpenGLID > 0)
                    {
                        GL.BindTexture(TextureTarget.Texture2D, meshMaterial.TextureRoughness.OpenGLID);
                        GL.Uniform1(mUniform_TextureRoughnessMap, textureIndex + 2);
                        GL.Uniform1(mUniform_TextureUseRoughnessMap, 1);
                        GL.Uniform1(mUniform_TextureRoughnessIsSpecular, meshMaterial.TextureRoughnessIsSpecular ? 1 : 0);
                    }
                    else
                    {
                        if (meshMaterial.TextureRoughnessInMetalness && meshMaterial.TextureMetalness.OpenGLID > 0)
                        {
                            GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureWhite);
                            GL.Uniform1(mUniform_TextureRoughnessMap, textureIndex + 2);
                            GL.Uniform1(mUniform_TextureUseRoughnessMap, 0);
                            GL.Uniform1(mUniform_TextureRoughnessIsSpecular, 1);
                        }
                        else
                        {
                            GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureWhite);
                            GL.Uniform1(mUniform_TextureRoughnessMap, textureIndex + 2);
                            GL.Uniform1(mUniform_TextureUseRoughnessMap, 0);
                            GL.Uniform1(mUniform_TextureRoughnessIsSpecular, 0);
                        }
                    }

                    // metalness map:
                    GL.ActiveTexture(TextureUnit.Texture0 + textureIndex + 3);
                    if (meshMaterial.TextureMetalness.OpenGLID > 0)
                    {
                        GL.BindTexture(TextureTarget.Texture2D, meshMaterial.TextureMetalness.OpenGLID);
                        GL.Uniform1(mUniform_TextureMetalnessMap, textureIndex + 3);
                        GL.Uniform1(mUniform_TextureUseMetalnessMap, 1);
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureBlack);
                        GL.Uniform1(mUniform_TextureMetalnessMap, textureIndex + 3);
                        GL.Uniform1(mUniform_TextureUseMetalnessMap, 0);
                    }

                    // emissive map:
                    GL.ActiveTexture(TextureUnit.Texture0 + textureIndex + 4);
                    if (meshMaterial.TextureEmissive.OpenGLID > 0)
                    {
                        GL.BindTexture(TextureTarget.Texture2D, meshMaterial.TextureEmissive.OpenGLID);
                        GL.Uniform1(mUniform_TextureEmissiveMap, textureIndex + 4);
                        GL.Uniform1(mUniform_TextureUseEmissiveMap, 1);
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureBlack);
                        GL.Uniform1(mUniform_TextureEmissiveMap, textureIndex + 4);
                        GL.Uniform1(mUniform_TextureUseEmissiveMap, 0);
                    }



                    GL.ActiveTexture(TextureUnit.Texture0 + textureIndex + 5);
                    if (meshMaterial.TextureLight.OpenGLID > 0)
                    {
                        GL.BindTexture(TextureTarget.Texture2D, meshMaterial.TextureLight.OpenGLID);
                        GL.Uniform1(mUniform_TextureLightMap, textureIndex + 5);
                        GL.Uniform1(mUniform_TextureUseLightMap, 1);
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, KWEngine.TextureWhite);
                        GL.Uniform1(mUniform_TextureLightMap, textureIndex + 5);
                        GL.Uniform1(mUniform_TextureUseLightMap, 0);
                    }

                    GL.Uniform1(mUniform_TextureFullAlpha, g.Model.Name == "kwquad.obj" ? 1 : 0);

                    GL.BindVertexArray(mesh.VAO);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.VBOIndex);
                    GL.DrawElements(mesh.Primitive, mesh.IndexCount, DrawElementsType.UnsignedInt, 0);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
                    GL.BindVertexArray(0);
                    GL.BindTexture(TextureTarget.Texture2D, 0);
                }
            }
        }