- Home /
Creating GL Lines
Id like to set up a function that draw lines exactly like the Debug.DrawLine() function works.
I just want to say DrawLine(point1, point2) and it will draw a line between point 1 and two.
I I tried to use GL Lines (I have Pro)
the line get generated when its called. Could someone tell me what Im doing wrong? (I really just copied this from the documentation because I dont fully understand how it works.
public static void DrawGLLine(Vector3 P1, Vector3 P2)
{
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(P1);
GL.Vertex(P2);
GL.End();
GL.PopMatrix();
}
Im then trying to call it in the part of the script that gets updated every frame
DrawGLLine(cam.transform.TransformPoint(v[0]),hit_point4.point);
Does it have to be OnPostRender()? That would make it difficult to call the function at anytime when I have two points I need to draw between.
That statement just says "usual" does that imply it has to be there or not necessarily?
I take it to mean that there are other functions you could use to get it to render after everything else, but OnPostRender
is the most convenient. I haven't tried using this myself as I don't have Pro, unfortunately.
I'm not sure I understand how it makes it any harder, could you explain? Isn't it just a matter of setting up your calculations outside of the function, creating a queue of lines to be drawn, waiting for PostRender, then calling them all?
Interpret "usual" as "call GL drawing functions in OnPostRender() unless you know exactly what you are doing". If you have the code in an Update() then it won't work. Can't you just add an OnPostRender, and store your two points in variables set in your update or where-ever. You can also set a boolean value that controls whether the line is drawn or not.
The thing that makes this all difficult is that the points are stored inside of a static class that extends the capabilities of the built in Camera class. Im currently using debug.DrawLine() inside this class once the points are calculated. Everything works exactly as it should except Debug.DrawLine only works in the editor. I need to replace it with someway to draw a vectored line.
Extension classes are somewhat new to me, which is why the usual techniques are giving me a hard time.
Answer by Bunny83 · Jul 27, 2011 at 07:41 PM
You can't call it in Update since the first thing that happens in the render step (which comes after Update) is to clear the framebuffer. If you want to show something above the rendered cameras you need to use one of the post-events (OnPostRender or OnGUI for example). Also keep in mind that LoadOrtho will load a totally different projectionmatrix than your camera is using. You should also set a modelview matrix to transform the world coordinates into camera coordinates or your line won't appear at the desired world coordinates.
You can set the perspective matrix from your camera.
Next thing is that you need a material to render with. If you don't set one it's quite random what material is used.
http://unity3d.com/support/documentation/ScriptReference/GL.LINES.html
I've used GL.LINES a lot but i have no time at the moment to show a full example.
Great explanation, However I broke down and just purchased vectrosity. Trying to use OnPostRender() would have required me to restructure my code.
Answer by Chris D · Jul 27, 2011 at 07:16 PM
From the docs:
GL immediate drawing functions use whatever is the "current material" set up right now. The material controls how the rendering is done (blending, textures, etc.), so unless you explicitly set it to something before using GL draw functions, the material can happen to be anything. Also, if you call any other drawing commands from inside GL drawing code, they can set material to something else, so make sure it's under control as well.
GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).
The usual place to call GL drawing is most often in OnPostRender() from a script attached to a camera
Edited to include all relevant details. Docs here.
For an example of drawing to screen with GL, check out this question and the link it has to this wiki post.
Your answer
![](https://koobas.hobune.stream/wayback/20220612160500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I change GL LINES color? 1 Answer
Drawing 3D Lines with GL? 2 Answers
Draw 2D polygon 1 Answer
Use Gl.Lines to connect Vector3 points in world space 1 Answer