I have the following code and want to add 2D texture mapping rotation. (Rotate the texture not the model)
This code displays a texture map in quadrant zero of the display area… ie top right quadrant.
My goal is to paste this texture onto a 2x2 polygon centered about the origin. Rotate the texture but have the polygon remain fixed at the origin with an image of the texture map rotating within this polygon… pixels outside of the texture map should be blue (as that is the border color of the texture map)
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#define iWidth (16+2)
#define iHeight (16+2)
static GLubyte image[iHeight][iWidth][3];
static GLuint texName;
void makeImage(void)
{
int s, t;
for (s=0; s < 18; ++s)
{
for (t=0; t < 18; ++t)
{
image[t][s][0] = 0;
image[t][s][1] = 0;
image[t][s][2] = 255;
}
}
for (s=1; s < 17; s++)
{
for (t=1; t < 17; t++)
{
image[t][s][0] = (GLubyte) ((s-1) * 17);
image[t][s][1] = (GLubyte) ((t-1) * 17);
image[t][s][2] = (GLubyte) (1);
}
}
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 1, GL_RGB, GL_UNSIGNED_BYTE, image);
glEnable(GL_TEXTURE_2D);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
glTranslatef(0.0,0.0,-3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}