Why is there no circle or ellipse primitive in OpenGL? - opengl

Circles are one of the basics geometric entities. Yet there is no primitives defined in OpenGL for this, like lines or polygons. Why so? It's a little annoying to include custom headers for this all the time!
Any specific reason to omit it?

While circles may be basic shapes they aren't as basic as points, lines or triangles when it comes to rasterisation. The first graphic cards with 3D acceleration were designed to do one thing very well, rasterise triangles (and lines and points because they were trivial to add). Adding any more complex shapes would have made the card a lot more expensive while adding only little functionality.
But there's another reason for not including circles/ellipses. They don't connect. You can't build a 3D model out of them and you can't connect triangles to them without adding gaps or overlapping parts. So for circles to be useful you also need other shapes like curves and other more advanced surfaces (e.g. NURBS). Circles alone are only useful as "big points" which can also be done with a quad and a circle shaped texture, or triangles.
If you are using "custom headers" for circles you should be aware that those probably create a triangle model that form your "circles".

Because historically, video cards have rendered points, lines, and triangles.
You calculate curves using short enough lines so the video card doesn't have to.

Because graphic cards operate on 3-dimensional points, lines and triangles. A circle requires curves or splines. It cannot be perfectly represented by a "normal" 3D primitive, only approximated as an N-gon (so it will look like a circle at a certain distance). If you want a circle, write the routine yourself (it isn't hard to do). Either draw it as an N-gon, or make a square (2 triangles) and cut a circle out of it it using fragment shader (you can get a perfect circle this way).

You could always use gluSphere (if a three-dimensional shape is what you're looking for).
If you want to draw a two-dimensional circle you're stuck with custom methods. I'd go with a triangle fan.
The primitives are called primitives for a reason :)

Related

OpenGL: Why triangles are chosen as basic building blocks?

I am starting openGL. Not able to understand why everything in graphics starts from triangles. Every article that I read says entire graphics rests on triangles.
What is the reason for choosing such a shape as a basic building block? I though square or circle would be much better and is logical because of the symmetry properties.
Great question. It's because triangles are the only polygons that can approximate other shapes while also being guaranteed to lie in a plane, which means they have well-defined and easy-to-compute surfaces.

OpenGL: Drawing 3D curves with varying width

I am trying to draw a curve in 3D in which the width may vary. I am doing something similar to what described in this tutorial.
Meaning, i create quads (and triangles) with the desired width instead of using OpenGL width. The problem is that the triangles are actually in 2D - they are drawn in a specific plane. The width will look different, of course, from different angles.
Am i tackling this problem in the correct way? Is there a good solution for my problem?
This is a tough question that i have recently come across while developing a game myself.
They way i handled it instead of drawing a line i would draw boxes as segments of the line. You could potential make cylinders but if your using lighting you could cheat boxes to look like cylinders.
You could also cross two quads to make an X of sorts along the length of the string.

How to break up 2d art into triangles using OpenGL

I have 2d character art that will be mapped to a Box2d skeleton. The character's arm, for example, may be 2 physics rectangles connected with a joint at the elbow. Each rectangle will have its own corresponding art sprite. The art for the lower arm will not be completely rectangular. It may be a rectangular png source, but there will be a lot of negative space where the arm isn't necessarily drawn.
My question is what is the best way to break up the 2d art into triangles? Should I treat all flat sprites as a simple rectangle made up of 2 triangles, leaving a lot of alpha space? Or is it best to break up the shape into multiple triangles and try to match the actual shape (possibly with a triangle fan)? What might be some of the advantages/disadvantages of each approach? I will be updating the textures as various things happen to the character.
Here's an illustration of what I'm talking about:
(the red square is the physics shape, and the blue lines are possible triangle configurations)
Less geometry is usually better for performance (3rd from the left).
The "exact" triangulation (4th from the left) would be useful for high-fidelity collision-detection (lasers reflecting off a shiny asteroid?).
Be careful with frequent texture updates. glTexSubImage2d() is the way to go.
If your GL implementation supports it you can do asynchronous uploads using PBOs.

Perfect filled triangle rendering algorithm?

Where can I get an algorithm to render filled triangles? Edit3: I cant use OpenGL for rendering it. I need the per-pixel algorithm for this.
My goal is to render a regular polygon from triangles, so if I use this triangle filling algorithm, the edges from each triangle wouldn't overlap (or make gaps between them), because then it would result into rendering errors if I use for example XOR to render the pixels.
Therefore, the render quality should match to OpenGL rendering, so I should be able to define - for example - a circle with N-vertices, and it would render like a circle with any size correctly; so it doesn't use only integer coordinates to render it like some triangle filling algorithms do.
I would need the ability to control the triangle filling myself: I could add my own logic on how each of the individual pixels would be rendered. So I need the bare code behind the rendering, to have full control on it. It should be efficient enough to draw tens of thousands of triangles without waiting more than a second perhaps. (I'm not sure how fast it can be at best, but I hope it wont take more than 10 seconds).
Preferred language would be C++, but I can convert other languages to my needs.
If there are no free algorithms for this, where can I learn to build one myself, and how hard would that actually be? (me=math noob).
I added OpenGL tag since this is somehow related to it.
Edit2: I tried the algo in here: http://joshbeam.com/articles/triangle_rasterization/ But it seems to be slightly broken, here is a circle with 64 triangles rendered with it:
But if you zoom in, you can see the errors:
Explanation: There is 2 pixels overlapping to the other triangle colors, which should not happen! (or transparency or XOR etc effects will produce bad rendering).
It seems like the errors are more visible on smaller circles. This is not acceptable if I want to have a XOR effect for the pixels.
What can I do to fix these, so it will fill it perfectly without overlapped pixels or gaps?
Edit4: I noticed that rendering very small circles isn't very good. I realised this was because the coordinates were indeed converted to integers. How can I treat the coordinates as floats and make it render the circle precisely and perfectly just like in OpenGL ? Here is example how bad the small circles look like:
Notice how perfect the OpenGL render is! THAT is what I want to achieve, without using OpenGL. NOTE: I dont just want to render perfect circle, but any polygon shape.
There's always the half-space method.
OpenGL uses the GPU to perform this job. This is accelerated in hardware and is called rasterization.
As far as i know the hardware implementation is based on the scan-line algorithm.
This used to be done by creating the outline and then filling in the horizontal lines. See this link for more details - http://joshbeam.com/articles/triangle_rasterization/
Edit: I don't think this will produce the lone pixels you are after, there should be a pixel on every line.
Your problem looks a lot like the problem one has when it comes to triangles sharing the very same edge. What is done by triangles sharing an edge is that one triangle is allowed to conquer the space while the other has to leave it blank.
When doing work with a graphic card usually one gets this behavior by applying a drawing order from left to right while also enabling a z-buffer test or testing if the pixel has ever been drawn. So if a pixel with the very same z-value is already set, changing the pixel is not allowed.
In your example with the circles the line of both neighboring circle segments are not exact. You have to check if the edges are calculated differently and why.
Whenever you draw two different shapes and you see something like that you can either fix your model (so they share all the edge vertexes), go for a z-buffer test or a color test.
You can also minimize the effect by drawing edges using a sub-buffer that has a higher resolution and down-sample it. Since this does not effect the whole area it is more cost effective in terms of space and time when compared to down-sampling the whole scene.

Fill curved object with color

I am new to OpenGL.
I want to draw an object which has 4 vertices. It is like a quad object, but for bottom side I need to draw an arc. Other sides are connected with straight lines. I want to fill the object.
Can anybody guide me to do this please?
Triangulate your shape and render those triangles any way you prefer (immediate mode / VBO / VAO).
Convert your arc shape into segments. Number of vertices depends on detalization/smoothness you want to achieve.
Triangulate the shape. With simple shapes, like this one, you can do it manually in code (draw it on paper like I did and write down vertices indexes that form triangles). With more complicated shapes you could use a triangulation algorithms (available on Net). When shapes are even more complicated (i.e. animal outline) - you might need to use special 2D/3D modelling software just to make them, and it will do triangulation in there.
Render the triangles.