Why isn't this a square? LWJGL

后端 未结 2 936
自闭症患者
自闭症患者 2021-01-12 09:54

I have a basic LWJGL window set up and I am trying to draw a square using the glBegin(GL_QUADS) method. Square square = new Square(25, 25, 25), is

2条回答
  •  粉色の甜心
    2021-01-12 10:17

    To draw a square around the point (x | y) you can calculate the four points that represent the corners of your square.

    First you'll need your width to height ratio

    float ratio = width / height
    

    I will use a defaultSize for the length of the shortest path from the midpoint to any of the sides.

    Then you can calculate four values like so:

    float a = x + defaultSize 
    float b = ratio * (y + defaultSize) 
    float c = x - defaultSize 
    float d = ratio * (y - defaultSize)
    

    with which you can represent all four corners to draw your square with. Since GL_SQUAD is deprecated I'll use GL_TRIANGLE.

    glBegin(GL_TRIANGLES);
    glColor3f(red, green, blue);
    
    // upper left triangle
    glVertex2f(a, b);
    glVertex2f(c, b);
    glVertex2f(c, d);
    
     // lower right triangle
    glVertex2f(a, b);
    glVertex2f(c, d);
    glVertex2f(a, d);
    
    glEnd();
    

    I don't know if this is the most performant or idiomatic way to do this since I just started exploring LWJGL.

提交回复
热议问题