Portaljacker
Portaljacker

Reputation: 3142

Material property help in Open GL using GLUT & C++

For the lighting portion of an OpenGL project I'm doing this part for my team at the moment:

Define material properties for the models in Part 1. Make your material definitions easily replaceable. Specifically, robot components and the remote control unit should be "shiny"; rubble piles should be defuse if various color shades; main platform tiles should be diffuse gray. Select your own material properties and properly document them for the blocks, bases, factories, etc.

I've only been trying to apply the "shiny" effect on the control unit and it hasn't worked well. I tried putting in some sample lighting since we haven't put in any yet (or at least it's not in the repository) to test it out. The result is that the cube portions of the controller reflected (though way too white, no matter what I tried, the color of the cubes is grey) but not the cylinder antenna. (None of those tests are in the repository since I couldn't commit something that won't work.)

Basically I need help with material properties and setting up some basic lighting to test them (the test lighting is temporary since one of my teammates is working on lighting).

I would really appreciate if someone could help me out with doing this part.

Project info:

If I missed anything or if you need more info please ask!

Upvotes: 0

Views: 2751

Answers (1)

nosmirck
nosmirck

Reputation: 666

I'll help you with the basics on lightning in OpenGL... first you need to enable and set your lights, so, here is some example:

    void setlight(){
            //here you set the lights and parameters, example with one light
            float LightAmbient[] = { 0.1f, 0.1f, 0.05f, 1.0f };
            float LightEmission[] = { 1.0f, 1.0f, 0.8f, 1.0f };
            float LightDiffuse[] = { 1.0f, 1.0f, 0.8f, 1.0f };
            float LightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
            float LightDirection[]={-0.5f, -0.5f, -0.5f};
            glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
            glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
            glLightfv(GL_LIGHT0, GL_POSITION, LightDirection);
            glEnable(GL_LIGHTING);
            glEnable(GL_LIGHT0);
    }
    void setmaterial(){
            //here you set materials, you must declare each one of the colors global or locally like this:
            float MatAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
            float MatDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
            float MatSpecular[] = { 0.1f, 0.1f, 0.0f, 0.1f };
            float MatShininess = 60;
            float black[] = {0.0f,0.0f,0.0f,1.0f};
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, MatAmbient);
            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MatDiffuse);
            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, MatSpecular);
            glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MatShininess);
            glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
    }
    void render(){
            //your render function
            //clean buffers, set viewport, projection, lookat, etc...
            . . .

            //set light and materials
            setlight();
            setmaterial();

            //draw your stuff

            . . .

            glutSwapBuffers();
    }

Now, if you need to set different materials for differents object to draw you must set the materials for each object before you draw them... example:

    for(int i=0; i<num_objetcs; i++){
            setmaterial(i);
            drawobject(i);
    }

Of course, you have to modify what I wrote before... You just have to set the light and then for each object you set their material and then draw, one by one. You must modify the "setmaterial" function to take the material parameter and I suppose you know what to do for the rest... good luck!

Upvotes: 2

Related Questions