# The Industry's Foundation for High Performance Graphics

1. ## I should see a Square, instead of a Rectangle...

Hello.

I want to draw a Square on my OpenGL application.

So, If I have the X,Y coordinates of the top-left vertex and the lenght of the sides (which are all the same for a square), with a very simple math I can find the other vertices in the 2D space.

Code :
```// Vertex is a simple struct with a constructor taking 2 arguments: X and Y of the vertex
// In this case I set the topLeft vertex to have [COLOR="#FF0000"]X = 0[/COLOR], [COLOR="#008000"]Y = 0.5f[/COLOR]
Vertex topLeft{ 0.0f, 0.5f };

//side is the lenght of the square's sides
GLfloat side = 0.5f;

// DrawTriangle is a function that takes 3 vertices (each of those has a x,y coordinate) and draws them on the application
// 1st argument: the topLeft vertex that we've already created
// 2nd and 3rd agument: the other two vertices
DrawTriangle(window, topLeft, Vertex{ topLeft.x + side, topLeft.y }, Vertex{ topLeft.x + side, topLeft.y - side });```

This code will draw half square. But not exactly as I expected.

This is the result: (attachment)

Now let's draw the other half square:

Code :
`DrawTriangle(window, topLeft, Vertex{ topLeft.x, topLeft.y - side }, Vertex{ topLeft.x + side, topLeft.y - side });`

Aaaand, the result is rectangle, not a square

Is my math wrong?

2. The code you have shown us really isnt enough to give you any kind of feedback. Especially because you didnt show us any OpenGL code but code using some kind of framework.
If I had to guess I would say you are using the old deprecated rendering pipeline and you have forgotten to set up your matrices.

3. Originally Posted by Cornix
The code you have shown us really isnt enough to give you any kind of feedback. Especially because you didnt show us any OpenGL code but code using some kind of framework.
If I had to guess I would say you are using the old deprecated rendering pipeline and you have forgotten to set up your matrices.
Ok sorry.

This is the DrawTriangle() function that I use above:

Code :
```void DrawTriangle(SDL_Window* Win, Vertex v1, Vertex v2, Vertex v3)
{
Vertex vertices[3] { v1, v2, v3 };

glGenBuffers(1, &vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}```

4. You should take into account that clip space coordinates don't change ([-1;1] on all 3 axis) while the window's aspect ratio changes.

5. Originally Posted by Spoops
You should take into account that clip space coordinates don't change ([-1;1] on all 3 axis) while the window's aspect ratio changes.
Sorry, I don't get what you wanna say.

How can I solve that problem?

6. I meant that OpenGL doesn't care what size your window is, the coordinates still range from -1 to 1 on the whole window.

7. Originally Posted by Spoops
I meant that OpenGL doesn't care what size your window is, the coordinates still range from -1 to 1 on the whole window.
I know. But even though I just draw a triangle with these coordinates:

First vertex:
{0, 0.5f}

Second vertex:
{ 0.5f, 0.5f }

Third vertex:
{ 0.5f, 0 }

OpenGL draws this:

As you can see, the two catheti aren't equal but they should be

8. They are equal. But the X-Axis has a different scaling factor.

If your Window is 500 pixels in width and your rectangle is 0.5 length units in width with the bounds going from -1.0 to 1.0 then that is 125 pixels (=500/4).
If your Window is 300 pixels in height and your rectangle is 0.5 length units in height with the bounds going from -1.0 to 1.0 then that is 75 pixels (=300/4).
Its 0.5 length units on both axis, but the results in pixels are different.

OpenGL doesnt care about pixels. You are supposed to set up your own coordinate system in the way that you want to use it.

9. Originally Posted by Cornix
They are equal. But the X-Axis has a different scaling factor.

If your Window is 500 pixels in width and your rectangle is 0.5 length units in width with the bounds going from -1.0 to 1.0 then that is 125 pixels (=500/4).
If your Window is 300 pixels in height and your rectangle is 0.5 length units in height with the bounds going from -1.0 to 1.0 then that is 75 pixels (=300/4).
Its 0.5 length units on both axis, but the results in pixels are different.

OpenGL doesnt care about pixels. You are supposed to set up your own coordinate system in the way that you want to use it.
How do I well render my geometry independently from the size of the Window?

10. Choose correct positions or send a matrix in your vertex shader to rescale/translate primitives.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•