Exemplo n.º 1
0
        public void onEnter()
        {
            this.camera = new PerspecitveCamera(MathHelper.PiOver4, Game.shared.width, Game.shared.height);
            this.shader = Game.assets.shader("test");
            VertexAttributes attrs = new VertexAttributes(VertexAttribute.Position(), VertexAttribute.Color());

            this.rawMesh     = new VertexBufferObject(true, 3, 7, attrs);
            this.rawIndicies = new IndexBufferObject(true);

            float[] data = new float[] {
                -0.8f, -0.8f, 0f, 1f, 0f, 0f, 1.0f,
                0.8f, -0.8f, 0f, 0f, 1f, 0f, 1.0f,
                0f, 0.8f, 0f, 0f, 0f, 1f, 1.0f,
                0.8f, 0.8f, 0f, 1f, 0f, 1f, 1.0f
            };

            uint[] indicies = new uint[] {
                0, 1, 2,
                1, 2, 3
            };

            this.rawIndicies.setIndicies(ref indicies);
            this.rawMesh.setVerticies(ref data);

            this.mviewdata = Matrix4.Translation(0f, 0f, 0f);
            Vector3 cameraPosition = new Vector3(5f, 5f, 5f);

            this.camera.translate(ref cameraPosition);

            Vector3 target = new Vector3(0f, 0f, 0f);

            this.camera.lookAt(ref target);
        }
Exemplo n.º 2
0
        private void SetupMesh()
        {
            VertexBuffer_ = new VertexBufferObject(VertexBufferFormat.PositionUvNormal, Vertices);
            IndexBuffer_  = new IndexBufferObject(Indices);

            for (var Index = 0; Index < Textures.Length; ++Index)
            {
                if (Textures[Index].Type == "texture_diffuse")
                {
                    TexDiffuse_ = Textures[Index].Tex;
                }
                else if (Textures[Index].Type == "texture_specular")
                {
                    TexSpecular_ = Textures[Index].Tex;
                }
            }
        }
Exemplo n.º 3
0
        public TestObject(GraphicsDevice Device)
            : base(Device)
        {
            Rand_ = new Random((int)DateTime.Now.Ticks);

            var Vertices = new[]
            {
                -1f, 1f, 0f, 0f, 0f, 0f, 0f, 1f,  // 0
                1f, 1f, 0f, 1f, 0f, 0f, 0f, 1f,   // 1
                1f, -1f, 0f, 1f, 1f, 0f, 0f, 1f,  // 2
                -1f, -1f, 0f, 0f, 1f, 0f, 0f, 1f  // 3
            };

            var Indices = new[]
            {
                0, 3, 1, // first triangle
                1, 3, 2  // second triangle
            };

            VertexBuffer_ = new VertexBufferObject(VertexBufferFormat.PositionUvNormal, Vertices);
            IndexBuffer_  = new IndexBufferObject(Indices);

            var DirLight = new Light();

            DirLight.Type      = LightType.Direction;
            DirLight.Direction = new Vector3(0.2f, 0.2f, -1f);
            DirLight.Ambient   = new Colorf(0.05f, 0.05f, 0.05f);
            DirLight.Diffuse   = new Colorf(0.4f, 0.4f, 0.4f);
            DirLight.Specular  = new Colorf(0.5f, 0.5f, 0.5f);
            DirLight.Enabled   = true;
            DirLight.UpdateSIMD();

            Shader_           = new ShaderLight();
            Shader_.Lights    = new Light[1];
            Shader_.Lights[0] = DirLight;

            Shader_.TexDiffuse    = TextureHelper.Load("container2.png");
            Shader_.TexSpecular   = TextureHelper.Load("container2_specular.png");
            Shader_.SpecularPower = 32;

            //Camera_ = new Camera(new Vector3(0, 0, 3), new Vector3(0, 0, -1), new Vector3(0, 1, 0), 60.0f, 800.0f / 600.0f, 0.3f, 100.0f);
            Camera_ = new Camera(800, 600);
        }
Exemplo n.º 4
0
        public RenderCommand debugRender()
        {
            if (myVbo == null)
            {
                myVbo = new VertexBufferObject <V3>(BufferUsageHint.DynamicDraw);
                myIbo = new IndexBufferObject(BufferUsageHint.DynamicDraw);
            }

            uint vi = 0;
            uint ii = 0;

            V3[]   verts = new V3[1024 * 1024];
            uint[] index = new uint[1024 * 1024 * 2];

            myNodes[0].debugRender(ref verts, ref vi, ref index, ref ii);

            myVbo.setData(verts, (int)vi);
            myIbo.setData(index, (int)ii);
            RenderVboCommand cmd = new RenderVboCommand(myVbo, myIbo, Color4.Blue, PrimitiveType.Quads);

            return(cmd);
        }
Exemplo n.º 5
0
        public GraphicsContext(GraphicsDevice Device, Box Viewport)
        {
            this.CoreNum = 6;

            this.Device     = Device;
            this.Viewport   = Viewport;
            this.FrameCount = 0;

            this.ColorBuffer = null;
            this.ZBuffer     = new float[Viewport.Width * Viewport.Height];

            this.Vertices  = new ListCore <Vertex>(this.CoreNum);
            this.Fragments = new ListCore <Fragment>(this.CoreNum);
            this.Indices   = null;

            this.ClearColor         = Colorf.Black;
            this.FillModeState      = FillMode.Solid;
            this.CullFaceModeState  = CullFaceMode.Back;
            this.FrontFaceModeState = FrontFaceMode.CCW;
            this.Shader             = null;
            this.VertexBuffer       = null;
            this.IndexBuffer        = null;
        }
Exemplo n.º 6
0
        public UiRenderCommand(DrawCmd drawCmd, Matrix4 modelmatrix, VertexBufferObject vbo, IndexBufferObject ibo)
            : base()
        {
            myElementCount  = (int)drawCmd.elementCount;
            myElementOffset = (int)drawCmd.elementOffset;

            pipelineState = thePipelineState;

            renderState.scissorTest.enabled = true;
            renderState.scissorTest.rect    = drawCmd.clipRect;
            renderState.setVertexBuffer(vbo.id, 0, 0, V2T2B4.stride);
            renderState.setIndexBuffer(ibo.id);

            renderState.setUniform(new UniformData(0, Uniform.UniformType.Int, 0));
            renderState.setUniform(new UniformData(0, Uniform.UniformType.Mat4, modelmatrix));
            renderState.setTexture((int)drawCmd.texture.id(), 0, TextureTarget.Texture2D);
        }
Exemplo n.º 7
0
        static RenderCubemapSphere()
        {
            theOrientation = Matrix4.CreateFromQuaternion(new Quaternion(0f, 0f, 0f));

            List <ShaderDescriptor> shadersDesc = new List <ShaderDescriptor>();

            shadersDesc.Add(new ShaderDescriptor(ShaderType.VertexShader, "GpuNoise.shaders.cube-vs.glsl"));
            shadersDesc.Add(new ShaderDescriptor(ShaderType.FragmentShader, "GpuNoise.shaders.cube-ps.glsl"));

            ShaderProgramDescriptor sd = new ShaderProgramDescriptor(shadersDesc);

            theShader = Renderer.resourceManager.getResource(sd) as ShaderProgram;

            theVBO = new VertexBufferObject <V3>(BufferUsageHint.StaticDraw);
            theIBO = new IndexBufferObject(BufferUsageHint.StaticDraw);

            int lats    = 25;
            int longs   = 25;
            int vertIdx = 0;

            V3[]     verts = new V3[(lats + 1) * (longs + 1)];
            ushort[] index = new ushort[lats * longs * 6];

            for (int i = 0; i <= lats; i++)
            {
                float theta    = (float)i * (float)Math.PI / lats;
                float sinTheta = (float)Math.Sin(theta);
                float cosTheta = (float)Math.Cos(theta);

                for (int j = 0; j <= longs; j++)
                {
                    float phi    = (float)j * (float)(Math.PI * 2) / longs;
                    float sinPhi = (float)Math.Sin(phi);
                    float cosPhi = (float)Math.Cos(phi);

                    float x = cosPhi * sinTheta;
                    float y = sinPhi * sinTheta;
                    float z = cosTheta;
                    //          float u = 1 - (j / longs);
                    //          float v = 1- (i / lats);

                    V3 temp = new V3();
                    temp.Position    = new Vector3(x, y, z);
                    verts[vertIdx++] = temp;
                }
            }

            int indexIdx = 0;

            for (int i = 0; i < lats; i++)
            {
                for (int j = 0; j < longs; j++)
                {
                    ushort first  = (ushort)((i * (longs + 1)) + j);
                    ushort second = (ushort)(first + longs + 1);
                    index[indexIdx++] = first; index[indexIdx++] = second; index[indexIdx++] = (ushort)(first + 1);
                    index[indexIdx++] = second; index[indexIdx++] = (ushort)(second + 1); index[indexIdx++] = (ushort)(first + 1);
                }
            }

            theVBO.setData(verts);
            theIBO.setData(index);

            theVAO = new VertexArrayObject();
            theVAO.bindVertexFormat <V3>(theShader);

            thePipeline = new PipelineState();
            thePipeline.shaderState.shaderProgram = theShader;
            thePipeline.vaoState.vao = theVAO;
            thePipeline.generateId();
        }
