1
\$\begingroup\$

Im developing a graphically very simple program (Does not require any 3D) in opengl. The main issue is that the default float positions for vertices are based on percentages of the screen.

Basicly what I want is to be able to pass 4 coordinates: x1, y1, x2, y2 and draw a quad based on those coordinates. For example if it was (0, 0, 10, 10) It would draw a 10px by 10px box in the top left.

So far shaders for transformations like pos and scale have been implemented. But they do not provide enough fine control on the quad and access to window width, height and other variables.

I don't need actual code, pseudo code is more than enough. But if you are curious im using lwjgl 3 with java in the programmable pipeline.

Is there any way of doing this with shaders? Or should I be using a completely different method?

\$\endgroup\$
0
\$\begingroup\$

You need to set your projection matrix to be orthographic and to scale the range 0-1 to fit into a single pixel. You can do this in your vertex shaders by changing the projection matrix.

I'm not familiar with lwjgl, but would assume it has some sort of matrix library. I've used the C++ library glm, and it has a method ortho for creating a 2D orthographic projection matrix. I would bet that lwjgl has a similar method. If not, you can do it by hand. Here is Wikipedia's article on how to create one.

\$\endgroup\$
  • \$\begingroup\$ LWJGL3 has joml, which is a commonly used math library for matrices and vectors. Thanks for the help. \$\endgroup\$ – codedcosmos Oct 7 '17 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.