ARTICLE AD BOX
I would like to render multiple cubes in different locations at the same time. It would help to make a landscape. I am unsure of how I can do this since I am a beginner at OpenTK. Here is my OpenTK Window.cs script.
And yes, I did use the OpenTK lessons to code this, but they don't really say how to add multiple cubes at once unless I missed something.
using OpenTK.Compute.OpenCL; using OpenTK.Graphics.OpenGL4; using OpenTK.Mathematics; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop; using OpenTK.Windowing.GraphicsLibraryFramework; using StbImageSharp; using System.Numerics; using System.Reflection.Metadata; using System.Xml.Linq; namespace ByteCraft___The_Fantasy_Land_of_Far_Away { public class BCWindow : GameWindow { private readonly float[] vertices = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, //bottom left 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //bottom right 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //bottom right -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, //top left -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, //top right -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, //bottom left 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, //bottom right 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, //bottom right -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, //top left -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, //top right -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //bottom left -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, //bottom right -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, //bottom right -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, //top left -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, //top right 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //bottom left 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, //bottom right 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, //bottom right 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, //top left 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, //top right -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, //bottom left 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, //bottom right 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, //bottom right -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, //top left -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, //top right -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //bottom left 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, //bottom right 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, //bottom right -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, //top left -0.5f, 0.5f, -0.5f, 0.0f, 1.0f //top right }; private readonly uint[] indices = { 0, 1, 3, 1, 2, 3 }; private readonly float[] texCoords = { 0.0f, 0.0f, // bottom left 1.0f, 0.0f, // bottom right 0.0f, 1.0f, // top left 1.0f, 1.0f // top right }; private Shader shader; private int vbo; private int ebo; private int vao; private Texture texture; private Texture texture2; OpenTK.Mathematics.Vector3 position = new OpenTK.Mathematics.Vector3(0.0f, 0.0f, 3.0f); OpenTK.Mathematics.Vector3 front = new OpenTK.Mathematics.Vector3(0.0f, 0.0f, -1.0f); OpenTK.Mathematics.Vector3 up = new OpenTK.Mathematics.Vector3(0.0f, 1.0f, 0.0f); float speed = 2f; float Pitch = 0f; float Yaw = 0f; float Roll = 0f; OpenTK.Mathematics.Vector2 lastpos; public BCWindow(int width, int height, string title) : base(GameWindowSettings.Default, new NativeWindowSettings() { ClientSize = (width, height), Title = title }) { } protected override void OnLoad() { base.OnLoad(); var sky = OpenTK.Mathematics.Color4.Lavender; GL.ClearColor(sky); vbo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw); vao = GL.GenVertexArray(); GL.BindVertexArray(vao); ebo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo); GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.DynamicDraw); shader = new Shader("shader.vert", "shader.frag"); shader.Use(); var vertexLocation = shader.GetAttribLocation("aPosition"); GL.EnableVertexAttribArray(vertexLocation); GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0); var texCoordLocation = shader.GetAttribLocation("aTexCoord"); GL.EnableVertexAttribArray(texCoordLocation); GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); GL.EnableVertexAttribArray(0); /*GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapNearest);*/ texture = Texture.LoadFromFile("Textures/grass_top.png"); texture.Use(TextureUnit.Texture0); texture2 = Texture.LoadFromFile("Textures/texture_atlas.png"); //texture2.Use(TextureUnit.Texture1); shader.SetInt("texture1", 0); shader.SetInt("texture2", 1); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); if (!IsFocused) { return; } KeyboardState input = KeyboardState; CursorState = CursorState.Grabbed; front.X = (float)Math.Cos(MathHelper.DegreesToRadians(Pitch)) * (float)Math.Cos(MathHelper.DegreesToRadians(Yaw * 5)); front.Y = (float)Math.Sin(MathHelper.DegreesToRadians(Pitch * 1.5)); front.Z = (float)Math.Cos(MathHelper.DegreesToRadians(Pitch)) * (float)Math.Sin(MathHelper.DegreesToRadians(Yaw * 5)); front = OpenTK.Mathematics.Vector3.Normalize(front); var mouse = MouseState; float sensitivity = 0.2f; float deltaX = mouse.X - lastpos.X; float deltaY = mouse.Y - lastpos.Y; lastpos = new OpenTK.Mathematics.Vector2(mouse.X, mouse.Y); Yaw += deltaX * sensitivity; Pitch -= deltaY * sensitivity; if (Pitch > 89.9f) { Pitch = 89.9f; } else if (Pitch < -89.9f) { Pitch = -89.9f; } else { Pitch -= deltaX * sensitivity; } bool firstMouse = true; if (firstMouse) { lastpos = new OpenTK.Mathematics.Vector2(mouse.X, mouse.Y); firstMouse = false; } else if (IsFocused) { } if (input.IsKeyDown(Keys.W)) { position += front * speed * (float)e.Time; } if (input.IsKeyDown(Keys.S)) { position -= front * speed * (float)e.Time; } if (input.IsKeyDown(Keys.A)) { position -= OpenTK.Mathematics.Vector3.Normalize(OpenTK.Mathematics.Vector3.Cross(front, up)) * speed * (float)e.Time; } if (input.IsKeyDown(Keys.D)) { position += OpenTK.Mathematics.Vector3.Normalize(OpenTK.Mathematics.Vector3.Cross(front, up)) * speed * (float)e.Time; } if (input.IsKeyDown(Keys.Space)) { position += up * speed * (float)e.Time; } if (input.IsKeyDown(Keys.LeftControl)) { position -= up * speed * (float)e.Time; } if (input.IsKeyDown(Keys.LeftShift)) { speed = 5f; } if (input.IsKeyReleased(Keys.LeftShift)) { speed = 2f; } if (KeyboardState.IsKeyDown(Keys.Escape)) { this.Close(); } } protected override void OnFramebufferResize(FramebufferResizeEventArgs e) { base.OnFramebufferResize(e); GL.Viewport(0, 0, e.Width, e.Height); } protected override void OnRenderFrame(FrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit); GL.BindVertexArray(vao); /*var transform = Matrix4.Identity; transform = transform * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(90f)); transform = transform * Matrix4.CreateScale(1.5f); transform = transform * Matrix4.CreateTranslation(0.1f, 0.1f, 0.0f);*/ int width = 500; int height = 500; OpenTK.Mathematics.Vector3 cameraPos = new OpenTK.Mathematics.Vector3(0.0f, 0.0f, 3.0f); OpenTK.Mathematics.Vector3 camTarget = OpenTK.Mathematics.Vector3.Zero; OpenTK.Mathematics.Vector3 camDirection = OpenTK.Mathematics.Vector3.Normalize(cameraPos - camTarget); OpenTK.Mathematics.Vector3 camUp = OpenTK.Mathematics.Vector3.UnitY; OpenTK.Mathematics.Vector3 cameraRight = OpenTK.Mathematics.Vector3.Normalize(OpenTK.Mathematics.Vector3.Cross(camUp, camDirection)); OpenTK.Mathematics.Vector3 cameraUp = OpenTK.Mathematics.Vector3.Cross(camDirection, cameraRight); Matrix4.CreateOrthographicOffCenter(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f); OpenTK.Mathematics.Vector4 vec = new OpenTK.Mathematics.Vector4(1.0f, 0.0f, 0.0f, 1.0f); Matrix4 model = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(0.0f)); Matrix4 model2 = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(0.0f)); Matrix4 model3 = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(0.0f)); Matrix4 model4 = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(0.0f)); Matrix4 model5 = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(0.0f)); Matrix4 scale = Matrix4.CreateScale(1.0f, 1.0f, 1.0f); Matrix4 view = Matrix4.LookAt(position, position + front, up); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0f), (float)width / (float)height, 0.1f, 100.0f); Matrix4 trans = model * scale; vec *= trans; Console.WriteLine("{0}, {1}, {2}", vec.X, vec.Y, vec.Z); /*GL.UseProgram(program); int location = GL.GetUniformLocation(Handle, name);*/ //GL.UniformMatrix4(location, true, ref matrix); texture.Use(TextureUnit.Texture0); texture2.Use(TextureUnit.Texture1); shader.Use(); shader.SetMatrix4("transform", trans); shader.SetMatrix4("model", model); shader.SetMatrix4("model2", model2); shader.SetMatrix4("model3", model3); shader.SetMatrix4("model4", model4); shader.SetMatrix4("model5", model5); shader.SetMatrix4("view", view); shader.SetMatrix4("projection", projection); GL.Enable(EnableCap.DepthTest); //GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DrawArrays(PrimitiveType.Triangles, 0, 36); Context.SwapBuffers(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); base.OnRenderFrame(e); } public void DrawCube() { shader.Use(); GL.BindVertexArray(vao); GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 3); } protected override void OnUnload() { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindVertexArray(0); GL.UseProgram(0); GL.DeleteBuffer(vbo); GL.DeleteVertexArray(vao); GL.DeleteProgram(shader.Handle); base.OnUnload(); } } }