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.
// 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 X = 0, Y = 0.5f
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)
[ATTACH=CONFIG]1367[/ATTACH]
Now let’s draw the other half square:
DrawTriangle(window, topLeft, Vertex{ topLeft.x, topLeft.y - side }, Vertex{ topLeft.x + side, topLeft.y - side });
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.
[QUOTE=Cornix;1281925]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.[/QUOTE]
Ok sorry.
This is the DrawTriangle() function that I use above:
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.
[QUOTE=Cornix;1281942]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.[/QUOTE]
How do I well render my geometry independently from the size of the Window?
You need to make sure your aspect ratio is 1:1 like this: When you setup your rendering context setup the aspect ratio last:
// Grab hold of this windows Device Context
if ((hDC = GetDC(handle)) == NULL) return 0;
// Use the Device Context to get the graphics cards' pixel format
if (!SelectPixelFormat(hDC)) return 0;
// Create the OpenGL Rendering Context
if ((hRC = wglCreateContext(hDC)) == NULL) return 0;
// Next we select the Rendering Context and make it the current Rendering Context
if (!wglMakeCurrent(hDC, hRC)) return 0;
// Finally we setup the projection and viewport BEFORE we initialise OpenGL
ResizeViewport(800, 600);
My ResizeViewport function looks like this:
// This function resizes the screen display to the requested size
void ResizeViewport(int width, int height)
{
// Prevent the aspect ratio from dividing by zero
if (height == 0) { height = 1; }
// ****** STAGE 1: CALCULATE THE VIEWPORT TRANSFORMATION ******/
glViewport(0, 0, width, height);
// ****** STAGE 2: CALCULATE THE PROJECTION TRANSFORMATION ******/
UpdateProjection(45.0f, (float)width, (float)height, 1.0f, 100.0f);
}
Then in update projection I update the aspect ratio and setup my projection matrix like this: