0

Currently creating a small animation of Neptune and its moon's revolving around the sun. With some help I managed to get a background of stars but instead of being white, they're now black. I've tried putting glColor3f(1.0, 1.0, 1.0) inside and outside of the matrix consisting the background and none of it seems to be working. Any solutions?

Background declaration: Display()

Background call: End of Display()

int triton = 0;
int proteus = 0;
int neptune = 0;
int sun = 0;
GLint buf, sbuf;

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    // Kind of Useless rn
    glGetIntegerv(GL_SAMPLE_BUFFERS, &buf);
    printf("Number of sample Buffers: %d\n", buf);
    glGetIntegerv(GL_SAMPLES, &sbuf);
    printf("Number of samples: %d\n", sbuf);
    printf("Controls: \n A = Orbit Left. \n D = Orbit Right. \n S = Stop. \n (,) = Move Left. \n (.) = Move right.");

    glShadeModel(GL_SMOOTH);

    // Material Specs
    GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
    GLfloat mat_shininess[] = { 40.0 };
    GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
    GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };

    // Light 0 Initialized.
    GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

    // Spotlight (Sun)


    // Mat Specs Implmentations.
    glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

    // Light 0 implementation
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light0);


    // Ambient surrounding light on object.
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

    // Antialias 3D Shape (In Progress)
    /*glEnable(GL_BLEND);
    glEnable(GL_POLYGON_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);*/

    // Enable Lighting and Depth

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
}

void orbit(void)
{
    triton = (triton - 2) % 360;
    proteus = (proteus - 5) % 360;
    neptune = (neptune - 1) % 360;
    glutPostRedisplay();
}
void backorbit(void)
{
    triton = (triton + 2) % 360;
    proteus = (proteus + 5) % 360;
    neptune = (neptune + 1) % 360;
    glutPostRedisplay();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_COLOR_MATERIAL);

    glPushMatrix();
    glNewList(1, GL_COMPILE);
    glBegin(GL_POINTS); 
    glColor3f(1.0, 1.0, 1.0);
    for (int i = 0; i < 300; i++)
    {
        for (int j = 0; j < 300; j++)
        {
            if (((i + j) % 2) == 0)
            {
                glVertex2f(30 * i, 30 * j);
            }
        }
    }
    glEnd();
    glEndList();

    // Sun 
    glPushMatrix();
        glColor3f(1.0, 0.35, 0.1);
        glTranslatef(0.0, 0.0, 0.0);
        glutSolidSphere(2.0, 100, 100);

        // Neptune
        glPushMatrix();
            glRotatef((GLfloat)neptune, 0.0, 1.0, 0.0);
            glTranslatef(3.0, 0.0, 0.0);
            glColor3f(0.1, 0.1, 0.3);
            glutSolidSphere(0.3, 100, 100);

            // Triton
            glPushMatrix();
                glColor3f(0.85, 0.7, 0.8);
                glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
                glTranslatef(1.0, 0.0, 0.0);
                glutSolidSphere(0.05, 100, 100);
            glPopMatrix(); // Ends Triton

            // Proteus
            glPushMatrix();
                glColor3f(1.0, 1.0, 1.0);
                glRotatef((GLfloat)proteus, 0.0, 1.0, 0.0);
                glTranslatef(1.0, 0.0, 0.0);
                glutSolidSphere(0.02, 100, 100);
            glPopMatrix(); // Ends Proteus

        glPopMatrix(); // Ends Neptune

    glPopMatrix(); // Ends Sun


    glEnable(GL_MULTISAMPLE);

    // Stars 
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
        glLoadIdentity();
        glPushMatrix();
            glMatrixMode(GL_PROJECTION);
            glPushMatrix();
                glLoadIdentity();
                gluOrtho2D(0, 1000, 0, 1000);
                glColor3f(1.0, 1.0, 1.0);
                glCallList(1);
            glPopMatrix();
            glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
    glPopMatrix(); 

    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix(); // Ends Solar System
    glFlush();
    glutSwapBuffers();

}
void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
        // Triton + Proteus Orbit. 
    case 'a':
        glutIdleFunc(orbit);
        break;
    case 'd':
        glutIdleFunc(backorbit);
        break;
    case 's':
        glutIdleFunc(NULL);
        break;
    case ',':
        glTranslatef(-0.3, 0.0, 0.0);
        glutPostRedisplay();
        break;

    case '.':
        glTranslatef(0.3, 0.0, 0.0);
        glutPostRedisplay();
        break;
    case 27:
        exit(0);
        break;
    default:
        break;
    }
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
    glutInitWindowSize(1000, 1000);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Neptune and Space");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Isn't 0,0,0 in RGB black? You are coloring them to be black. – Dabiuteef Sep 04 '18 at 01:22
  • Typo, my mistake however the problem still remains after that fix. – SkyLightsYou Sep 04 '18 at 01:44
  • @Rabbid76 I had tried creating the list in both display() and init() but it has no effect on whether stars change color or not. This code happens to have the list created in display(). – SkyLightsYou Sep 04 '18 at 06:11
  • @SkyLightsYou Did you disable lights for the starts or set normal so the points are lighted? Also this [swift sphere combine star data](https://stackoverflow.com/a/40171880/2521214) and [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](https://stackoverflow.com/a/28020934/2521214) might interest you – Spektre Sep 04 '18 at 08:03
  • @Spektre I troubleshooted this a little bit and found out that 'light0', and only 'light0', is causing this problem. When 'light0' is inactive, the stars are displayed in white. When active the stars are displayed in black. I initially had a similar problem when I colored my planets however that was solved with 'glColorMaterial()'. Thank you for the resources! Much appreciated. – SkyLightsYou Sep 04 '18 at 09:00
  • @SkyLightsYou then it is as I guessed. So either render stars without light or set `glNormal` to value that is lighted (depends on your camera and light properties). You can do it just for first star as the normal is remembered. That is also your problem the normal is set from the last usage (any mesh with normal like planet) so once you start rotating the camera or light source the starts might flicker (from black to white) depending on the angle between last normal and light source – Spektre Sep 04 '18 at 09:39
  • @Spektre I managed to fix it differently, I'm unsure if it's the correct way though. I first scrapped the original code and replaced it with two lights. Light 0 position at the (1,1,1,-100) and Light 1 position at (-1, -1, -1, -100) as well. Interestingly the stars became white again. – SkyLightsYou Sep 04 '18 at 09:45
  • @SkyLightsYou for me the correct way is to disable lights for stars as points does not have surface/normal and also stars are emitting light not reflecting it so the normal and lighting equations do not fit for them... but any visually OK solution will cut it... although adding new light source might not be the best idea as that will corrupt the lighting for objects ... – Spektre Sep 04 '18 at 09:59
  • @Spektre Got it, I think that's a much less error-prone solution. Much appreciated! – SkyLightsYou Sep 05 '18 at 02:30

0 Answers0