gronzzz
gronzzz

Reputation: 615

Rotate texture openGL c++

when i'm making a texture from openCV image, it's always rotated on 180, why is this ? There is my code, which bind texture and display this on screen. If code norm, suggest me how to rotate texture properly, i can't get it .

glBindTexture( GL_TEXTURE_2D, slice.texture);
glLoadIdentity();
glEnable(GL_TEXTURE_2D); //enable 2D texturing

glBegin (GL_QUADS);

float nullX = slObj->rect.x/400.0;
float nullY = slObj->rect.y/300.0;

float sliceWidth = slObj->rect.width/400.0;
float sliceHeight = slObj->rect.height/300.0;

//with our vertices we have to assign a texcoord
//so that our texture has some points to draw to
glTexCoord2d(0.0,0.0); glVertex2f(nullX, nullY);
glTexCoord2d(1.0,0.0); glVertex2f(nullX + sliceWidth, nullY);
glTexCoord2d(1.0,1.0); glVertex2f(nullX + sliceWidth, nullY + sliceHeight);
glTexCoord2d(0.0,1.0); glVertex2f(nullX, nullY + sliceHeight);
glEnd();
glFlush();

UPDATE:

// initialize
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glClearColor(0.3,0.3,0.3,0.0);
glMatrixMode(GL_PROJECTION); 
glOrtho(-400.0 ,400.0 ,-300.0 ,300.0 ,0 ,1.0); 

Upvotes: 1

Views: 1310

Answers (2)

gronzzz
gronzzz

Reputation: 615

as i used openCV for load and slice image, i simply flip image.

IplImage *source = cvLoadImage("space.png",1);
if(source == NULL) source = cvLoadImage("C://space.png",1);

cvFlip(source, source, 0);

Thanks all for your help!

Upvotes: -1

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

How is your projection matrix setup?

Those coordinates are correct assuming you have a traditional projection matrix where the Y-axis increases from the bottom to top of the screen. If, on the other hand, you reversed your projection matrix so that (0,0) is effectively the top-left corner of your screen then you have a problem.

If this is the case, the texture is not really rotated, it is mirrored. There is no rotation that can produce such a situation, it is what is known as a change of chirality (also known as handedness). You can either use a traditional projection matrix where the Y-axis behaves as described earlier, or compensate when you compute your texture coordinates by flipping the second texture coordinate (T).

Upvotes: 1

Related Questions