PDA

View Full Version : lights don't work in my project



killwin
09-26-2009, 12:52 PM
Hello,

In my 3d project, lights don't work correctely.
3d objects are spheres and when lights are on, lights illuminate spheres with triangles in various colors.
I don't see a perfect smooth or diffuse light color on spheres. Only ambiant_color is ok.

Can you right my code ? i think i forgot something. Thanks verry match.

Init function : dockpInitScreen
while(1) run function : dockpPaintOutput

void gener_texture(GLuint *texture, unsigned char *image, GLuint width, GLuint height) {

// Generation et attribution du nom de texture
glGenTextures (1, texture);
// creation et utilisation de la texture
glBindTexture(GL_TEXTURE_2D, *texture);
// charge les parametres des textures
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// definition des textures
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);

}

static Bool
dockpInitScreen (CompPlugin *p,
CompScreen *s)
{
int i, j, c;

DOCKP_DISPLAY (s->display);

BenchScreen *bs = (BenchScreen *) calloc (1, sizeof (BenchScreen) );

s->base.privates[bd->screenPrivateIndex].ptr = bs;

WRAP (bs, s, paintOutput, dockpPaintOutput);
WRAP (bs, s, preparePaintScreen, dockpPreparePaintScreen);
WRAP (bs, s, donePaintScreen, dockpDonePaintScreen);

bs->width = 1280;
bs->height = 1024;
bs->alpha = 0.0;

glViewport(-(bs->width / 2), -(bs->height / 2), bs->width * 2, bs->height * 2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// definit les modes de stockages des pixels
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

// allocation des indexes contigus
fontOffset = glGenLists(256);

for(i = 0; i < 256; i++) {
// specifie le debut de la liste d affichage
glNewList(fontOffset + i, GL_COMPILE);
glBitmap(8, 16, 0.0, 2.0, 10.0, 0.0, letters[i]);
glEndList();
}

gener_texture(&amp;bs->halotTex0, image_halot_0, 128, 128);
gener_texture(&amp;bs->halotTex1, image_halot_1, 128, 128);
gener_texture(&amp;bs->iconeTex[0], image_icone0, 200, 200);
gener_texture(&amp;bs->iconeTex[1], image_icone1, 200, 200);
gener_texture(&amp;bs->backTex, image_data, dock.width, dock.height);

// definit les proprietes de matiere des objets de la scene
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material.specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, material.specular);
//glMaterialfv(GL_FRONT, GL_SHININESS, material.shininess);

// definit les proprietes de la lumiere
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
for(i = 0; i < 10; i++) {
etoile.halot[i].actif = 0;
}

bs->curseur = XCreateFontCursor(s->display->display, XC_center_ptr);

GLERR;

return TRUE;
}

static Bool
dockpPaintOutput (CompScreen *s,
const ScreenPaintAttrib *sa,
const CompTransform *transform,
Region region,
CompOutput *output,
unsigned int mask)
{

Bool status;
CompTransform sTransform = *transform;
unsigned int i;
char str[] = "Bonjour";

DOCKP_SCREEN (s);

UNWRAP (bs, s, paintOutput);
status = (*s->paintOutput) (s, sa, transform, region, output, mask);
WRAP (bs, s, paintOutput, dockpPaintOutput);

if(bs->alpha <= 0.0)
return status;

glGetError();
glPushAttrib (GL_LIST_BIT | GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT);
GLERR;

transformToScreenSpace (s, output, -DEFAULT_Z_CAMERA, &amp;sTransform);

glPushMatrix ();
glLoadMatrixf (sTransform.m);
glOrtho(-1, 1, -1, 1, -200, 200);

glShadeModel(GL_SMOOTH);

// activation du blending
glEnable (GL_BLEND);

// blending alpha
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// parametre les fonctions combinatoires de texturage
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

// Transformation de translation
glTranslatef (dockpGetPositionX (s->display) - (float)dock.width + dock.decallage,
dockpGetPositionY (s->display), 0);

// activations
glEnable (GL_TEXTURE_2D);
glEnable (GL_MAP1_VERTEX_3);

glColor4f (1.0, 1.0, 1.0, 1.0);
glRasterPos2i(200, 200);
glListBase(fontOffset);
glCallLists(8, GL_UNSIGNED_BYTE, str);

if(etoile.actif) {

// creation et utilisation de la texture
glBindTexture (GL_TEXTURE_2D, bs->halotTex1);

// affichage du halot avec sa texture
texture_4p(etoile.halot[0].alpha, (float)etoile.halot[0].b[0], (float)etoile.halot[0].b[1],
etoile.size + (float)etoile.halot[0].b[0], etoile.size + (float)etoile.halot[0].b[1],
(float)etoile.halot[0].b[2]);

// creation et utilisation de la texture
glBindTexture (GL_TEXTURE_2D, bs->halotTex0);

// affichage du halot avec sa texture
for(i = 1; i < 20; i++)
if(etoile.halot[i].actif) {
texture_4p(etoile.halot[i].alpha, (float)etoile.halot[i].b[0], (float)etoile.halot[i].b[1],
-(float)i * 1 + etoile.size + (float)etoile.halot[i].b[0], -(float)i * 2 + etoile.size
+ (float)etoile.halot[i].b[1], (float)etoile.halot[i].b[2]);
}
}

// creation et utilisation de la texture
glBindTexture (GL_TEXTURE_2D, bs->backTex);

// affichage du plan avec sa texture
texture_4p(1.0, 0.0, 0.0, dock.width, dock.height, 0.0);

// affichage des lumieres
glLightfv(GL_LIGHT0, GL_POSITION, light[0].position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light[0].color);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light[0].color);

glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);

glTranslatef(icone[0].x, 0, -25);
glScalef(1, 1, 1);

for(i=0; i < NBR_ICONES; i++) {

glTranslatef(0, 125, 0);
glScalef(.25 + icone[i].size, .25 + icone[i].size, .25);

glBindTexture (GL_TEXTURE_2D, bs->iconeTex[i]);

// specifie les donnees du tableau, vertices + normales + textures
glInterleavedArrays(GL_T2F_N3F_V3F, 0, vicone);

// affichage des icones
glDrawElements(GL_TRIANGLES, NBR_VERTICES_ICONE, GL_UNSIGNED_BYTE, indices_icone);
glScalef(1/(.25 + icone[i].size), 1/(.25 + icone[i].size), 1/.25);
}
// on detruit la texture 2D
glBindTexture (GL_TEXTURE_2D, 0);

glDisable (GL_BLEND);
glDisable (GL_TEXTURE_2D);
glDisable (GL_LIGHTING);
glDisable (GL_LIGHT0);
glDisable (GL_MAP1_VERTEX_3);

glPopMatrix();
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glColor4f (1.0, 1.0, 1.0, 1.0);

glPopAttrib();
glGetError();

return status;
}

vivek vyas
09-27-2009, 03:04 AM
at first the program part where u think is not working
second
use redbook demos for learning these thing
Mesa demos included most of them
third
glMaterial must there for shininess and all u think

killwin
09-27-2009, 04:33 AM
thanks,

but i have tried material with shininess and it's the same problem. The // was only for a test.