PDA

View Full Version : Traversing sphere array without loop



Shao tian
01-21-2011, 08:00 PM
: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) :D

mobeen
01-21-2011, 08:41 PM
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) :D
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

Shao tian
01-21-2011, 09:04 PM
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?

mobeen
01-21-2011, 11:43 PM
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() \n"
"{ \n"
" float pointSize = 500.0 * gl_Point.size; \n"
" vec4 vert = gl_Vertex; \n"
" vert.w = 1.0; \n"
" vec3 pos_eye = vec3 (gl_ModelViewMatrix * vert); \n"
" gl_PointSize = max(1.0, pointSize / (1.0 - pos_eye.z)); \n"
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
//" gl_TexCoord[1] = gl_MultiTexCoord1; \n"
" gl_Position = ftransform(); \n"
" gl_FrontColor = gl_Color; \n"
" gl_FrontSecondaryColor = gl_SecondaryColor; \n"
"} \n"
};

const char pixelShader[] =
{
"uniform sampler2D splatTexture; \n"

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

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

mobeen
01-23-2011, 09:13 AM
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;
}

http://www3.ntu.edu.sg/home2007/mova0002/test/particlesAsSpheres.png

Kindest regards,
Mobeen

Shao tian
01-24-2011, 01:36 AM
thanks really much!!!!!

Shao tian
01-24-2011, 12:42 PM
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.

mobeen
01-24-2011, 08:51 PM
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

Shao tian
01-25-2011, 09:49 AM
Thanks really much