0

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);   
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
WildWorld
  • 517
  • 1
  • 5
  • 18

1 Answers1

2

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

Community
  • 1
  • 1
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
  • this is working great. can you tell me where do i change the background color ? now i change the model color. thank you – WildWorld Feb 08 '13 at 11:08
  • 1. use Gl.glOrtho(-5.0, 5.0, -5.0, -5.0, -1.0, 1.0); to setup a orthogonal projection. 2. Draw a fullscreen quad and use the background texture. – Vertexwahn Feb 08 '13 at 11:55