Search code examples
delphiopenglprojective-geometryglscene

Make texture fit 4-point polygon/plane with GLScene or OpenGL for Delphi


I am using the latest GLScene trunk for Delphi XE2 and I have worked on a GUI-ish type of project which uses 3D instead of 2D to draw GUI elements on screen.

It works, but the graphics placement isn't pixel-perfect for obvious reasons.

I have managed to map the 3D view so that 1 pixel on TGLSceneViewer is more or less 1 pixel on screen. That's good enough for me.

However I am giving up the idea of using 3D for my GUI and I will instead focus on using GLScene for2D graphics-representation exclusively since it is GPU accelerated.

Then I remember using Projective Transformation in a previous project where I used Graphics32 for my GUI.

With projective transformation I could simply plot points in a 3D space, read the 3D-coordinates of those points, convert the coordinates to 2D-coordinates and apply those to the TBitmap32 Projective Transformation parameters, and viola, I had 3D capabilites in my software.

So, my question is:

Is it possible to "stretch" a texture in GLScene or OpenGL so that the texture fits between 4 points, or within a plane, so that whenever I move the corner of a plane to a new position, the texture will be stretched accordingly?

That way I could use GLScene for 2D graphics, but do some simple trickery to make things look like it's 3D.

The link below is pointed to an image on the internet which is a visual representation of what I am looking for: https://i.sstatic.net/hDdqa.png


Solution

  • assuming you are not implementing something crazy in your shaders, or that you are using fixed function pipeline functionality this should be fairly trivial.

    Texture coordinates in OpenGL are always normalized, which means they fall with in the range of [0-1];

    So basically as long as when you are drawing your objects you always set the coordinates of the texture to be maximum 1 and minimum 0 you should always have the texture filling your quad.

    Could you possibly provide the code you are using to draw your 4 points? I might be able to help with that if I can see it.