PDA

View Full Version : Extrude a surface without glBegin-glEnd?



Lefteris
07-18-2011, 09:15 AM
Hello,

In my application I have many routines which create 2D (x,y) surfaces, triangulate them and upload them to a 2D VBO. So no Z coordinate is actually in play. Later I draw with glDrawElements() and glOrtho and all is fine and 2D.

My first question is in here. What's the default z that is given when glDrawElements() is used in such a situation?

Now I have started to have the need to extrude some of these surfaces. So from 2d they will go to 3d.
I thought that the solution would be to go to perspective projection, draw the surface once, translate on the z axis by the depth I would like the extruded surface to have and then redraw the surface. And after that I would only need to draw the triangles that connect these two surfaces and voila, we have an extruded surface.

The problem is this though:

How can this be done without reading data from the VBO with glMapBuffer or similar functions? What would generally be the fastest way to do this?

I thought of doing it with glMapBuffer but I am afraid it would end up being too slow. Another option would be to use geometry shaders. Would that be faster? I did it simply with glBegin-glEnd but I am afraid that when my application scales up it will become impossible to keep it like that.

aqnuep
07-19-2011, 02:15 AM
AFAIK, the default value for the x,y,z coordinates is 0 and for w it is 1.

I think the most elegant method for extrusion is using geometry shaders. While the additional shader stage in fact will decrease the performance somewhat, it is not comparable with doing the extrusion on the CPU and especially faster than using buffer mapping.

mbentrup
07-19-2011, 07:14 AM
As I understand it he just wants to render with a small offset to z, so you don't need a geometry shader - a vertex shader can do that too.

If all vertices are to be offset by the same amount, and you can even do this without shaders: instead of moving the vertices to the camera, you can move the camera closer to the vertices to get the same result.

Oops: I missed the part about rendering the connecting surfaces, so it is proper extrusion - forget all I said.

Lefteris
07-19-2011, 12:16 PM
Yeah exactly guys thanks for taking the time to reply. I figure out too that the only way would be geometry shaders but never having touched them before I have some difficulties. So here is a followup question. If I don't see any replies here since the topic title is misleading now I might make another topic.

My gpu supports up to 3.3 openGL version. I plan to offer alternative to geometry shaders but from what I understand after 3.3 they are inside the core and before that they are an extension so for modern hardware a geometry shader should not be that hard.

The geometry shader compiles and links correctly but I don't see what I expected to see.
My test code in the cpu is:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/height,0.01f,100.0f);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_TRIANGLES);
glVertex3f(-0.2,-0.2,-1);
glVertex3f(0.2,-0.2,-1);
glVertex3f(0,0,-1);
glEnd();

And my test geometry shader is this:


#version 150

layout(triangles) in;
layout(triangle_strip, max_vertices = 10) out;

#extension GL_EXT_geometry_shader4 : enable

void main()
{
for(int i = 0; i < gl_VerticesIn; ++i)
{
// copy position
gl_Position = gl_PositionIn[i];

EmitVertex();
}
EndPrimitive();

//add one additional triangle (?)
for(int i=0; i< gl_VerticesIn; i++)
{
gl_Position = gl_PositionIn[i];
gl_Position.z -= 0.5;
EmitVertex();
}
EndPrimitive();
}


The geometry shader seems to be working if I mess with .x or .y attributes of gl_Position but I don't see any new triangle being drawn with the above program. If it was .x instead I see 2 triangles. One a duplicate of the other displaced by 0.5.

So what could be the problem here? Also can you state what is blatantly wrong, if anything with my use of geometry shader? Why could the z coordinate not be drawn correctly?

aqnuep
07-21-2011, 12:01 AM
Are you sure the extruded triangle is not covered by the original one? As I see you displace it so that it will be at the same position, just biased away from the camera, so maybe it is just hidden by the original triangle.
Btw, the code seems to be okay.