I've been trying to learn some OpenGL this past couple of weeks. It's for my own interest, and just a 2D program I would like to create. I should also add that my maths is not great.

I want to display lots of images on the screen at once, each being moved and rotated, and all in 2D. Each image is just two triangles, with the image texture. All pretty basic stuff (or should be!).

After much Googling over the past couple of nights, I've worked out how to convert from the OpenGL co-ordinates to screen co-ordinates. However when I do so, and then rotate an image, squares become rectangles as the degree of rotation moves closer to 180. I've determined that it's because of rectangular pixels, but I just can't work out what I need to do to adjust for the pixel size/shape. I apologise because I know that co-ordinate conversion must be asked so often, but I've had so much trouble finding a solution in part because so many answers are non-VAO code.

The code below is my test code, and all it does is display a blue square and rotates it on the Z-axis. (I realise that GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX are deprecated and haven't yet looked into what they should be replaced with, but if it's a quick answer then it would also be appreciated.)

After working at this some more tonight, I realise that of course I should be making the co-ordinates conversion in the shader after the transformation, and not before buffering the co-ordinates. So I believe gluUnProject is for code which does not use VAOs/VBOs, and so my ScreenToOglPositions function should not be used and instead I should be passing matrices to the shader using glm. I used to pass in two other matrices and have added them again (alongside the model matrix), but they don't convert the screen co-ordinates to OpenGL co-ordinates and I wonder: is there another matrix I need to pass in to make the conversion?

(I should really just multiply the projection/view/model matrices before passing them to the vector.)

Tim

timtomtoo

10-18-2012, 06:39 AM

(I don't know if posts should be edited when replying to one's own posts, or a new post added. And I couldn't see any rules regarding this. So if this should have been an edit: sorry!)

I've realised where I went wrong. The first thing was of course, what I referred to last night just before going to bed. I was using a now deprecated non-VAO method in a VAO context. So this morning I took a look at the documentation for gluUnProject, and noticed it had the computation detailed there. So I've taken that and used it to make the calculation in the vertex shader, using an inverse matrix calculated using glm and passed in as a uniform value.

That was all pretty easy (assuming I got it right).

I've spent the remainder of the day trying to work out why rotation causes so much flicker, and translation causes the vertices to disappear. And it took me hours to realise my second mistake, which was that I don't want to rotate or translate any vertices which I display at a fixed point on the screen. So ... that was a big chunk of time wasted!

However, if there is a way to rotate (without flicker) and translate these elements around the screen, then it would be great if someone could describe how. It could be useful for animated interface controls.

In case there is any other beginner looking how to position something in screen-coordinates, I've attached the code below. It doesn't really change much from the code in my first post, but rather does it correctly (I think).