Exemplo n.º 8
0
 public void BindIndexBuffer(IndexBufferObject Buffer)
 {
     Context_.IndexBuffer = Buffer;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Generates indices for an index buffer
        /// </summary>
        /// <param name="ibo">Index buffer instance</param>
        private static void GenerateIndices(IndexBufferObject ibo)
        {
            int[] indices = new int[IndexCount];

            // Top
            indices[0] = FrontTopLeft;
            indices[1] = FrontTopRight;
            indices[2] = FrontTopLeft;
            indices[3] = BackTopLeft;
            indices[4] = BackTopRight;
            indices[5] = FrontTopRight;
            indices[6] = BackTopRight;
            indices[7] = BackTopLeft;

            // Bottom
            indices[8] = FrontBottomLeft;
            indices[9] = FrontBottomRight;
            indices[10] = FrontBottomLeft;
            indices[11] = BackBottomLeft;
            indices[12] = BackBottomRight;
            indices[13] = FrontBottomRight;
            indices[14] = BackBottomRight;
            indices[15] = BackBottomLeft;

            //Sides
            indices[16] = FrontTopRight;
            indices[17] = FrontBottomRight;
            indices[18] = FrontTopLeft;
            indices[19] = FrontBottomLeft;
            indices[20] = BackTopRight;
            indices[21] = BackBottomRight;
            indices[22] = BackTopLeft;
            indices[23] = BackBottomLeft;

            ibo.SetData(indices);
        }
        private Skin SendDataToGpu(string modelPath)
        {
            Skin resultSkin = null;

            // Get mesh data
            using (AssimpModelLoader loader = new AssimpModelLoader(modelPath))
            {
                VertexArrayObject vao      = new VertexArrayObject();
                MeshVertexData    meshData = loader.GetMeshData();
                // explicit null assignment if there is no some of mesh data
                var vertices     = meshData.Verts;
                var normals      = meshData.bHasNormals ? meshData.N_Verts : null;
                var texCoords    = meshData.T_Verts;
                var tangents     = meshData.bHasTangentVertices ? meshData.Tangent_Verts : meshData.bHasTextureCoordinates ? VectorMath.AdditionalVertexInfoCreator.CreateTangentVertices(vertices, texCoords) : null;
                var bitangents   = meshData.bHasTangentVertices ? meshData.Bitanget_Verts : meshData.bHasTextureCoordinates ? VectorMath.AdditionalVertexInfoCreator.CreateBitangentVertices(vertices, texCoords) : null;
                var blendWeights = meshData.bHasAnimation ? meshData.BlendWeights : null;
                var blendIndices = meshData.bHasAnimation ? meshData.BlendIndices : null;

                UInt32[] indices = meshData.bHasIndices ? meshData.Indices.ToArray() : null;

                IndexBufferObject ibo = null;
                VertexBufferObjectTwoDimension <float> normalsVBO = null, texCoordsVBO = null, tangentsVBO = null, bitangentsVBO = null, blendWeightsVBO = null;
                VertexBufferObjectTwoDimension <Int32> blendIndicesVBO = null;

                if (meshData.bHasIndices)
                {
                    ibo = new IndexBufferObject(indices);
                }

                VertexBufferObjectTwoDimension <float> vertexVBO = new VertexBufferObjectTwoDimension <float>(vertices, BufferTarget.ArrayBuffer, 0, 3, VertexBufferObjectBase.DataCarryFlag.Invalidate);

                if (meshData.bHasNormals)
                {
                    normalsVBO = new VertexBufferObjectTwoDimension <float>(normals, BufferTarget.ArrayBuffer, 1, 3, VertexBufferObjectBase.DataCarryFlag.Invalidate);
                }
                if (meshData.bHasTextureCoordinates)
                {
                    texCoordsVBO = new VertexBufferObjectTwoDimension <float>(texCoords, BufferTarget.ArrayBuffer, 2, 2, VertexBufferObjectBase.DataCarryFlag.Invalidate);
                }
                if (meshData.bHasTangentVertices)
                {
                    tangentsVBO = new VertexBufferObjectTwoDimension <float>(tangents, BufferTarget.ArrayBuffer, 4, 3, VertexBufferObjectBase.DataCarryFlag.Invalidate);
                }
                if (meshData.bHasTangentVertices)
                {
                    bitangentsVBO = new VertexBufferObjectTwoDimension <float>(bitangents, BufferTarget.ArrayBuffer, 5, 3, VertexBufferObjectBase.DataCarryFlag.Invalidate);
                }
                if (meshData.bHasAnimation)
                {
                    Int32 skeletonWeightsPerVertexCount = (Int32)loader.m_skeletonType;
                    if (skeletonWeightsPerVertexCount > 4)
                    {
                        throw new NotImplementedException("There is no implementation yet for cases when there are more than four weights influencing on vertex.");
                    }

                    Int32 vectorSize = skeletonWeightsPerVertexCount;
                    blendWeightsVBO = new VertexBufferObjectTwoDimension <float>(blendWeights, BufferTarget.ArrayBuffer, 6, vectorSize, VertexBufferObjectBase.DataCarryFlag.Invalidate);
                    blendIndicesVBO = new VertexBufferObjectTwoDimension <int>(blendIndices, BufferTarget.ArrayBuffer, 7, vectorSize, VertexBufferObjectBase.DataCarryFlag.Invalidate);
                }

                vao.AddVBO(vertexVBO, normalsVBO, texCoordsVBO, tangentsVBO, bitangentsVBO, blendWeightsVBO, blendIndicesVBO);
                vao.AddIndexBuffer(ibo);
                vao.BindBuffersToVao();

                if (meshData.bHasAnimation)
                {
                    ParentBone rootBone = AssimpConverter.Converter.ConvertAssimpBoneToEngineBone(meshData.SkeletonRoot);
                    resultSkin = new AnimatedSkin(vao, rootBone);
                }
                else
                {
                    resultSkin = new Skin(vao);
                }
            }

            return(resultSkin);
        }