簡體   English   中英

C#OpenTK GLSL在嘗試渲染時崩潰

[英]C# OpenTK GLSL Crashing on attempting to render

我一直在嘗試在程序中實現GLSL,但是鑒於我以前從未使用過GLSL,因此我決定嘗試按照教程進行操作。 不幸的是,以下教程並不是我的專長,並且當調用“ RenderTerrain()”時(在函數第二行代碼(GL.DrawElements)中),程序崩潰使我陷入困境

現在,這顯然是程序中的所有代碼,還有很多,沒有人可能要經過20k行才能找到我的問題:P但是,如果您需要詢問有關它的任何問題,請發表評論:)

所以我的問題就是,這段代碼有什么問題嗎? 為什么會崩潰?

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;
using OpenTK;

namespace HoardOfUpgrades
{
    public class Shaders
    {
        private static string TerrainVertexShaderText = @"

            #version 140

            // object space to camera space transformation
            uniform mat4 modelview_matrix;            

            // camera space to clip coordinates
            uniform mat4 projection_matrix;


            // incoming vertex position
            in vec3 vertex_position;

            // incoming vertex normal
            in vec3 vertex_normal;

            // incoming vertex_color
            in vec3 vertex_color

            // transformed vertex normal
            out vec3 normal;

            void main(void)
            {
              //not a proper transformation if modelview_matrix involves non-uniform scaling
              normal = ( modelview_matrix * vec4( vertex_normal, 0 ) ).xyz;

              // transforming the incoming vertex position
              gl_Position = projection_matrix * modelview_matrix * vec4( vertex_position, 1 );
            }

        ";

        private static string TerrainFragmentShaderText = @"

            #version 140

            precision highp float;

            const vec3 ambient = vec3( 0.1, 0.1, 0.1 );
            const vec3 lightVecNormalized = normalize( vec3( 0.5, 0.5, 2 ) );
            const vec3 lightColor = vec3( 1.0, 0.8, 0.2 );

            in vec3 normal;

            out vec4 out_frag_color;

            void main(void)
            {
              float diffuse = clamp( dot( lightVecNormalized, normalize( normal ) ), 0.0, 1.0 );
              out_frag_color = vec4( ambient + diffuse * lightColor, 1.0 );
            }

        ";

        public static int TerrainFragmentShaderHandle, TerrainVertexShaderHandle, TerrainProgramHandle, ProjectionMatrixLocation, ModelviewMatrixLocation, TerrainNormHandle, TerrainPosHandle, TerrainColorHandle, TerrainIndicesHandle, TerrainIndiceCount;

        public static void Load(Vector3[] position, Vector3[] normals, Vector3[] colors, int[] indices)
        {
            LoadShaders();
            LoadProgram();

            LoadVertexPositions(position);
            LoadVertexNormals(normals);
            LoadVertexColors(colors);
            LoadIndexer(indices);
        }

        static void LoadProgram()
        {
            TerrainProgramHandle = GL.CreateProgram();

            GL.AttachShader(TerrainProgramHandle, TerrainVertexShaderHandle);
            GL.AttachShader(TerrainProgramHandle, TerrainVertexShaderHandle);

            GL.LinkProgram(TerrainProgramHandle);
        }

        static void LoadShaders()
        {
            TerrainVertexShaderHandle = GL.CreateShader( ShaderType.VertexShader );
            TerrainFragmentShaderHandle = GL.CreateShader( ShaderType.FragmentShader );

            GL.ShaderSource(TerrainVertexShaderHandle, TerrainVertexShaderText);
            GL.ShaderSource(TerrainFragmentShaderHandle, TerrainFragmentShaderText);

            GL.CompileShader(TerrainVertexShaderHandle);
            GL.CompileShader(TerrainFragmentShaderHandle);
        }

        private static void QueryMatrixLocations()
        {
            ProjectionMatrixLocation = GL.GetUniformLocation(TerrainProgramHandle, "projection_matrix");
            ModelviewMatrixLocation = GL.GetUniformLocation(TerrainProgramHandle, "modelview_matrix");
        }

        public static void SetModelviewMatrix(Matrix4 matrix)
        {
            GL.UniformMatrix4(ModelviewMatrixLocation, false, ref matrix);
        }

        public static void SetProjectionMatrix(Matrix4 matrix)
        {
            GL.UniformMatrix4(ProjectionMatrixLocation, false, ref matrix);
        }

        private static void LoadVertexPositions(Vector3[] data)
        {
            GL.GenBuffers(1, out TerrainPosHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, TerrainPosHandle);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                new IntPtr(data.Length * Vector3.SizeInBytes),
                data, BufferUsageHint.StaticDraw);

            GL.EnableVertexAttribArray(0);
            GL.BindAttribLocation(TerrainProgramHandle, 0, "vertex_position");
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
        }

        private static void LoadVertexNormals(Vector3[] data)
        {
            GL.GenBuffers(1, out TerrainNormHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, TerrainNormHandle);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                new IntPtr(data.Length * Vector3.SizeInBytes),
                data, BufferUsageHint.StaticDraw);

            GL.EnableVertexAttribArray(1);
            GL.BindAttribLocation(TerrainProgramHandle, 1, "vertex_normal");
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
        }

        private static void LoadVertexColors(Vector3[] data)
        {
            GL.GenBuffers(1, out TerrainColorHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, TerrainColorHandle);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                new IntPtr(data.Length * Vector3.SizeInBytes),
                data, BufferUsageHint.StaticDraw);

            GL.EnableVertexAttribArray(1);
            GL.BindAttribLocation(TerrainProgramHandle, 1, "vertex_color");
            GL.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
        }

        private static void LoadIndexer(int[] data)
        {
            TerrainIndiceCount = data.Length;

            GL.GenBuffers(1, out TerrainIndicesHandle);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, TerrainIndicesHandle);
            GL.BufferData<int>(BufferTarget.ElementArrayBuffer,
                new IntPtr(data.Length * sizeof(int)),
                data, BufferUsageHint.StaticDraw);
        }

        public static void RenderTerrain()
        {
            GL.UseProgram(TerrainProgramHandle);

            GL.DrawElements(BeginMode.Triangles, TerrainIndiceCount,
                DrawElementsType.UnsignedInt, IntPtr.Zero);

            GL.UseProgram(0);
        }
    }
}

這些代碼行必須包括在內:

        GL.DisableClientState(ArrayCap.NormalArray);
        GL.DisableClientState(ArrayCap.VertexArray);
        GL.DisableClientState(ArrayCap.TextureCoordArray);

啟用了數組,必須禁用它們才能使用GL.DrawElements()函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM