Texturing It
Now, we add textures to out objects. It's basically the act of wrapping an image around an object. This is the first tutorial where I used two classes. Reading in a file can be a little messy so I tried to keep that separate. There are a lot more setting with textures so there will be a lot to do.
- Read in the image file: this really is the hardest part
- The image file has to be read in a byte area so the RGBA data can be extracted.
- The code for this is in the MyTexture class in the function getRGBAPixelData.
- getRGBAPixelData(BufferedImage img)
- The next step is getting an ID for this image
- glGenTextures(1, [ int_id ], 0);
- Then you need to bind the id to the type of texture you are using
- glBindTexture(GL.GL_TEXTURE_2D, int_id);
- Now you need to load the Texture information into OpenGL.
- glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, imageHidth, imageHieght, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, RGBA_Data);
- This is a beast of a function so lets go through the parameters
- GL_TEXTURE_2D is the target of information. When this texture is used, it will target the texture of the object
- This is the level of the image. This only comes into play when you are mipmapping. More on this later.
- GL_RGBA is the format of the texture. There more then a dozen different choices for this param. I encourage you to check the other ones out, but for the basic texturing RGBA will work just fine.
- The width of the image in pixels
- The height of the image in pixels
- The width of the border. The texture I used has no border so I used zero. Some images have a 1-pixel border around them and you would put 1. Those are your two choices.
- The second GL_RGBA is the format of the incoming data for the texture. Your choices here a slightly more limited but GL_RGBA is what you want for now.
- GL_UNSIGNED_BYTE is the type of data structure you are using to send the image data into OpenGL.
- and now the image data.
- OpenGL Documentation for this function.
- Setup the global settings
- Set up the texture environment.
- glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
- Set up the filter.
- glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); - Set how the wraps.
- glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); - Set how the texture is mapped to the object.
- glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, GL.GL_SPHERE_MAP);
glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, GL.GL_SPHERE_MAP); - Attach to object
- Before you draw the object you have to load the texture you want it drawn with. The following code will do that, just make sure you have the right id.
- glBindTexture(GL.GL_TEXTURE_2D, int_id);
