You are on page 1of 2

#include <glut.

h>
//Create Texture:
#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLuint texName;
void makeCheckImage(void) //Assign color to each bit
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++)
{
for (j = 0; j < checkImageWidth; j++)
{
c = (((i&0x8)==0)^((j&0x8))==0)*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) 255;
}
}
}
void set_texture_properties()
{
makeCheckImage(); //Create texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);//set pixel storage modes (define
packing of pixels into memory)
glGenTextures(1, &texName); //generate texture names: 1st param n is nu
mber of textures to store, 2nd param is array in which the generated texture nam
es are stored.

glBindTexture(GL_TEXTURE_2D, texName); // lets you create or use a named
texture
//Options for 1st arg can be: GL_TEXTURE_1D, GL_TEXTURE_2D,GL_TEXTURE_3D,GL_T
EXTURE_1D_ARRAY,GL_TEXTURE_2D_ARRAY,GL_TEXTURE_RECTANGLE etc.
//Set Texture Parameters using function:- glTexParameteri(targer, paramN
ame, paramList or value)
//For further Details: http://www.opengl.org/sdk/docs/man/xhtml/glTexPar
ameter.xml
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //Sets the
wrap parameter for texture coordinate s
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Sets the
wrap parameter for texture coordinate t
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //tex
ture magnification function
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); //tex
ture minifying function
//set texture environment parameters:http://www.opengl.org/sdk/docs/man2
/xhtml/glTexEnv.xml
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//specify a two-dimensional texture image. Reference: http://www.opengl.
org/sdk/docs/man/xhtml/glTexImage2D.xml
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,checkImageHeight
, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
//Also check function:- glTexGeni: controls the generation of texture co
ordinates, Ref: http://www.talisman.org/opengl-1.1/Reference/glTexGen.html
glEnable(GL_TEXTURE_2D); //Enable texture mapping
}
void define_object()
{
glColor4f(1.0, 0.0, 0.0, 1.0);
glBegin(GL_QUADS); //set object vertices and texture coordinates
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
set_texture_properties();
define_object();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 400);
glutInitWindowPosition(50,50);
glutCreateWindow ("Checkbox Texture Mapped on Quads");
glClearColor(0.5, 0.5, 0.5, 1.0);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like