Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was wondering, is it possible to render a (3D) torus shape entirely in GLSL, i.e. without using vertex/index arrays? Suggestions are welcome, thanks.

share|improve this question
    
You will need to use vertex arrays to some extent, otherwise the shaders will have no inputs to work with, and therefore wouldn't be able to do anything. There is probably a way to create the torus in a geometry or tesselation shader using interpolation points as inputs to the vertex shader. –  ktodisco Mar 9 '12 at 18:46
2  
"You will need to use vertex arrays to some extent, otherwise the shaders will have no inputs to work with, and therefore wouldn't be able to do anything." You've obviously not worked with impostors. You can draw these with nothing at all. –  Nicol Bolas Mar 9 '12 at 19:18
    
Thank you Nicol, I didn't know about imposters. My mistake. –  ktodisco Mar 9 '12 at 21:07
add comment

4 Answers 4

You can use distance functions described in here: http://iquilezles.org/www/articles/distfunctions/distfunctions.htm

share|improve this answer
add comment

One can use different approaches, but the best way to my mind is to code your own renderer in the fragment shader. Some ray-tracer or ray-marcher. In both cases you will need function that builds ray for current pixel on the screen. Next you can analytically find intersection of ray with torus (ray-tracer), or iteratively step along your ray until you encounter torus (ray-marching). In the latter case distance field is the best solution. See artificialidiot's answer.

Some links:

  1. Raytracing Topics & Techniques by Jacco Bikker
  2. A raytracer in C++
  3. introduction to raytracing
  4. simple gpu raytracing
  5. terrain marching

For advanced studying of ray-tracing see this book.

Ray-tracing example:

Ray-tracing example

Ray-marching example:

Ray-marching example

share|improve this answer
add comment

Not really an answer, but I can't post comments yet..

In Directx you can make Draw(x, 0) calls, x being the number of vertices to draw, and should no vertex buffer be bound it will auto-generate x vertices with no attributes except a unique ID. If something similar is possible in openGL...

You could then pass these vertices with just an ID through to a geometry shader, and use the ID*4 to determine the position on the torus, and generate a quad for each ID. Think of the torus' surface flattened out as a simple plane of rows by colums. For example, (int)((ID*4) / number_of_minor_segments) would give you the row offset along the length of the torus' surface, and ((ID*4) % number_of_minor_segments) would give you the column offset in the current row, minor segments beingt the number of segments you want around the minor radius.

You can calculate the offsets of the next 3 vertices by doing the same for (ID*4)+1, +2, and +3.

Map the offsets from "torus coordinates" to object then to world space, then send two triangles (from vertices 0, 1, 2; 2, 3, 0) to the rasterizer.

Of course the geometry would be re-generated every frame. Why do you want to avoid vertex buffers in the first place?

share|improve this answer
add comment

If you're using a wrapper for OpenGL, like GLUT there are often built in methods to render basic shapes. For example there is glutTorus() in the OpenGL wrapper GLUT to draw a torus without having to set anything up.

As for the comment about impostors by Nicol Bolas, I'm not sure if it's helpful in this case. All I could find with a quick search was http://http.developer.nvidia.com/GPUGems3/gpugems3_ch21.html in which case you indeed don't have to set up vertex and index arrays but I think this method might be more advanced than you need.

Anyway, if you can explain us better why you don't want to set-up a vertex and index buffer we can come up with better solutions.

share|improve this answer
    
The opengl GLUT ones make use of nothing special in the shaders.. Impostors do help if you associate the proper pixel shader to them. –  teodron Apr 27 '12 at 9:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.