Question

I think the 2D plane I draw on is not parallel to the screen.

First, in my resize window function I put:

static void Resize(int w, int h)
{
width = w;
height = h;

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float32 ratio = width / height;

b2Vec2 extents(ratio * 25.0f, 25.0f);
extents *= viewZoom;

b2Vec2 lower = settings.viewCenter - extents;
b2Vec2 upper = settings.viewCenter + extents;
gluOrtho2D(lower.x, upper.x, lower.y, upper.y);
}

Here is how I initialize opengl.

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize(width, height);
mainWindow = glutCreateWindow("window");

And the drawscene function:

void drawScene() {

    glClear(GL_COLOR_BUFFER_BIT );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();



    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glColor3f(1.0f, 1.0f, 1.0f);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(cords[0].x,cords[0].y);
    glTexCoord2f(1.0f, 0.0f);
    glVertex2f(cords[1].x,cords[1].y);
    glTexCoord2f(1.0f, 1.0f);
    glVertex2f(cords[2].x,cords[2].y);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2f(cords[3].x,cords[3].y);
    glEnd();

    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}

I think the code is no problem because I've already loadIdentity for ModelView and Projection. The result of my drawing is that if my rectangle is parallel to the horizontal line, then it is a rectangle, when turned some angle, then it becames skewed. Seems the plane I'm drawing on is tilted backwards.

Was it helpful?

Solution

The thing is trivial:

float32 ratio = width / height;

coz width and height are both int, thus ratio is not accurate.

If convert to float, then it becomes normal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top