Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:22:33+00:00 2026-06-08T05:22:33+00:00

I’ve been trying to implement GLSL into my program, however given that i have

  • 0

I’ve been trying to implement GLSL into my program, however given that i have never used GLSL before, i decided I would try following a tutorial. Unfortunately following tutorials aren’t my forte, and I am stuck here with my program crashing when ‘RenderTerrain()’ is called (in the second line of code of the function (GL.DrawElements))

Now this obviously all the code in the program, there is A LOT more, there’s no chance any of you would go through 20k lines to find my problem 😛 But if you need to ask questions about any of it, please comment 🙂

So my question simply is, is there anything wrong with this code? Why would it be crashing?

    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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:22:34+00:00Added an answer on June 8, 2026 at 5:22 am

    These lines of code must be included:

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

    Arrays were enabled, they must be disabled to use the GL.DrawElements() function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.