Yaminichi's picture

Detailed Explanation of Loading Textures

Hey everyone,

So recently, I've been trying to learn OpenTK for use in a 2D C# game (Grade 12 AP programming final project). So far, from the documentation on this site, I have learned absolutely nothing. It pains me to say, but the guides on this site are probably up in my top 5 worst written guides of all time. They explain next to nothing about how any individual function works and it honestly made me feel as though the person who wrote them thought "Hey, maybe if I just throw a bunch of code at people, they'll understand how everything works!"

With that in mind, I have a problem that no guide seems to actually address properly: the issue of loading textures onto the screen. If someone could write a detailed guide as to how to load and display textures using only OpenTK and C#, I would greatly appreciate it. And by detailed guide, I mean something that literally explains every function and parameter used. As some background, I'm currently able to draw textureless quads to the screen using a matrix4 projection.

P.S. Sorry if I came off as rude, it's just frustrating go through literally 30+ guides and getting nothing out of them because they don't explain the functions used and why they used them.

A snippet of the OpenTK portion of the code I've been using, if anyone cares:

            //Clear back buffer
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            //Camera and Link vectors
            Vector3 linkLocation = new Vector3(Link.X, Link.Y, 4.0f);
            Vector3 camLocation = new Vector3(Link.X, Link.Y, zoomFactor);
 
            //Set camera to follow Link
            modelview = Matrix4.LookAt(camLocation, linkLocation, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);
 
            //Start drawing
            GL.Begin(PrimitiveType.Quads);
            GL.Color3(0.0f, 0.0f, 0.0f);
            GL.Vertex3(invalidCoordsX[0], invalidCoordsY[0], 4.0f);
            GL.Vertex3(invalidCoordsX[0], invalidCoordsY[1], 4.0f);
            GL.Vertex3(invalidCoordsX[1], invalidCoordsY[1], 4.0f);
            GL.Vertex3(invalidCoordsX[1], invalidCoordsY[0], 4.0f);
 
            GL.Color3(0.5f, 1.0f, 1.0f);
            GL.Vertex3(invalidCoordsX[2], invalidCoordsY[3], 4.0f);
            GL.Vertex3(invalidCoordsX[2], invalidCoordsY[2], 4.0f);
            GL.Vertex3(invalidCoordsX[3], invalidCoordsY[2], 4.0f);
            GL.Vertex3(invalidCoordsX[3], invalidCoordsY[3], 4.0f);
            GL.End();
 
            //Swap back buffer to front buffer.
            SwapBuffers();

Comments

Comment viewing options

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

Take a look at the example code here: https://github.com/opentk/opentk/blob/develop/Source/Examples/OpenGL/1.x...

In this sample, the OnLoad() method loads a System.Drawing.Bitmap into OpenGL. The OnRender() method renders the texture to screen.

OpenTK is simply a thin C# wrapper around the 2647 functions offered by OpenGL. It includes the reference OpenGL documentation from Khronos, but it does not teach you how to use those functions - that's not its purpose.

If you are interested, there is an excellent book covering modern rendering concepts with OpenTK: http://www.amazon.com/3D-Engine-Design-Virtual-Globes/dp/1568817118. It focuses on terrain rendering but its content is applicable everywhere. (Its sample code is available online here: https://github.com/virtualglobebook/OpenGlobe )

Edit: that's not to say that OpenTK documentation should not be improved, it's fact that it leaves a lot to be desired right now. Contributions are more than welcome.

Yaminichi's picture

From what you linked me, I still am left with my original question, what does each function here do/why did you choose to use it? I'll paste the groups of functions I don't understand below:

            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
 
            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.Format32bppArgb);
 
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
 
            bitmap.UnlockBits(data);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
jrwatts's picture

Do you have an OpenGL reference book of some sort (e.g., OpenGL Superbible)? As OpenTK is primarily a wrapper around OpenGL, an OpenGL reference will explain almost all of its functions.

Yaminichi's picture

I've looked for an OpenGL reference that actually makes sense for so long now. Sadly, I have yet to find one.

the Fiddler's picture

Try "the Official Guide to Learning OpenGL", also known as "the Red Book". An online version covering OpenGL 1.1 can be found here: http://www.glprogramming.com/red/. More up-to-date versions, up to OpenGL 4.3, can be bought at bookstores.

To understand what each function does, you can use either the OpenGL manpages or the OpenGL specification (included with your copy of OpenTK under the Documentation/ folder.) Also included with OpenTK is the OpenGL reference card. This is an invaluable resource, print it out and keep it close by.

Finally, a short description for each function and function parameter should also be shown by your IDE in a documentation tooltip:

// OpenGL
Hint - specify implementation-specific hints
 
// OpenGL texture specification 
GenTextures - generate texture names
BindTexture - create or use a named texture
TexParameter - assign texture parameters
TexImage2D - specify a two-dimensional texture image
 
// OpenGL matrix stack
MatrixMode - specify which matrix is the current matrix
LoadIdentity - replace the current matrix with the identity matrix
Ortho - multiply the current matrix with an orthographic matrix
 
// System.Drawing
Bitmap.LockBits - lock an existing bitmap in system memory so that it can be changed programmatically
Bitmap.UnlockBits - unlocks a Bitmap from system memory