Colouring 3D Objects

This topic contains 8 replies, has 3 voices, and was last updated by  hellboy 4 years, 5 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #31316

    hellboy
    Member

    Hi,

    I have many objects in my scene and I want to colour every object with different colours. Now, my fragment shader consists of :

    void main (void)
    {
    gl_FragColor = vec4(0.82, 0.41, 0.12 ,1.0);
    }”;

    and hence it colours each object with the same colour. Can anyone tell how can I colour differently ? I have prepared a 2D array consisting of the colours for all the objects. I can’t figure out how to pass them to the fragment shader or how to change the fragment shader code.

    #37527

    dgu
    Member

    you could generate random color and pass it to your shader.
    You can also build a random function inside your shader to pick ramdom color dynamicaly like

    const float UINT_MAX = 4294967295.0;

    // Generate a random uint
    uint randhash(uint seed)
    {
    uint i=(seed^12345391u)*2654435769u;
    i^=(i<>26u);
    i*=2654435769u;
    i+=(i<>12u);
    return i;
    }

    // Generate a random float [0.0, 1.0]
    float randhashf(uint seed)
    {
    return float(randhash(seed)) / UINT_MAX;
    }

    make sure that you study GLSL a little bit, the beginner section of the sdk is perfect for that , i havent seen better for now

    regards
    daavid

    #37528

    hellboy
    Member

    Do we need to create a handle for colour buffers similar to the VBO and IBO and call functions like :

    glGenBuffers(nummeshes, m_ui32Cbo);
    glBindBuffer(GL_ARRAY_BUFFER, m_ui32Cbo); //in a loop
    glBufferData(GL_ARRAY_BUFFER, sizeof(colours_array),colours_array
    , GL_STATIC_DRAW); // in a loop

    I can’t find a sample program which uses a colours array to set the colour of different objects.

    #37529

    dgu
    Member

    yes there is a sample that does the job :
    DrawSceneFlatColoured in the OGLES2DeferredShading sample

    If you had followed the pfx sample in the beginner section then no problemo you are ready for it ( only the color bit 🙂 )
    but mostly the principle is to set your uniform variable glUniform4f containing your Colors ( vec4 (r,g,b,1) )
    If you may look at the sample you will see it , that help aloooooooooooot.

    hope it help
    kind regards
    david

    #37530

    Joe Davis
    Member

    I agree with David. Generating a unique colour in your CPU code and passing it in to the shader would be the most efficient solution.

    There is no need to pass an array to the shader as you can calculate and assign a single value to a vec4 uniform before rendering the object.

    Regards,
    Joe

    #37531

    hellboy
    Member

    David, I can’t find any DeferredShading sample program in my sdk. And, I can’t find anything new in pfx sample too. I want to know how should I change my fragment shader code and how to pass the colours to the shader.

    #37532

    dgu
    Member

    Nothing new in the PFX Introduction ??? that a good one 🙂
    about the sample it s in the directory : SDK_3.1/Examples/Advanced.

    about a sample usage here it is :

    case ePVRTPFX_UsMATERIALCOLORDIFFUSE: {
    glUniform4f(aUniforms[j].nLocation, pMaterial->pfMatDiffuse[0], pMaterial->pfMatDiffuse[1], pMaterial->pfMatDiffuse[2], 1.0f);
    }

    i am just giving advise that is all , make sure you understand uniform and GLSL the sample for beginner is super rich of new knowledge that solve this issue.

    good luck
    david

    #37533

    dgu
    Member

    there is a doc that is golden for me it s in the directory ;
    /SDK_3.1/Documentation/Architecture Guides

    the name is PowerVR.Performance Recommendations.pdf

    beyond the recommendation ,there is a lot of info specially section 6.4 might be interesting.

    very good stuff

    #37534

    hellboy
    Member

    Hey, Got the colour done. It was easy. Was doing silly things. Finally, I changed the fragment shader as :

    uniform mediump vec4 myColor;
    void main (void)
    {
    gl_FragColor = myColor;
    }”;

    and called the below in a loop :

    int i32LocationColor = glGetUniformLocation(m_uiProgramObject, “myColor”);

    glUniform4f(i32LocationColor, colours[0], colours[1], colours[2], 1);

Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic.