How to make sure a drawn object obscures another if both using same vertex locations? - opengl

Im working on an OpenGL project where i have to draw a colored gridblock as well as (white/black) lines bordering each cell in the block.
The vertices locations composing the cells are the same as the ones used for the lines(borders)..
If I use the same vertices to draw both the cells and the lines, will the lines be obscured by the faces or is it the other way around?
How do i make sure that the lines will be always visible ??

This depends on the depthfunc. with GL_LESS later primitives draw over earlier primitives if the later primitives are located before the earlier primitives. With GL_LEQUAL, the later primitives get drawn if they are not behind (i.e. same postition or before) the earlier primitives.
Thus you can either draw the lines after the cells using GL_LEQUAL or draw the cells after the lines with GL_LESS.
If you use different shaders, you might want to declare gl_Position as invariant, to ensure that the computed coordinates are exactly equal.

Related

Dynamically create complementary triangles of a regular grid in OpenGL

I have created a regular grid which originates from a 2D image, i.e. each pixels has a vertex. There are two triangles per four pixels so that I have a triangle in the top right and in the bottom left. I use vertex and index buffers for that.
Now I dynamically remove triangles / faces at the border of two different kinds of vertices (according to my application) because else there would be distortions. I wrote a geometry shader which takes a triangle and outputs the triangle or nothing (see first picture). The shader recognizes if a triangle is "faulty" (has orange edges) and omits it.
Now this works fine, but I may lose some details because of my vertex geometry. I can add complementary triangles to the mesh (see second picture, new triangles with dashed orange line).
How do I accomplish this in OpenGL?
My first idea is to create one quad instead of two triangles, check for the four possible triangles cases and create those triangles dynamically in the geometry shader. But this might be slow; GL_QUADs are deprecated and alternatives might be slow too. What do you have in mind?
Here's my idea:
Put the whole grid in a buffer/texture.
Build four triangles for each four pixels. They cross each other, yes.
In the geometry shader you can tell if a triangle is "faulty" because it connects two wrong regions. Or, sampling form the texture, because the crossing triangle is valid, so this new one can be discarded.
EDIT: Another approach
Use the texture. Draw instanced with GL_POINTS. With some order and the help of the instanceID the shader knows where the point is.
For this point test the four possible triangles. If you instance top to down and left to right, only a point to the right and the two below are used for the four triangles. And you avoid repeating tests.
Emit only those you choose.

Is it possible to draw lines and triangles in a single draw call in OpenGL?

In Blender and many other 3D modeling programs, there are 3D manipulator widgets (handles) which look like this.
However, as you can see, these widgets consist of polygon handles and lines connected to them from the origin point. However, in OpenGL, draw calls like
glDrawElements or glDrawArrays can use only one primitive mode (such as GL_TRIANGLES, GL_POINTS, GL_LINES, ...) per single draw call.
So in order to draw the 3D widgets consisting of triangles and lines, it needs at least two VBOs, one for polygons and one for lines. However I think it's pretty awkward to make draw call twice for that. (Or, without instancing or merging VBOs, we need 2 draw calls per one axis.)
Is it possible to draw both lines and triangles in single draw call in a case like this? There won't be too much performance penalty for drawing widgets though, but I'm curious if there is an efficient method to draw it.
Is it possible to draw both lines and triangles in single draw call in a case like this?
Yes, you could use geometry shaders. These would allow you to emit new output primitives that don't have to match the input primitives.
Is it worth it in this case? I would suggest not, it would be horribly complicated for little benefit.
Use a single VBO/IBO for all the axi's geometry data. Include a colour in the vertex data then draw the three axi's lines in one call, then draw the three axi's boxes in another.

OpenGL 3.2+ Drawing Cubes around existing vertices

So I have a cool program that renders a pretty cube in the centre of the screen.
I'm trying to now create a tiny cube on each corner of the existing cube (so 8 tiny cubes), centred on each of the existing cubes corners (or vertices).
I'm assuming an efficient way to implement this would be with a loop of some kind, to minimise the amount of code.
My query is, how does this affect the VAO/VBO's? Even in a loop, would each one need it's own buffer or could they all be sent at the same time...
Secondly, if it can be done, what would the structure of this loop be like, in terms of focusing on separate vertices given that each vertex has different coordinates...
As Vaughn Cato said, each object (using the same VBOs) can simply be drawn at different locations in world space, so you do not need to define separate VBO's for each object.
To complete this task, you simply need a loop to modify the given matrix before each one is rendered to the screen to change the origins of where each cube is drawn.

Alternative to glMultiDrawArrays when using uniform stride?

Background: I am developing an application that essentially draws a huge 3D graph of nodes and edges. The nodes are drawn as instanced cubes while the edges are drawn with GL_LINE and expanded with a geometry shader into 3D volumetric "lines" made of triangle strips. Currently, I am performing this expansion every time I redraw the edges. However, as my graph is entirely static (nodes can not move and thus neither can the edges), I figure that I only need to expand the GL_LINE definitions into triangle strips once, capture the expanded vertices into a buffer (using Tranform Feedback), and then from that point on draw the edges using those captured vertices with glMutliDrawArrays using a primitive type of GL_TRIANGLE_STRIP.
Question: All of these volumetric lines I am drawing contain 10 vertices. However, glMultiDrawArrays requires an array of starting indices and count sizes that essentially describe the start point and count (in elements) of each primitive. As none of my primitives vary in size, I would be making an seemingly unnecessary list of starting indices & counts. Is there any functionality OpenGL provides that would allow me to simply specify a stride (in elements) at which primitive restart would occur?
There is no such function, but for your needs, you don't need one. Transform feedback cannot output triangle strips. It outputs only basic primitives: individual points, lines, or triangles. That's why glBeginTransformFeedback only takes GL_POINTS, GL_LINES, or GL_TRIANGLES.
All you have to do is render all of your edges at once, collect the results into a single buffer via feedback, and later render the entire thing with a single call to glDrawArrays.

Triangle drawing order in DirectX/OpenGL

Is the order of drawing triangles in a 3D API guaranteed to be the same as their order in the index buffer?
For example if I have two overlapping triangles in a single draw call, and depth testing is disabled, will the first or second triangle be visible in the end?
Or do I need to issue separate draw calls to be sure that the 2nd triangle appears on top of the 1st?
In OpenGL the order is indeed preserveed, so that painters algorithm can be used, for example when rendering semitransparent geometry.
Direct3D I don't know for sure, but I'd say the same applies there.