WildWorld WildWorld - 2 months ago 16
C# Question

How to load a texture from a jpg image and display it as a background?

I am making a simple 3d program and now I need to insert an 2D image as a background. I need to use the tao framework for college. This is a part of the code. How do I load the image into an

int
array?

Gl.glEnable(Gl.GL_TEXTURE_2D);

int texture; // storage for texture for one picture
texture = ????? ;
Gl.glGenTextures(1, texture); ??????

// Create The Texture

// Typical Texture Generation Using Data From The Bitmap
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); // Linear Filtering
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

Answer Source

The texture itself is not an int array. int texture is just an unquie identifier for the texture - usually the first created texture has the id = 0, the second id = 1, etc. (its driver dependent - the ids can totally different on your system)

To specify a two-dimensional texture image you have to use Gl.glTexImage2D(...)

Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);

// some random data
Random r = new Random(); 
byte[] image = new byte[512 * 512 * 3]; 
for (int i = 0; i < 512 * 512; i++) 
{ 
       image[i * 3 + 0] = (byte)r.Next(0, 255); 
       image[i * 3 + 1] = (byte)r.Next(0, 255); 
       image[i * 3 + 2] = (byte)r.Next(0, 255); 
} 

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, 512, 512, 0, 
                Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, image); 

Instead of using random data you can also convert an image to an byte array as described here