- Home /
Use Gl.Lines to connect Vector3 points in world space
I have the basic idea for GL and how to render lines, but i am a bit confused on how to render in world space given Vector3 coordinates.
I know I must either use GL.modelView or MultMatrix to render them in world space.
The real use is to iterate over a list of Vector3 objects of a path of one of my units, and render a line between them. I know I want to make a vertex at my unit, and for the first point in the list. Then I loop for every other vertex in the list and make a GL.Vertex call.
Here is my current snippet (that compiles but shows no lines): Current OnPostRender code
maybe the code in this answer will help. sorry I don't have any more information.
I have seen that post, but wanted to write my own code, ins$$anonymous$$d of just blatant copying, especially since that does way more than what I want.
Plus, there is little to no explanation for that code. Thanks anyways.
Answer by Bunny83 · Jul 15, 2014 at 06:23 AM
If you want to setup the GL context for a specific camera, all you have to do is:
Camera cam;
GL.LoadProjectionMatrix(cam.projectionMatrix);
GL.modelview = cam.worldToCameraMatrix;
Now you're rendering in world space from that camera view.
edit
If your camera doesn't cover the whole screen, you also need to set the viewport so everything you draw is clipped by the viewport rect and the projection is actually centered in the viewport:
GL.Viewport(cam.pixelRect);
Hmm, both code segments seemed to solve my issue! Thanks a lot! Where did you learn GL concepts?
Can you explain or link me to a page/wiki that explains what these do? I would love to learn what this code does, rather than just "use it and have no idea what it does."
Plus, if I encounter problems I have to then ask others again. I can also pass on my knowledge to others who may not know.
$$anonymous$$y main camera (that renders my entire scene) should cover the entire screen, but for some reason needs the viewport code. Why would that be? (it's my main game view camera).
Uhm, no sorry ;) I started program$$anonymous$$g 19 years ago. In the beginning i programmed in pascal (mainly DOS/W95) and later i switched to delphi. I started diving into OpenGL about 14 years ago i guess. The most famous OGL tutorials are probably the NeHe turorials because the example code of most tutorials has been ported to about 30 different (program$$anonymous$$g) lanugages. The first ones are already a bit outdated and of course not directly related to Unity. OpenGL is just an API, but it helps to understand how the GPU works.
Unity's GL class has some similarities to OpenGL. If you want to understand how to setup a certain "view" you should understand the graphics pipeline and what each stage actually means (local->world->camera->projection->clipping->screen). In plain OpenGl there's no definition for local, world or camera space. It's all combined in the modelview matrix. The projection matrix defines how to project the 3d coordinates to 2d coordinates to finally put things on our 2d screen.
OpenGL is nothing but a huge state-machine. So once you set a matrix / color / normal(missing in Unity) / texture coordinate / shader pass, it will be used from now on until it's changed again. In Unity it's similar but keep in $$anonymous$$d that Unity does it's own thing in between so you can't expect to have the same state the next frame.
It also depends on where you execute your drawing code. OnPostRender is called from a specific camera while OnRenderObject is called for every camera that renders the scene / object. This is useful when you want to see your stuff in the sceneview as well. $$anonymous$$eep in $$anonymous$$d to use Camera.current in that case and not Camera.main or something like that ;)
Your answer
Follow this Question
Related Questions
Drawing 3D Lines with GL? 2 Answers
Draw 2D polygon 1 Answer
Camera OnPostRenderer on iPhone/iPad sometimes paints all the GL.Lines Black. 1 Answer
GL.MultMatrix multiple instances drawing 0 Answers
Creating GL Lines 2 Answers