Results 1 to 9 of 9

Thread: Traversing sphere array without loop

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    25

    Traversing sphere array without loop

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

    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 ). 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)

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Traversing sphere array without loop

    Quote Originally Posted by Shao tian
    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)
    You can render spheres using particle sprites as shown in the nvidia gpu computing sdk (particles sample) here http://developer.nvidia.com/object/c...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
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    25

    Re: Traversing sphere array without loop

    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?

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Traversing sphere array without loop

    Quote Originally Posted by Shao tian
    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
    Code :
    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.
    Regards,
    Mobeen

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Traversing sphere array without loop

    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.
    Code :
    #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
    Regards,
    Mobeen

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    25

    Re: Traversing sphere array without loop

    thanks really much!!!!!

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    25

    Re: Traversing sphere array without loop

    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.

  8. #8
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Traversing sphere array without loop

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

    Kindest regards,
    Mobeen
    Regards,
    Mobeen

  9. #9
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    25

    Re: Traversing sphere array without loop

    Thanks really much

Similar Threads

  1. Array indexing in for loop
    By overTaker in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-05-2014, 01:27 AM
  2. Replies: 0
    Last Post: 08-14-2013, 03:03 PM
  3. Traversing a Tree using the root pointer
    By preston in forum OpenCL
    Replies: 13
    Last Post: 02-11-2011, 11:06 AM
  4. Render a trimmed sphere ? (a quarter of sphere)
    By HaVyThoai in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-07-2007, 12:05 PM
  5. traversing hierarchy?
    By mgf1123 in forum COLLADA
    Replies: 1
    Last Post: 06-15-2007, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean