Lighting Problem

EDIT: I fixed it with a few hours of google searching. If anyone could delete/lock the thread that’d be great :slight_smile: (I hadn’t declared a specific material for those also wondering)

Hello fellow programmers! A couple of months ago I decided that I wanted to learn OpenGl. Finally, I decided to bite the bullet and learn it! Now, I am coding with GLUT and GLEW, but I still felt this would be an appropriate forum to post on, as they ( to my knowledge ) simplify some of the functions. Anyway, to the problem! I just now started fumbling with lighting, and I picked up a very strange problem. The objects seem to be lit fine, but there is a large completely black spot on them. ( both spheres ) Screenshot, and code included below.

  • Warning: Please excuse the extreme sloppiness as, this is merely a test of knowledge, not an attempt to write neat code. So it’s kinda all over the place. My apologies :stuck_out_tongue: -

#include <GL/glew.h>
#include <GL/glut.h>
#include <cmath>

float locx = 0.0f;
float locy = 0.0f;
float sinAdd = 0.0f;

float planx = 0.0f;
float plany = 0.0f;

void Reshape( int width, int height )
{

    glViewport( 0, 0, (GLsizei)width, (GLsizei)height );
    glMatrixMode( GL_PROJECTION );

    glLoadIdentity();
    gluPerspective( 90, (GLfloat)width / (GLfloat)height, 1.0, 100.0 );

    glMatrixMode( GL_MODELVIEW );

}

void lightInit( void )
{

    GLfloat matSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat matShininess[] = { 50.0 };
    GLfloat lightPos[] = { 0.0, -2.0, -5.0, 0.0 };

    glShadeModel( GL_SMOOTH );

    glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, matSpecular );
    glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, matShininess );
    glLightfv( GL_LIGHT0, GL_POSITION, lightPos );

    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );

}

void drawPlanet( void )
{

    locx = ( sin( sinAdd )*3 ) + planx;
    locy = ( cos( sinAdd )*3 ) + plany;

    glPushMatrix();

    glTranslatef( locx, 0.0f, locy );
    glutSolidSphere( 0.2, 64, 64 );

    glPopMatrix();

    sinAdd+= 0.0001f;

}

void drawSun( void )
{

    planx = sin( sinAdd );
    plany = cos( sinAdd );

    glPushMatrix();

    glPushAttrib( GL_CURRENT_BIT );

    glColor3f( 0.3f, 0.0f, 0.0f );
    glTranslatef( 0.0f, 0.0f, 0.0f );
    glutSolidSphere( 0.7, 16, 16 );

    glPopAttrib();

    glPopMatrix();

}

void Display( void )
{

    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glLoadIdentity();

    glTranslatef( 0.0f, -2.0f, -10.0f );
    drawPlanet();
    drawSun();
    //drawMoon();

    glutSwapBuffers();

}

int main( int argc, char **argv )
{

    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
    glEnable( GL_DEPTH );

    glEnable( GL_COLOR_MATERIAL );

    glutInitWindowSize( 1024, 900 );
    glutInitWindowPosition( 200, 100 );

    glutCreateWindow( "Orbital Sim" );

    lightInit();

    glutDisplayFunc( Display );
    glutIdleFunc( Display );
    glutReshapeFunc( Reshape );

    glutMainLoop();

}

Edit: It doesn’t seem to want me to add pictures, so if anyone could help me out with that, that would be great.

hi,

thats really a strange one. i copy and ran your code.
by changing the parameters of glut sphere i was able to change black spot size …
my attempt would be: use own mesh, add colors, start moving objects and camera.
but still: strange !

cu
uwi