Sunday, October 17, 2021

Creating textures using glut

 You may have been searching for hours across the internet to find out how to create textures, only using glut. I'm going to show you how:

Here is the script to do it:

int texture(unsigned char data[])

{

/*unsigned char data[] =

{

128, 128, 128, 255,

255, 0, 0, 255,

0, 255, 0, 255,

0, 0, 255, 255,

};*/


glGenTextures(1, &activeTexture);

glBindTexture(GL_TEXTURE_2D, activeTexture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);


return 1;

}


To make a texture, simply make an array like the commented out code right there and use it. To change the size, change the 2's in glTexImage2D into width and height, but you will also have to extend the array. In order to get a custom texture file, you will have to download some library or make your own image importer and import the image into the array. You can also write your own new texture type that is already ready to be imported with no conversions, but remember to also make a texture editor and viewer for it too!

Here is an example of it being used:

glEnable(GL_TEXTURE_2D);

texture(dirt);

rectangle dirt1 = { 0, -0.5, 0.125, 0.125, 1.0, 0.0, 0.0 };

renderRectEntity(dirt1, false);

glDisable(GL_TEXTURE_2D);


Note that rectangle dirt1 and renderRectEntity are special functions. They don't have to do with anything. You can replace both of them with this:

glBegin(GL_QUADS);

glVertex2d(-0.5, -0.5); glTexCoord2d(0, 0);

glVertex2d(0.5, -0.5); glTexCoord2d(1, 0);

glVertex2d(0.5, 0.5); glTexCoord2d(1, 1);

glVertex2d(-0.5, 0.5); glTexCoord2d(0, 1);

glEnd();

Now you can texture objects in glut.

No comments:

Post a Comment

Importing BMP images to use for texturing in glut and iostream

 In this post, we will be continuing from the previous post on how to make textures in glut,  here . This is going to be a custom BMP import...