Rotate a 2D Texture 1 step at a time please!

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

I transated the texture and the polygon now to have their centers at the origin.

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glTranslatef(+0.5, +0.5, 0.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
    glBegin(GL_QUADS);
        glVertex3f(-0.5, -0.5, 0.0);
        glVertex3f( 0.5, -0.5, 0.0);
        glVertex3f( 0.5,  0.5, 0.0);
        glVertex3f(-0.5,  0.5, 0.0);
    glEnd();

    glFlush();
    glutSwapBuffers();

}

But if I need to rotate the texture map (about the z axis) then it will no loger fit in the 1x1 square I’ve defined. So I try a 2x2 square centered about the origin.

    
   glBegin(GL_QUADS);
        glVertex3f(-1.0, -1.0, 0.0);
        glVertex3f( 1.0, -1.0, 0.0);
        glVertex3f( 1.0,  1.0, 0.0);
        glVertex3f(-1.0,  1.0, 0.0);
    glEnd();

I tried this but get strange results… (The texture is centered in the 2x2 square… I see a single blue border but the rest of the square is random colors) I expected the texture to be in the center of the square with a blue box around it. (Because I clamped to border)

You can use glBindTexture to animated textures in the scene, but you must have 2 images…

Well I’m at work now on a different computer…
and I’m getting different results.

At work on my G5 I am unable to get the blue border at all around my image. Any idea why?

I’ve changed the display code to rotate the texture and it does indeed rotate… (but no blue border)

void display(void)
{
    static float r=0.0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glTranslatef(+0.5, +0.5, 0.0);
    glRotatef(r, 0.0, 0.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glBegin(GL_QUADS);
        glVertex3f(-1.0, -1.0, 0.0);
        glVertex3f( 1.0, -1.0, 0.0);
        glVertex3f( 1.0,  1.0, 0.0);
        glVertex3f(-1.0,  1.0, 0.0);
    glEnd();
    
    glFlush();
    glutSwapBuffers();
    
    r = r + .05;
}

I am curious as to why I didn’t need to define the S and T planes
like:

  
float planeS[]={1.0f, 0.0f, 0.0f, 1.0f);
float planeT[]={0.0f, 1.0f, 0.0f, 1.0f);

Nor did I seem to need:

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, planeS);
glTexGenfv(GL_T, GL_EYE_PLANE, planeT);

When are these necessary?
Shouldn’t the planes be multiplied by some matrix in order for them to actually do something?

Ah ha you must use :-

glBegin(GL_TRIANGLES);
glTexCoord2f(U, V); glVertex3f(x1, y1, z1);
glTexCoord2f(U, V); glVertex3f(x1, y1, z1);
glTexCoord2f(U, V); glVertex3f(x1, y1, z1);
glEnd;

to transform or rotation the textures that sure you try this…

U := X Coordinate Textures
V := Y Coordinate Textures

No I do not wish to specify the texture coordinates.
I want opengl to automatically generate them for me.

You try call the TEXGEN to all scene and change parameter to animation texture into frame loop.

use glTexGenfv

Yes.
Please re-read the last question.
It says that it works… but that I didn’t need to call these functions…
Many thanks.
:wink:

Why you not build the inner matrix?

this:-
glPushMatrix();
…Draw triangle
glPushMatrix();
…TexGen and transform textures here
glPopMatrix();
glPopMatrix();

An example please of what you mean.

I’m not sure for this but I think the surely pixel position on the surface matrix.