# OpenGL How would I draw a line between two world-coordinate points using OpenGL?

This topic is 1062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, hopefully you can help me understand how to approach this line drawing question I have.

I have a basic 3D demo set up drawing lines with OpenGL using VAO's and shaders, and a first person camera to move around.

Key snippets:

// Example vertices
float[] verts = {
1f, 1f, 1f,
0f, 0f, 0f
};

// Draw call
glBindVertexArray(vaoID);
glDrawArrays(GL_LINES, 0, 2);


?Vertex shader is basic, no model matrix in use at the moment:

gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0


This outputs the line drawn properly in the 3D world, but the start/end points are based on the vertex positions only which isn't too useful.

My question is: how can I specify the start and end locations in my world coordinates and draw a line between those points? My first thought was about transforming each vertex by a different model matrix, but I asked this earlier also on stackoverflow and had a comment saying:

"You'd apply the same transformation to both points. The transformation matrix needs to contain a scaling to (p2-p1) first and then a translation to p1(x,y,z). Oh and you chose the vertices of (0,0,0) and (1,1,1) correctly."

Unfortunately that reply just left me even more confused about how to do this. How would the scaling work with the original vertices? If I need to move vertex 1 to 20x, 20y, 20z and vertex 2 to 20x, 100y, 100z then I'm not clear how this would work, and how the original vertices relate to the final world coordinates I want.

I'd really appreciate any help with what I hope is a simple question. I'm using Java and LWJGL if that affects anything, but I think the concept will be the same regardless of language.

Thanks!

##### Share on other sites
The stackoverflow reply sounds fine to me.

If you imagine your two vertices are a model, your 0,0,0 is the "center" of the model and isn't changed by scaling.

The 0,0,0 point stays at 0,0,0 no matter how much you scale it, and then moves exactly by your translation amount.
The 1,1,1 point first moves to a point equal to the scaling (x,y,z), and then has the translation added to the result.

Therefore, translation controls your 0,0,0 point's final location and scaling only controls the 1,1,1 point's location relative to wherever your 0,0,0 point is.

Point A = 0,0,0
Point A transformed = (0,0,0) * (scaling) + (translation)
// (0,0,0) * scaling = (0,0,0), so this simplifies to:
Point A transformed = (0,0,0) + (translation)
Point A transformed = (translation)

Point B = 1,1,1
Point B transformed = (1,1,1) * (scaling) + (translation)
// (1,1,1) * scaling = scaling, so this simplifies to:
Point B transformed = (scaling) + (translation)

So this means you can control the exact location by using:
Point A transformed = Desired point A location
Point B transformed = Desired point B location

Substituting:
Desired point A location = translation

Desired point B location = (scaling) + (translation)

Subtracting translation from both sides:
Desired point B location - (translation) = (scaling)

Substituting:
Desired point B location - Desired point A location = scaling Edited by Nypyren

##### Share on other sites

Thanks so much, that was exactly the explanation I needed to understand what was going on, and of course it makes total sense as soon as I read over your post :) Seems to be working great.

##### Share on other sites

Worth pointing out that your terminology is confusing. Since you aren't applying a model matrix anywhere, your points *are already* in world space (not necessarily what you think of as 'world coordinates' right now).

In general it's all much simpler if you setup your camera transform such that OpenGL's world space is the same as your world coordinates, or you are in for a world of confusion down the line.

##### Share on other sites

Thanks for the pointer swiftcoder. I think I'm a little mixed up on the terms then, as the things I've been reading talk about "world space" meaning after it's been transformed by the model matrix so that it's world coordinate position / rotation / scale is taken into account, but it sounds like that's not the correct way to think about it then?

Could you elaborate on what you meant with your second point about setting up the camera transform in a way to avoid later confusion? Thanks!

##### Share on other sites

as the things I've been reading talk about "world space" meaning after it's been transformed by the model matrix

Yes, that's a reasonable way to look at it. And since your original post doesn't use a model matrix, therefore your points are being specified in world space.

Or, in other words, the answer to your question "how can I specify the start and end locations in my world coordinates and draw a line between those points?" is that you are already doing so.

Could you elaborate on what you meant with your second point about setting up the camera transform in a way to avoid later confusion?

What I mean is that there is no particular requirement that your game world actually correspond to OpenGL's world space. For example, a large open-world game may experience floating point precision problems if placed in standard world space, in which case they'll have to move the origin of OpenGL's world space around in their game world.

From your post, I had reached the conclusion that you were trying to draw your line in some different world coordinate space, and my point was that it is much simpler if you don't introduce additional coordinate spaces, and just treat OpenGL's world space as your word coordinates.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633708
• Total Posts
3013468
×