Tuesday, October 19, 2021

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 library, so you can very easily edit it to your liking. First, we also need to include iostream in the project.


#include <iostream>


Then, inside the texture function from the last post, we will need to change the parameters.


int texture(char *filename, int size)


The size integer is the width and height of the square image, but you can also replace it with two ints for the width and height, just remember to change all the size variables in the code though.

Next, we want to assign a few variables.


unsigned char * data;

unsigned char R, G, B;

FILE * file;


And then import and read the file.


file = fopen(filename, "rb");

if (file == NULL)return 0;

data = (unsigned char *)malloc(size * size * 3);

fseek(file, 128, 0);

fread(data, size * size * 3, 1, file);

fclose(file);


After, we want to convert the RGB values into usable stuff.

int index;

for (int i = 0; i < size * size; ++i)

{

index = i * 3;

B = data[index]; G = data[index + 1]; R = data[index + 2];

data[index] = R; data[index + 1] = G; data[index + 2] = B;

}


And finally, replace the last parameters and gl related stuff with this:

glGenTextures(1, &activeTexture);

glBindTexture(GL_TEXTURE_2D, activeTexture);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, size, size, GL_RGB, GL_UNSIGNED_BYTE, data);


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);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);


free(data);

return 0;


It will be used the same way as the texturing in the last tutorial, except with different parameters like this:


glEnable(GL_TEXTURE_2D);

texture((char *)"cobbled.bmp", 64);

rectangle dirt1 = { 0, 0, 1, 1, 1.0, 0.0, 0.0 };

renderRectEntity(dirt1, false);

glDisable(GL_TEXTURE_2D);


cobbled.bmp is a 64x64 texture.

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.

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...