badtaste's picture

[Solved] Simple texture attempt not giving expected results

I'm very new to this so please bear with me. I'm trying to make use of the GLControl form control to display a simple, textured quad. I've tried to cobble together some code using a few examples I have found. However, I can't seem to get the texture to appear. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK;
using System.Drawing.Imaging;
namespace TextureTest
    public partial class Main : Form
        private bool loaded = false;
        int texture;
        public Main()
        private void GLControl_Load(object sender, EventArgs e)
            loaded = true;
            Bitmap bitmap = new Bitmap("stoneTexture.png");
            GL.GenTextures(1, out texture);
            GL.BindTexture(TextureTarget.Texture2D, texture);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
        private void SetupViewport()
            GL.Viewport(0, 0, GLControl.Width, GLControl.Height);
            double aspect_ratio = Width / (double)Height;
            OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64);
            GL.LoadMatrix(ref perspective);
        private void GLControl_Resize(object sender, EventArgs e)
            if (!loaded) return;
        private void GLControl_Paint(object sender, PaintEventArgs e)
            if (!loaded) return;
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Matrix4 lookat = Matrix4.LookAt(-2, 0, -3, 1, 1, 1, 0, 1, 0);
            GL.LoadMatrix(ref lookat);
            GL.BindTexture(TextureTarget.Texture2D, texture);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(1, 0, 0);
            GL.Vertex3(1, 1, 0);
            GL.Vertex3(0, 1, 0);

And here is the result:

As you can see it is not very...textured. I'm sure I'm doing something that is very obviously wrong. I just can't see it. Any ideas?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
winterhell's picture

You need to supply texture coordinates
GL.TexCoord2(0,0); GL.Vertex3(0, 0, 0);
GL.TexCoord2(1,0); GL.Vertex3(1, 0, 0);
GL.TexCoord2(1,1); GL.Vertex3(1, 1, 0);
GL.TexCoord2(0,1); GL.Vertex3(0, 1, 0);

badtaste's picture

That did the trick! Thank you very much.