public Mesh(VertexBuffer <Vertex> vbo, IndexBuffer ibo) { _vao = new(); _vao .AddVertexBuffer(vbo) .SetIndexBuffer(ibo); }
public override void OnAttach() { app = Application.GetApplication(); var window = app.GetWindow(); Renderer.SetClearColor(Color.White.ToVec4()); orthoCamera = new OrthographicCamera(-16f, 16f, -9f, 9f); //persCamera = new PerspectiveCamera(45, 16f/9f, 0.01f, 1000); //selectedCamera = persCamera; //persCamera.Position = new OpenTK.Mathematics.Vector3(0, 0, -1); float[] verticies = { -0.5f, -0.5f, 0.0f, /* Colors 0.0f, 0.0f, 0.0f, */ /* Tex Coords */ 0.0f, 0.0f, 0.5f, 0.5f, 0.0f, /* Colors 0.0f, 0.0f, 0.0f, */ /* Tex Coords */ 1.0f, 1.0f, -0.5f, 0.5f, 0.0f, /* Colors 0.0f, 0.0f, 0.0f, */ /* Tex Coords */ 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, /* Colors 0.0f, 0.0f, 0.0f, */ /* Tex Coords */ 1.0f, 0.0f, }; uint[] indices = { 0, 1, 2, 0, 3, 1 }; shader = new Shader("Assets/Shaders/vertex.glsl", "Assets/Shaders/fragment.glsl", true); vertexBuffer = new VertexBuffer(verticies, verticies.Length * sizeof(float)); indexBuffer = new IndexBuffer(indices, indices.Length * sizeof(uint)); BufferLayout layout = new BufferLayout( new BufferElement("positions", ShaderDataType.Float3), new BufferElement("texCoords", ShaderDataType.Float2) //new BufferElement("colors", ShaderDataType.Float3) ); vertexBuffer.SetLayout(layout); vertexArray = new VertexArray(); vertexArray.AddVertexBuffer(vertexBuffer); vertexArray.SetIndexBuffer(indexBuffer); checkerboard = new Texture2D("Assets/Textures/Checkerboard.png"); cherno = new Texture2D("Assets/Textures/ChernoLogo.png"); shader.SetInt("uTexture", 0); vendorString = GL.GetString(StringName.Vendor); rendererString = GL.GetString(StringName.Renderer); versionString = GL.GetString(StringName.Version); FramebufferData fbData = new FramebufferData(1280, 720); framebuffer = new Framebuffer(fbData); viewportSize = new Vector2(framebuffer.FramebufferData.Width, framebuffer.FramebufferData.Height); }
public void Init() { if (VertexArray != null) { return; } VertexArray = new VertexArray(); VertexArray.SetStride(VertexProperties); //IEnumerable<float> raws; bool hasPosition = HasProperty(VertexProperty.Position), hasNormal = HasProperty(VertexProperty.Normal), hasUV = HasProperty(VertexProperty.UV); int offset = 0; int stride = (hasUV ? 2 : 0) + (hasPosition ? 3 : 0) + (hasNormal ? 3 : 0); if (hasPosition) { VertexArray.Model(Shader.PositionName, 3, stride, offset); offset += 3; } if (hasNormal) { VertexArray.Model(Shader.NormalName, 3, stride, offset); offset += 3; } if (hasUV) { VertexArray.Model(Shader.UVName, 2, stride, offset); } /*raws = Vertices.SelectMany(v => { * var raw = new List<float>(); * if (hasPosition) raw.AddRange(v.Position.Raw); * if (hasNormal) raw.AddRange(v.Normal.Raw); * if (hasUV) raw.AddRange(v.UV.Raw); * return raw; * });*/ VertexArray.AddVertexBuffer(Vertices); VertexArray.Init(); }
static void Main(string[] args) { Log.AddLogger(new FileLogger("log.txt")); Log.AddLogger(new ConsoleLogger()); Assets.AddAssets(Assembly.GetExecutingAssembly(), ""); var window = new Window(800, 600, "Sandbox"); window.MakeCurrent(); GL.LoadBindings(new GLFWBindingsContext()); var vbo = new Buffer <float>(new [] { 0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f }); var ebo = new Buffer <uint>(new uint[] { 0, 1, 2, 0, 2, 3 }); var vao = new VertexArray(); vao.AddVertexBuffer(vbo, 0, sizeof(float) * 2, new VertexArray.Attribute(0, 2, VertexAttribType.Float, sizeof(float) * 2)); vao.SetElementBuffer(ebo); var shader = new Shader(Assets.GetAsset <VertexShader>("shader.vert"), Assets.GetAsset <FragmentShader>("shader.frag")); shader.Bind(); shader.SetUniform(shader.GetUniformLocation("uTest"), Vector4.One); GL.ClearColor(Color4.Aqua); while (window.IsRunning) { GL.Clear(ClearBufferMask.ColorBufferBit); GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0); window.SwapBuffers(); window.PollEvents(); } vbo.Dispose(); ebo.Dispose(); vao.Dispose(); shader.Dispose(); window.Dispose(); }
static void Main(string[] args) { Log.AddLogger(new ConsoleLogger()); Assets.AddAssets(Assembly.GetExecutingAssembly(), "Assets"); var window = new Window(800, 600, "CubeWave"); window.MakeCurrent(); GL.LoadBindings(new GLFWBindingsContext()); GL.Enable(EnableCap.DepthTest); var shader = new Shader(Assets.GetAsset <VertexShader>("shader.vert"), Assets.GetAsset <FragmentShader>("shader.frag")); var positionBuffer = new Buffer <float>(new float[] { -_size, -_size, -_size, //0 -_size, -_size, _size, //1 -_size, _size, -_size, //2 -_size, _size, _size, //3 _size, -_size, -_size, //4 _size, -_size, _size, //5 _size, _size, -_size, //6 _size, _size, _size, //7 }); var ebo = new Buffer <uint>(new uint[] { 0, 1, 2, 1, 2, 3, //Left 4, 5, 6, 5, 6, 7, //Right 0, 1, 4, 1, 4, 5, //Bottom 2, 3, 6, 3, 6, 7, //Top 0, 2, 4, 2, 4, 6, //Front 1, 3, 5, 3, 5, 7, //Back }); var vao = new VertexArray(); vao.SetElementBuffer(ebo); vao.AddVertexBuffer(positionBuffer, 0, sizeof(float) * 3, new VertexArray.Attribute(0, 3, VertexAttribType.Float, 0)); var perspective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(65), window.Width / (float)window.Height, 0.001f, 1000f); var viewLoc = shader.GetUniformLocation("uView"); var timeLoc = shader.GetUniformLocation("uTime"); shader.Bind(); shader.SetUniform(shader.GetUniformLocation("uPerspective"), ref perspective); var stopWatch = new Stopwatch(); stopWatch.Start(); vao.Bind(); while (window.IsRunning) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); var t = stopWatch.ElapsedMilliseconds / 5000f; var view = Matrix4.LookAt(new Vector3(MathF.Cos(t) * 5, 5f, MathF.Sin(t) * 5), Vector3.Zero, Vector3.UnitY); shader.SetUniform(viewLoc, ref view); shader.SetUniform(timeLoc, ref t); GL.DrawElementsInstanced(PrimitiveType.Triangles, 36, DrawElementsType.UnsignedInt, IntPtr.Zero, 10000); window.SwapBuffers(); window.PollEvents(); } window.Dispose(); }
private static void CreateDeviceResources() { vertexArray = new VertexArray(); vertexBufferSize = 10000; indexBufferSize = 2000; //GL.CreateBuffers(1, out vertexBuffer); vertexBuffer = new VertexBuffer(vertexBufferSize); indexBuffer = new IndexBuffer(indexBufferSize); //GL.CreateBuffers(1, out indexBuffer); //GL.NamedBufferData(vertexBuffer, vertexBufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); //GL.NamedBufferData(indexBuffer, indexBufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); RecreateFontDeviceTexture(); string VertexSource = @" #version 330 core uniform mat4 projection_matrix; layout(location = 0) in vec2 in_position; layout(location = 1) in vec2 in_texCoord; layout(location = 2) in vec4 in_color; out vec4 color; out vec2 texCoord; void main() { gl_Position = projection_matrix * vec4(in_position, 0, 1); color = in_color; texCoord = in_texCoord; } "; string FragmentSource = @" #version 330 core uniform sampler2D in_fontTexture; in vec4 color; in vec2 texCoord; out vec4 outputColor; void main() { outputColor = color * texture(in_fontTexture, texCoord); } "; shader = new Shader(VertexSource, FragmentSource, false); // Old Code //GL.VertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, IntPtr.Zero, Unsafe.SizeOf<ImDrawVert>()); //GL.VertexArrayElementBuffer(vertexArray, indexBuffer); //GL.EnableVertexArrayAttrib(vertexArray, 0); //GL.VertexArrayAttribBinding(vertexArray, 0, 0); //GL.VertexArrayAttribFormat(vertexArray, 0, 2, VertexAttribType.Float, false, 0); //GL.EnableVertexArrayAttrib(vertexArray, 1); //GL.VertexArrayAttribBinding(vertexArray, 1, 0); //GL.VertexArrayAttribFormat(vertexArray, 1, 2, VertexAttribType.Float, false, 8); //GL.EnableVertexArrayAttrib(vertexArray, 2); //GL.VertexArrayAttribBinding(vertexArray, 2, 0); //GL.VertexArrayAttribFormat(vertexArray, 2, 4, VertexAttribType.UnsignedByte, true, 16); // New Code vertexArray.Bind(); //vertexBuffer.Bind(); //indexBuffer.Bind(); BufferLayout layout = new BufferLayout( new BufferElement("position", ShaderDataType.Float2, false), new BufferElement("texCoords", ShaderDataType.Float2, false), new BufferElement("color", ShaderDataType.UnsignedByte4, true) ); vertexBuffer.SetLayout(layout); vertexArray.AddVertexBuffer(vertexBuffer); vertexArray.SetIndexBuffer(indexBuffer); //GL.EnableVertexAttribArray(0); //GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, Unsafe.SizeOf<ImDrawVert>(), 0); // //GL.EnableVertexAttribArray(1); //GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, Unsafe.SizeOf<ImDrawVert>(), 8); // //GL.EnableVertexAttribArray(2); //GL.VertexAttribPointer(2, 4, VertexAttribPointerType.UnsignedByte, true, Unsafe.SizeOf<ImDrawVert>(), 16); }