示例#1
0
 private void UnloadMeshData(MeshAssetData meshAsset)
 {
     if (meshAsset.IsLoaded)
     {
         GL.DeleteVertexArray(meshAsset.VertexArrayObjectHandle);
         GL.DeleteBuffer(meshAsset.VertexBufferHandle);
         GL.DeleteBuffer(meshAsset.IndicesBufferHandle);
     }
 }
示例#2
0
        private void RenderWithBlinn(
            MeshAssetData mesh,
            ImageAssetData colorTexture,
            ImageAssetData normalTexture,
            Matrix4 modelMatrix)
        {
            var modelViewProjection =
                modelMatrix *
                this.gameData.CameraData.Transformation *
                this.gameData.CameraData.PerspectiveProjection;

            var shader = this.gameData.BlinnShader;

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, colorTexture.OpenGLHandle);
            GL.ActiveTexture(TextureUnit.Texture1);
            GL.BindTexture(TextureTarget.Texture2D, normalTexture.OpenGLHandle);

            GL.BindVertexArray(mesh.VertexArrayObjectHandle);

            GL.UseProgram(shader.BasicShader.ProgramHandle);
            GL.UniformMatrix4(
                shader.BasicShader.ModelviewProjectionMatrixLocation,
                false,
                ref modelViewProjection);

            GL.UniformMatrix4(shader.ModelMatrixLocation, false, ref modelMatrix);
            GL.Uniform1(shader.ColorTextureLocation, 0);
            GL.Uniform1(shader.NormalTextureLocation, 1);
            GL.Uniform1(shader.MaterialShininessLocation, 2.0f);
            GL.Uniform3(shader.LightDirectionLocation, this.gameData.AmbientLightDirection);

            GL.Uniform4(shader.LightAmbientColorLocation, new Vector4(0.6f, 0.6f, 0.6f, 0));
            GL.Uniform4(shader.LightDiffuseColorLocation, new Vector4(0.8f, 0.8f, 0.8f, 0));
            GL.Uniform4(shader.LightSpecularColorLocation, new Vector4(0.0f, 0.0f, 0.0f, 0));
            GL.Uniform4(shader.CameraPositionLocation, new Vector4(this.gameData.CameraData.Eye, 1));

            GL.DrawElements(
                PrimitiveType.Triangles,
                mesh.IndicesCount,
                DrawElementsType.UnsignedInt,
                IntPtr.Zero);

            GL.BindVertexArray(0);
            GL.ActiveTexture(TextureUnit.Texture0);
        }
示例#3
0
        private void LoadMeshAsset(MeshAssetData meshAsset)
        {
            if (meshAsset.IsLoaded)
            {
                return;
            }

            var planeMesh = Wavefront.Load(meshAsset.AssetName);

            meshAsset.IndicesCount = planeMesh.Indices.Length;
            meshAsset.xMin         = planeMesh.xMin;
            meshAsset.xMax         = planeMesh.xMax;
            meshAsset.yMin         = planeMesh.yMin;
            meshAsset.yMax         = planeMesh.yMax;
            var xMax = Math.Max(Math.Abs(meshAsset.xMin), Math.Abs(meshAsset.xMax));
            var yMax = Math.Max(Math.Abs(meshAsset.yMin), Math.Abs(meshAsset.yMax));

            meshAsset.OverallMaximum = Math.Max(xMax, yMax);

            // load geometry into gpu memory
            int strideCount      = 14;
            var interleaved      = new float[strideCount * planeMesh.Vertices.Length];
            var interleavedIndex = 0;

            for (int i = 0; i < planeMesh.Vertices.Length; i++)
            {
                interleavedIndex = i * strideCount;
                interleaved[interleavedIndex++] = planeMesh.Vertices[i].X;
                interleaved[interleavedIndex++] = planeMesh.Vertices[i].Y;
                interleaved[interleavedIndex++] = planeMesh.Vertices[i].Z;

                interleaved[interleavedIndex++] = planeMesh.Normals[i].X;
                interleaved[interleavedIndex++] = planeMesh.Normals[i].Y;
                interleaved[interleavedIndex++] = planeMesh.Normals[i].Z;

                interleaved[interleavedIndex++] = planeMesh.UVs[i].X;
                interleaved[interleavedIndex++] = planeMesh.UVs[i].Y;

                interleaved[interleavedIndex++] = planeMesh.Tangents[i].X;
                interleaved[interleavedIndex++] = planeMesh.Tangents[i].Y;
                interleaved[interleavedIndex++] = planeMesh.Tangents[i].Z;

                interleaved[interleavedIndex++] = planeMesh.BiTangents[i].X;
                interleaved[interleavedIndex++] = planeMesh.BiTangents[i].Y;
                interleaved[interleavedIndex++] = planeMesh.BiTangents[i].Z;
            }

            int vertexBufferHandle;

            GL.GenBuffers(1, out vertexBufferHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferHandle);
            GL.BufferData(
                BufferTarget.ArrayBuffer,
                interleaved.Length * sizeof(float),
                interleaved,
                BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            meshAsset.VertexBufferHandle = vertexBufferHandle;

            int indexBufferHandle;

            GL.GenBuffers(1, out indexBufferHandle);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferHandle);
            GL.BufferData(
                BufferTarget.ElementArrayBuffer,
                sizeof(uint) * planeMesh.Indices.Length,
                planeMesh.Indices,
                BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            meshAsset.IndicesBufferHandle = indexBufferHandle;

            int vertexArrayObjectHandle;

            GL.GenVertexArrays(1, out vertexArrayObjectHandle);
            GL.BindVertexArray(vertexArrayObjectHandle);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferHandle);
            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);
            GL.EnableVertexAttribArray(2);
            GL.EnableVertexAttribArray(3);
            GL.EnableVertexAttribArray(4);

            var stride = (4 * Vector3.SizeInBytes) + Vector2.SizeInBytes;

            GL.VertexAttribPointer(
                VertexAttribIndex.Vertex,
                3,
                VertexAttribPointerType.Float,
                true,
                stride,
                0);

            GL.VertexAttribPointer(
                VertexAttribIndex.Normal,
                3,
                VertexAttribPointerType.Float,
                true,
                stride,
                Vector3.SizeInBytes);
            GL.VertexAttribPointer(
                VertexAttribIndex.Uv,
                2,
                VertexAttribPointerType.Float,
                true,
                stride,
                2 * Vector3.SizeInBytes);
            GL.VertexAttribPointer(
                VertexAttribIndex.Tangent,
                3,
                VertexAttribPointerType.Float,
                true,
                stride,
                2 * Vector3.SizeInBytes + Vector2.SizeInBytes);
            GL.VertexAttribPointer(
                VertexAttribIndex.Bitangent,
                3,
                VertexAttribPointerType.Float,
                true,
                stride,
                3 * Vector3.SizeInBytes + Vector2.SizeInBytes);
            GL.BindVertexArray(0);
            meshAsset.VertexArrayObjectHandle = vertexArrayObjectHandle;
            meshAsset.IsLoaded = true;
        }