Advertisement

Help newbie w/ Material

Started by November 27, 2002 03:32 PM
16 comments, last by Roming22 22 years, 3 months ago
Hi, I''m learning OpenGL and programming pool game. I tried to change the material to have a glossy ball, but it just won''t work. Obviously I didn''t understand how material works. Can someone post or mail me the code of a program that would just draw a shiny yellow sphere.
HAHAHA LOL
Ok, Im sorry, but that sounds like a very n00bish question. But, I will attempt to help.

There is no fixed function that renders a glossy ball. OpenGL doesnt have ''materials'' like a 3d program. It has textures. You can try to make your texyure the way you want it to look. But that will probably not look very good anyway. Through multitexturing or a fragment program you can get a pretty realistic glossy material. I suggest you look into Nvidia''s Cg. It is pretty good and has very good documentation IMO.
Advertisement
better yet, you should just do more simple things for now. If you dont understand texturing completely you need to learn more. NeHe has good tutorials and if you want a book, OpenGL Game Programming is good.
by gloss do you simply mean shiny? as in a nice specular component? or do you mean shiny as in slightly reflecting?

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Yes Riptorn that''s what I meant. I know there is a glMaterial function but I just can''t get it to work. I didn''t find any examples in Nehe''s tutorial, so that''d why I came here for help.

Thanks for reformuling my question (i''m french, and i didn''t realize that there was a difference between glossy and shiny).
Well I guess "reformuling" isn''t a word.
So here''s another try: Thanks for trying to understand my question.

I meant shiny.

If you want me to send you my code, there''s no problem. The pool works really well already. I just have to figure out this material problem and then learn how to have the mouse control the strength of the hit (it already moves the cue but it seems it has two speed when you hit the ball: 0 or 1).
Advertisement
If you *just* want shining spheres, you have to :
- setup lights
- setup materials

1. To setup light, you have to call glLight that will set the position and intensity of lights, and then you have to call glEnable(GL_LIGHTING) and glEnable(GL_LIGHTx ) where x is the light index. For instance :
GLfloat light_pos[] = { 5.0f, 1.0f, 10.0f, 1.0f};
GLfloat light_dif[] = { 1.0f, 1.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_dif);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

2. To setup material, you have to call glMaterial, for instance :
GLfloat mat_dif[] = { 0.8f, 0.3f, 0.0f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_dif);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 5.0f);

3. When you render your objects, you have to setup the normals using glNormal. If you''re too lazy to setup normals, you can start with automatic normal generation thanks to glEnable(GL_AUTO_NORMAL). It looks crap because it''s not smooth but it''s a good start for lighting/material.

ps: Vive la France !
Bonjour vincoof.

I have a light in the scene that's setup in InitGL (NeHe's code), and it's lightning up the scene right. And just before drawing the balls I call the material like that:


  float a_b={0.3,0.3,0.3},s={1.0,1.0,1.0)glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, a_d);glMaterialfv(GL_FRONT, GL_SPECULAR, s);glMateriali(GL_FRONT, GL_SHININESS, 100);draw_balls();  


But it seems nothing happens. If you want all the code, I can send it to you.

A bientot


[edited by - Roming22 on November 28, 2002 7:40:38 AM]
I don''t think this piece of code will ever work.
First, you need an array :
float a_b[]={0.3,0.3,0.3},s[]={1.0,1.0,1.0)
(note the red brackets).
Then, you may have four values instead of three :
float a_b[]={0.3,0.3,0.3,1.0},s[]={1.0,1.0,1.0,1.0)

You can send your source code at vincoof@netscape.net (not bigger than 1MByte please ! )
You could also blend a spheremapped "gloss" texture onto your billiard balls. Something like this was done in the reflection tutorial (on the beech ball).

"Free advice is seldom cheap."
-- Rule of Acquisition #59

This topic is closed to new replies.

Advertisement