Niraj Deshmukh
Niraj Deshmukh

Reputation: 22

How to Fix this error: initializer element is not constantt in OpenGL program?

I was trying to code it from OpenGL Graphics I would like to help it why is being error for being this like initializer element is not constant, I tried to add #define to keep in order constant. but still not yet far.how to fix it?

I am getiing error this far

earth.c:7:16: error: initializer element is not constant
 #define Dayyar 10.0
                ^
earth.c:37:19: note: in expansion of macro ‘Dayyar’
 float yearSpeed = Dayyar / 360 * daySpeed * SpeedMultiplicator;
                   ^~~~~~

and here this code

#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
#include<GL/glu.h>
#include<GL/gl.h>

#define Dayyar 10.0
#define SunSize 0.5
#define EarthSize 0.10
#define MoonSize 0.05
#define Speed 5
#define SpeedMultiplicator 1.0
#define Mooni 1

GLfloat year = 0.0;   //degrees
GLfloat day = 0.0;
GLfloat moonAroundEarth = 0.0;
GLfloat moonItsSelf = 0.0;
GLfloat EarthOrbitRadius = 1.25;
GLfloat MoonOrbitRadius = 0.20;

GLfloat daySpeed = Speed * SpeedMultiplicator;
GLfloat yearSpeed = Dayyar / 360 * daySpeed * SpeedMultiplicator; //check this need! thats only one problem getting error
GLfloat moonAroundEarthSpeed = 1.25 * SpeedMultiplicator;
GLfloat moonItsSelfSpeed = Mooni * SpeedMultiplicator; 

void RenderScene(void)
{
            glPushMatrix();
            // To rotate around drawn object
            gluLookAt(0.0,0.0,-4.0,0.0,0.0,1.0,0.0,-3.0,0.0);
            glColor3f(1.0,1.0,0.0);
            glutSolidSphere(SunSize,50,50);   // Display Sun as solid object
            glRotatef(year,0.0,1.0,0.0);   // Rotation of Earth around Sun
            glTranslatef(EarthOrbitRadius,0.0,0.0);
            glPushMatrix();
            glRotatef(day,0.25,1.0,0.0);
            glColor3f(0.0,0.0,1.0);
            glutSolidSphere(EarthSize,10,10);    //Display Earth as a solid object
            glPopMatrix();
            // Rotation of Moon around Earth
            glRotatef(moonAroundEarth,0.0,1.0,0.0);                                                                          
            glTranslatef(MoonOrbitRadius,0.0,0.0);                  
            glRotatef(moonItsSelf,0.0,1.0,0.0);          
            glColor3f(1.0,1.0,1.0);
            glutSolidSphere(MoonSize,8,8);   //Display Moon as a solid object
            glPopMatrix();                    
            glPopMatrix();
}

void Init(void)
{
            glClearColor(0.0,0.0,0.0,0.0);
            glClearDepth(10.0);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
}

void Display(void)
{
            glClear(GL_COLOR_BUFFER_BIT);
            RenderScene();
            glFlush();
            glutSwapBuffers();
}


void Reshape(int x, int y)
{
            if (y == 0) return;
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
            glMatrixMode(GL_MODELVIEW);
            glViewport(0,0,x,y);
            Display();
}

void Idle(void)
{
            day += daySpeed;
            year += yearSpeed;
            moonItsSelf += moonItsSelfSpeed;
            moonAroundEarth += moonAroundEarthSpeed;
            Display();
}

int main(int argc, char* argv[])
{
            glutInit(&argc, argv);
            glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
            glutInitWindowSize(700,700);
            glutCreateWindow("Earth Revoles");
            Init();
            glutReshapeFunc(Reshape);
            glutDisplayFunc(Display);
            glutIdleFunc(Idle);
            glutMainLoop();
            return 0;

}

and thus far year and day calculation

void Idle(void)
{
            day += daySpeed;
            year += yearSpeed;
            moonItsSelf += moonItsSelfSpeed;
            moonAroundEarth += moonAroundEarthSpeed;
            Display();
}

Upvotes: 0

Views: 62

Answers (1)

the busybee
the busybee

Reputation: 12683

It's not the macro value that is not constant, it's the expression on the right side of this line:

float yearSpeed = Dayyar / 360 * daySpeed * SpeedMultiplicator;

The values Dayyar, 360, and SpeedMultiplicator are constant.

But daySpeed is not constant, as it is a variable.

The error message is misleading, that's life. :-(


BTW, the messages you show seem not to match the source you show.

Upvotes: 2

Related Questions