Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 12

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

  1. #1
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Calgary, Alberta, Canada
    Posts
    72

    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)

    Code :
    #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, &amp;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(&amp;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; 
    }

  2. #2
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Calgary, Alberta, Canada
    Posts
    72

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

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

    Code :
    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();
     
    }

  3. #3
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Calgary, Alberta, Canada
    Posts
    72

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

    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.

    Code :
       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)

  4. #4
    Intern Contributor
    Join Date
    Oct 2004
    Location
    Thailand
    Posts
    83

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

    You can use glBindTexture to animated textures in the scene, but you must have 2 images...
    Put AI system into human robot.

  5. #5
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Calgary, Alberta, Canada
    Posts
    72

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

    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)

    Code :
    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:
    Code :
    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:
    Code :
    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?

  6. #6
    Intern Contributor
    Join Date
    Oct 2004
    Location
    Thailand
    Posts
    83

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

    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
    Put AI system into human robot.

  7. #7
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Calgary, Alberta, Canada
    Posts
    72

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

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

  8. #8
    Intern Contributor
    Join Date
    Oct 2004
    Location
    Thailand
    Posts
    83

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

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

    use glTexGenfv
    Put AI system into human robot.

  9. #9
    Intern Contributor
    Join Date
    Jun 2000
    Location
    Calgary, Alberta, Canada
    Posts
    72

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

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

  10. #10
    Intern Contributor
    Join Date
    Oct 2004
    Location
    Thailand
    Posts
    83

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

    Why you not build the inner matrix?

    this:-
    glPushMatrix();
    ...Draw triangle
    glPushMatrix();
    ...TexGen and transform textures here
    glPopMatrix();
    glPopMatrix();
    Put AI system into human robot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •