문제

I'm trying to draw rectangles with a certain distance in x-axis to each other. The distance on each rectangle should be

(screen width - rectangle width)

so that only one rectangle can be fully displayed on the screen at a time. And if I drag the first rectangle to left, the portion of that rectangle not displayed (or cut) on the screen would be the amount displayed on second rectangle .

The problem is that I could not figure out how to calculate the ratio of my rectangle's width to screen width. My rectangle vertices are

final float[] rectangleVerticesData = {
        // X, Y, Z
        -1.0f, 2.0f, 0.0f,            
        -1.0f, -2.0f, 0.0f,            
        1.0f, 2.0f, 0.0f,            
        -1.0f, -2.0f, 0.0f,            
        1.0f, -2.0f, 0.0f,            
        1.0f, 2.0f, 0.0f
};

Based above, my rectangle width is 2. I am using a tablet with 1280 pixels width. Using the formula above, I got

1280 - 2 = 1278

When I ran the program, the expected output is not as what I am trying to achieve. When I drag the first rectangle, the next rectangle is not displayed even if the first rectangle is fully dragged to left off the screen.

When I tried hard-coding distance to 10, the rectangles are displayed with that distance. I guess 1278 is so long to be the distance between them. How can I calculate the correct distance between the triangles based on screen width so that only one rectangle can be fully displayed at a time on the screen? Any help would be greatly appreciated. Thank you.

도움이 되었습니까?

해결책

In OpenGL, the screen is considered as a quad of normalised size ie, it extends from vertex (-1,-1) to vertex (+1,+1). The vertex input given to OpenGL for drawing does not range according to the screen pixel size, but is converted internally for drawing to actual screen size. Since there are many devices with varying screen sizes, to make the GL code run across all devices without hardcoding screen sizes, this is required.

So you will have to scale your calculations accordingly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top