Traversing sphere array without loop

 :eek: Hey guys any one knows how to draw n spheres wihout a loop?

:o To simplify the problem, I have an array of vertices, and i used to just make a loop to draw the spheres with glutSolidSpheres, and each sphere has its origin as corresponding vertex in the array. But this time i cant use loop because a loop will affect my current program’s rendering speed(maybe its due to my poor architecture design :whistle: ). So my question is, how to draw those spheres using vertices in the array as origins, without a loop???

Someone might say i can use point sprites. But the spheres might overlap each others, so maybe point sprite is not ok, since sprites simpliy put a texture on a square(point) :smiley:

You can render spheres using particle sprites as shown in the nvidia gpu computing sdk (particles sample) here http://developer.nvidia.com/object/cuda_3_2_downloads.html
basically, you need to do two things,

  1. adjust the particle size based on the current view and projection matrices.
  2. diffusely shader the particles so that they are rendered as spheres.

Hope this helps,
Mobeen

Hi thamk you very much for the reply. By the way, actually my project is applying CG gpu shader language. And the link u gave me is cuda. Is there any way to solve my problem using CG?

If u look carefully (see render_particles.cpp) it renders the particles with OpenGL using glsl shader hardcoded in the file (lines 237-265) copied to show u


const char vertexShader[] = 
{    
    "void main()                                                            
"
    "{                                                                      
"
    "    float pointSize = 500.0 * gl_Point.size;                           
"
	"    vec4 vert = gl_Vertex;												
"
	"    vert.w = 1.0;														
"
    "    vec3 pos_eye = vec3 (gl_ModelViewMatrix * vert);                   
"
    "    gl_PointSize = max(1.0, pointSize / (1.0 - pos_eye.z));            
"
    "    gl_TexCoord[0] = gl_MultiTexCoord0;                                
"
    //"    gl_TexCoord[1] = gl_MultiTexCoord1;                                
"
    "    gl_Position = ftransform();                                        
"
    "    gl_FrontColor = gl_Color;                                          
"
    "    gl_FrontSecondaryColor = gl_SecondaryColor;                        
"
    "}                                                                      
"
};

const char pixelShader[] =
{
    "uniform sampler2D splatTexture;                                        
"
        
    "void main()                                                            
"
    "{                                                                      
"
    "    vec4 color2 = gl_SecondaryColor;                                   
"
    "    vec4 color = (0.6 + 0.4 * gl_Color) * texture2D(splatTexture, gl_TexCoord[0].st); 
"
    "    gl_FragColor =                                                     
"
    "         color * color2;
"//mix(vec4(0.1, 0.0, 0.0, color.w), color2, color.w);
"
    "}                                                                      
"
};

And converting from glsl to cg should not be too difficult.

Just updating u. I managed to piece the application up. It renders the particles as spheres as shown here. I adapted the code from the sdk sample i told u earlier. Here is the fragment shader for u and attached is the output. Let me know if u need the source.


#version 330
out vec4 vFragColor;

uniform vec3 Color;
uniform vec3 lightDir;

void main(void)
{
    // calculate normal from texture coordinates
    vec3 N;
    N.xy = gl_PointCoord* 2.0 - vec2(1.0);    
    float mag = dot(N.xy, N.xy);
    if (mag > 1.0) discard;   // kill pixels outside circle
    N.z = sqrt(1.0-mag);

    // calculate lighting
    float diffuse = max(0.0, dot(lightDir, N));

    vFragColor = vec4(Color,1) * diffuse;
}

Kindest regards,
Mobeen

thanks really much!!!

hi do you still have the source? may I have a copy of that… I am new to CG actually and still can’t implement that even using your fragment shader.

Hi,
Yeah sure I have submitted the code here http://www.spacesimulator.net/wiki/index.php/3d_Engine_Projects
This is in GLSL you can easily convert it to Cg.

Kindest regards,
Mobeen

Thanks really much