Link to home
Start Free TrialLog in
Avatar of skiritis13
skiritis13

asked on

Lighting a GLUT object

I want to create a floor using a cube and then light it using a spotlight,
which would normally light a spherical area on the top face of the floor.

How do i create the floor?
with glutSolidCube the cube doesn't seem to have any interaction with the light,
Is there any way i can make this with glut instead of glVertex and GL_QUADS or GL_QUAD_STRIPS?
And how do i apply a texture on the glutSolidSphere?

The gluQuadrics objects dont have the same problem, because u can set their normals and the texture is auto-applied using the helper functions of GLU.

The only help that's not helpful is the help that's not given....
So any help is welcome.
Thanx.
Avatar of ikework
ikework
Flag of Germany image

taken from http://www.opengl.org/resources/libraries/glut/spec3/node80.html

"The routines generate normals appropriate for lighting but do not generate texture coordinates (except for the teapot)."

so that routines actually generate normals, send us your code in the render-loop to let us see, if there is something
wrong...

btw: why using a cube for the floor, shouldn't it be a planar object, like a rectangle or a disk or something like that?
what kind of floor do you want to create?
Avatar of skiritis13
skiritis13

ASKER

its for a project for the university, it's not like i'm making a game.
The code is pretty big so i will just give some of it.
Althought, you should know, that i 've already created a DrawCube-like function to create a cube using Quads( speed is not a matter, so i use the easy way here ) so that i can use vertex-based gouraud lighting.

// All initialiarions are done,      
glEnable(GL_CULL_FACE);
      glCullFace(GL_BACK);
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LESS);
      glEnable(GL_LINE_SMOOTH);
      glEnable(GL_LIGHTING);
      glEnable(GL_NORMALIZE);
      glEnable(GL_COLOR_MATERIAL);.
// So i have a spot light pointing down to the centre of the cube.
// I draw the cube like this.
// All the colors for the materials are global variables.

void DrawCube( float x, float y, float z, float sidex, float sidey, float sidez, bool textured )
{
if( textured )
{
 // for textured glutCube what??
}
else
{
glPushMatrix();
 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
 glColor4f(wColor[0], wColor[1], wColor[2], wColor[3]);
 glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
 glColor4f(wSpecular[0], wSpecular[1], wSpecular[2], wSpecular[3]);
 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);

 glTranslatef( x, y, z );
 glScalef( sidex, sidey, sidez );
 glutSolidCube(2.0);
glPopMatrix();
}
}

>>  its for a project for the university, it's not like i'm making a game.

games are complex simulations in these days and computer-science profits a lot of it, so it wouldnt be a shame if you would do a game anyway.
i know what im speaking of, because i do it ;)
maybe glutSolidCube uses another frontface mode, what do you use clockwise or counterclockwise?

try to disable the following in your initialize-func:
glDisable( GL_CULL_FACE );
glDisable( GL_NORMALIZE );



here is a function to draw the cube by your own:

void RenderCube( float const ext[3] )
{
      // l -> left
      // r -> right
      // T -> top
      // B -> bottom
      // f -> front
      // b -> back

      // code:  x    y    z
      //       l/r  T/B  f/b

      float const lTf[3] = {  ext[0],  ext[1],  ext[2] };
      float const lTb[3] = {  ext[0],  ext[1], -ext[2] };
      float const lBf[3] = {  ext[0], -ext[1],  ext[2] };
      float const lBb[3] = {  ext[0], -ext[1], -ext[2] };
      float const rTf[3] = { -ext[0],  ext[1],  ext[2] };
      float const rTb[3] = { -ext[0],  ext[1], -ext[2] };
      float const rBf[3] = { -ext[0], -ext[1],  ext[2] };
      float const rBb[3] = { -ext[0], -ext[1], -ext[2] };

      glBegin( GL_QUADS );
      
            // plane bottom
            glNormal3f( 0.0, -1.0, 0.0 );
            glVertex3fv( rBf );
            glVertex3fv( rBb );
            glVertex3fv( lBb );
            glVertex3fv( lBf );

            // plane top
            glNormal3f( 0.0, 1.0, 0.0 );
            glVertex3fv( lTf );
            glVertex3fv( lTb );
            glVertex3fv( rTb );
            glVertex3fv( rTf );

            // left plane
            glNormal3f( 1.0, 0.0, 0.0 );
            glVertex3fv( lBf );
            glVertex3fv( lBb );
            glVertex3fv( lTb );
            glVertex3fv( lTf );

            // right plane
            glNormal3f( 1.0, 0.0, 0.0 );
            glVertex3fv( rTf );
            glVertex3fv( rTb );
            glVertex3fv( rBb );
            glVertex3fv( rBf );

            // back plane
            glNormal3f( 0.0, 0.0, -1.0 );
            glVertex3fv( rBb );
            glVertex3fv( rTb );
            glVertex3fv( lTb );
            glVertex3fv( lBb );

            // front plane
            glNormal3f( 0.0, 0.0, 1.0 );
            glVertex3fv( lBf );
            glVertex3fv( lTf );
            glVertex3fv( rTf );
            glVertex3fv( rBf );

      glEnd();
}
to call my RenderCube-function do this:

so you have full control over backface/frontface & normals by your own. putting in texture-coords would be quite easy...


void DrawCube( float x, float y, float z, float sidex, float sidey, float sidez, bool textured )
{
    if( textured )
    {
        glBindTexture( GL_TEXTURE_2D, your_texture_id );
    }
    else
    {
        glBindTexture( GL_TEXTURE_2D, 0 );
    }

    glPushMatrix();
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glColor4f(wColor[0], wColor[1], wColor[2], wColor[3]);
    glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
    glColor4f(wSpecular[0], wSpecular[1], wSpecular[2], wSpecular[3]);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);

    float const sides_half[3] = { sidex*0.5, sidey*0.5, sidez*0.5 };
    glTranslatef( x, y, z );
    RenderCube( sides_half );

    glPopMatrix();
}

