Frage

I have the following vertices

private float vertices[]={
            -1.0f,0.0f,0.0f,  //V1 Bottom left
            -1.0f,1.0f,0.0f,   //V2 top left
            0.0f,0.0f,0.0f,   //V3 bottom right
            0.0f,1.0f,0.0f     //V4 top right
    };

And the following code in OnSurfaceChanged

if(height==0)//prevent a divide by zero by making height equal 1
    {
        height=1;
    }

    gl.glViewport(0,0,width,height);  //Reset the current viewport
    gl.glMatrixMode(GL10.GL_PROJECTION); //select the projection matrix
    gl.glLoadIdentity();            //Reset the Projection Matrix

    //calculate the  aspect ratio of the window
    GLU.gluPerspective(gl,45.0f,(float)width/(float)height,0.1f,100.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);//select the modelview matrix
    gl.glLoadIdentity();                //Reset the modelview matrix

However if I run my code I can not see the square on the screen until I zoom out using

gl.glTranslatef(0.0f,0.0f,-4.0f);

I can now see the square but it is a few pixels off the left hand side of the screen and quite a way from the top (I would assume the vertices would mean it would be placed in the top left corner of the screen). Also why do I need to zoom out in order to see anything?

Now I am under the impression that the top of the screen is 1.0f and the bottom is -1.0f and the left of the screen is -1.0f and the right is 1.0f. Is this correct?

If this is correct then how do we handle rectangular screens?

All I want to do is place a small square at the top left corner of my phones screen.....

War es hilfreich?

Lösung

Ok, I looked at more examples and realised that they all use gl.glOrthof() rather than GLU.gluPerspective()

So I replaced the GLU.gluPerspective() line with the following

gl.glOrthof(0f,1f,0f,1f,-1f,1f);

Then I changed the vertices to the following

private float vertices[]={
            1.0f,0.0f,0.0f,  //V1 Bottom left
            0.0f,0.0f,0.0f,   //V2 top left
            1.0f,1.0f,0.0f,   //V3 bottom right
            0.0f,1.0f,0.0f     //V4 top right
    };

This then renders the square full screen (but distorted) however it is pixel perfect so all I need to do now is alter the vertices to take into account the aspect ratio of the screen.

Andere Tipps

You are sort of correct with respect to the corner coordinates. One thing that you are missing, however, is that those coordinates are known as Normalized Device Coordinates.

Those are the coordinates that you draw in after ModelView and Projection transformation and perspective division. Now, if you use an identity projection matrix you can actually draw in this coordinate space directly. However, your gl.glTranslatef (0.0f, 0.0f, -4.0f) call will cause you major trouble, as that will push everything 3 units behind the near clipping plane and nothing will be drawn.

Another thing to keep in mind is that those corners actually represent the corners of your viewport. So, if you want to draw to a specific square in your window you can do this simply by setting glViewport (...) accordingly with an identity projection matrix and NDC coordinates.

That is, if you want a square that goes from 0,0 to 64,64 in your window you can do this:

glViewport (0,0,64,64);

gl.glMatrixMode(GL10.GL_PROJECTION); //select the projection matrix
gl.glLoadIdentity();                 //Reset the Projection Matrix

gl.glMatrixMode(GL10.GL_MODELVIEW); //select the modelview matrix
gl.glLoadIdentity();                //Reset the modelview matrix

private float vertices[]={
        -1.0f,-1.0f,-1.0f,  //V1 Bottom left
         1.0f,-1.0f,-1.0f,  //V2 top left
        -1.0f, 1.0f,-1.0f,  //V3 bottom right
         1.0f, 1.0f,-1.0f   //V4 top right
};

...

However, your polygon will be wound unconventionally if you do this. You should try to draw in counter-clockwise order as much as possible, because that is the default front-face winding.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top