Jump to content
  • Advertisement
Jens Eckervogt

OpenGL OpenTK: Can not show 2 white triangles!

Recommended Posts

Posted (edited)

Hello everyone, 

I have problem with texture :(

using System;
using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using System.Drawing;
using System.Reflection;

namespace Tutorial_05
{
    class Game : GameWindow
    {
        private static int WIDTH = 1200;
        private static int HEIGHT = 720;
        private static KeyboardState keyState;

        private int vaoID;
        private int vboID;
        private int iboID;

        private Vector3[] vertices =
        {
            new Vector3(-0.5f, 0.5f, 0.0f), // V0
            new Vector3(-0.5f, -0.5f, 0.0f), // V1
            new Vector3(0.5f, -0.5f, 0.0f), // V2
            new Vector3(0.5f, 0.5f, 0.0f) // V3
        };

        private Vector2[] texcoords =
        {
            new Vector2(0, 0),
            new Vector2(0, 1),
            new Vector2(1, 1),
            new Vector2(1, 0)
        };

        private int[] indices = {
            0, 1, 3,
            3, 1, 2
        };

        private string vertsrc =
            @"#version 450 core

            in vec3 position;
            in vec2 textureCoords;

            out vec2 pass_textureCoords;

            void main(void)
            {
                gl_Position = vec4(position, 1.0);
                pass_textureCoords = textureCoords;
            }";

        private string fragsrc =
            @"#version 450 core

            in vec2 pass_textureCoords;

            out vec4 out_color;

            uniform sampler2D textureSampler;

            void main(void)
            {
                out_color = texture(textureSampler, pass_textureCoords);
            }";

        private int programID;
        private int vertexShaderID;
        private int fragmentShaderID;

        private int textureID;
        private Bitmap texsrc;

        public Game() : base(WIDTH, HEIGHT, GraphicsMode.Default, "Tutorial 05 - Texturing", GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Default)
        {
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CursorVisible = true;

            GL.GenVertexArrays(1, out vaoID);
            GL.BindVertexArray(vaoID);

            GL.GenBuffers(1, out vboID);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vboID);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw);

            GL.GenBuffers(1, out iboID);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw);

            vertexShaderID = GL.CreateShader(ShaderType.VertexShader);
            GL.ShaderSource(vertexShaderID, vertsrc);
            GL.CompileShader(vertexShaderID);

            fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader);
            GL.ShaderSource(fragmentShaderID, fragsrc);
            GL.CompileShader(fragmentShaderID);

            programID = GL.CreateProgram();
            GL.AttachShader(programID, vertexShaderID);
            GL.AttachShader(programID, fragmentShaderID);
            GL.LinkProgram(programID);

            // Loading texture from embedded resource
            texsrc = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial_05.example.png"));
            textureID = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, textureID);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texsrc.Width, texsrc.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);

            System.Drawing.Imaging.BitmapData bitmap_data = texsrc.LockBits(new Rectangle(0, 0, texsrc.Width, texsrc.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texsrc.Width, texsrc.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0);

            texsrc.UnlockBits(bitmap_data);

            GL.Enable(EnableCap.Texture2D);
            GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(texcoords.Length * Vector2.SizeInBytes), texcoords, BufferUsageHint.StaticDraw);

            GL.BindAttribLocation(programID, 0, "position");
            GL.BindAttribLocation(programID, 1, "textureCoords");
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            GL.Viewport(0, 0, ClientRectangle.Width, ClientRectangle.Height);
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.Escape))
            {
                Exit();
            }
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            // Prepare for background
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.ClearColor(Color4.Red);

            // Draw traingles
            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);
            GL.BindVertexArray(vaoID);
            GL.UseProgram(programID);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vboID);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture3D, textureID);

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID);
            GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
            GL.DisableVertexAttribArray(0);
            GL.DisableVertexAttribArray(1);

            SwapBuffers();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            
            GL.DeleteVertexArray(vaoID);
            GL.DeleteBuffer(vboID);
        }
    }
}

I can not remember where do I add GL.Uniform2();

Edited by Jens Eckervogt
PS:

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!