but once more, isnt a cube a strange primitive for a floor? or do you mean you create a room from that cube, that your world is inside the cube
and the bottom of the cube is the floor?
> games are complex simulations in these days and computer-science profits a lot of it, so it wouldnt
> be a shame if you would do a game anyway.
> i know what im speaking of, because i do it ;)

ike, i want to get into the business myslef later on, so far i can only learn, and do school projects.
I said , it's not like i'm making a game so that u understand that optimization and common logic are not necessary, it's just how the dude wants it for the assignment.

thanx for the code-set, although i have already a very similar drawcube func and the problem with it, that made me use a lot of quads, is the following: lighting is vertex based so to be more 'correct' the faces must have more vertices. I don't know if this is correct, please correct me if i'm wrong.

> maybe glutSolidCube uses another frontface mode, what do you use clockwise or
> counterclockwise?
> try to disable the following in your initialize-func:
> glDisable( GL_CULL_FACE );
> glDisable( GL_NORMALIZE );

i'll try the above and i'll get back to u?

(i also want to thank u for being the only-one responding
maybe u could help me in other indusrty issues, i'm from greece and
things here are not quite good for game developers, since there is no such industry,
i'm graduating the year after this one and i'm thinking of going to UK for master degree,
although i don't know if i should study software engineering or go straight to a game progamming MS ).
Well, i tried disabling normalize and culling and although the objects did respond to light the result was the same as if i were using the function you sent, with the six planes, not corrent lighting.

is lighting supposed to be so dufficult and "heavy"?
should i use lightmap for the effect of a spotlight? or that would be too difficult?
hey skiritis13,

>>  lighting is vertex based so to be more 'correct' the faces must have more vertices
lighting is vertex based, youre absolutely correct, and we define more than 8 vertices,
as you said, we define 24 in the above xmpl, each vertex is defined 3 times one for each
plane(normal) it is part of.
the effect, which is achieved is, that the cube has an edged appearance. another issue
would it be if we would draw a sphere, this should look smoothly. but lets concentrate to your case ;)

and no need to thank, youre very welcome. i have fun doing this and i learn a lot from it too ;)
so feel free to ask as much as you want...

>>  i'm from greece and things here are not quite good for game developers
im from north-east germany, i know what youre speaking of ;)
in fact i cant help you with your industrie issue, i can only say: use the schooltime
to learn and experiment as you do already i think ;) if you are sure this should be your
business, dont concentrate only to learn programming, learn everything you can get about
physics and mathematics, rigid-body-dynamics, linear algebra, integrals, differentials all
this kind o stuff. i heard so much programmers saying (myself included ;)
"if i only had known this could help anytime, i would have listened better.... " 
and start your own projects, not only teachers stuff, buy/rent books, i.e. try to get the
gems-series from Marc DeLoura, i learned so much there, try to get SIGGRAPH-proceedings and
dont be afraid about the mathematics there, im not a genius and i understood it too, after
lots af pain, but it worth's ...

>> study software engineering or go straight to a game progamming MS
if you able to, do both. practical work beside the study is very good, other fields than 3d-programming are important as well,
and lets you see things different in contrast to the university-view...

but the main thing is, never forget to enjoy yourself ;)  dont let your brain explode from all this stuff.
and if you have an interesting project and questions about it, which doesnt fit here, feel free to mail me for support.

the best wishes and good luck ;)
ASKER CERTIFIED SOLUTION
Avatar of ikework
ikework
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanx ike. i already have many books, the gems and the red book included, and i have tried creating some 2d stuff in directdraw in the past, which is the api i started with, like snake or backgammon. then i moved to direct3d and quickly turned to opengl looking forward to portability issues. Here where i am i try experimenting before going to something great like a 3d shooter, which is much more complex in general, and the concepts needed here like math and physics are greater.
I was curius if you were using a open physics engine or you habe created your own.
thanx again.
hey skiritis13, ya i created my own "verlet-based rigidbody-physics lib" with angular and linear springs. i want to implement jokobi-matrix-based joints
in future there, but the one i have works good so far for my current project, so this joints are still in the todo-list ;) if you are interested, i can share
it with you and/or explaining it, if you want to.
i did it, because i wanted to know how to implement this and to reach a time-stability, which i didnt have before really with the usual euler-integration-method.
but there are good free solutions for it anyway. russel smith's ode-lib helped me a lot designing my own one and i tested the time-stability with russels
lib as reference. i guess you know him from gems & siggraph-proceedings..

and what about your code, does it work now the way you want it to?
sorry skiritis, while rereading the task, i saw that i didnt read one of your previous comments:

>> Well, i tried disabling normalize and culling and although the objects did respond to light the result was the same as if i
>> were using the function you sent, with the six planes, not corrent lighting.

what do you mean excactly with "not correct lighting"? i looked at your code once more, but i missed the glEnable( GL_LIGHT0 ), you just
enabled lighting with glEnable(GL_LIGHTING), this enables ambient-light, if you define one and the possibility to enable the light-sources.
but this call standalone does actually nothing without the 2 other calls .

please post once more the initialization-function and the whole renderfunction, please add any addiational gl....-functions from other parts of the
program, which are called, so i can take the code home and check, whats going on, ok?

ike
ike i'm sending you a email with the whole project to take a look if you are interested;
my mail is master_of_code@hotmail.com
hey Skiritis, i will have a look at it, but please never ever print your email address here.
you could put your email into your profile this way:
"adress at xxx dot com"
so spiders cant grab it that easaly. a mod told me to do so, and i think its a good idea...

ike