Search code examples
c++randomnumbersmax3ds

random number generator being called when it shouldn't, i think


#include "std_lib_facilities.h"
#include <GL/glut.h>

static float dx=0.0;
static float dz=10;
static float points[2];
static float cx=0.0;
static float cy=0.0;
static float cz=0.0;
static float spin=0.0;
static float rotation=1.0;


int readObject()
{

    ifstream inputFile("spikeBall.ogl");

    if(!inputFile)
    {
        cerr << "cannot open file spikeBall.ogl" << endl;
        return -1;
    }
    int count = 0;
    float x,y,z;

    do{
        inputFile >> count;
        glBegin(GL_POLYGON);
        for(int i=0;i<count;i++)
        {
            inputFile >> x >> y >> z;
            glVertex3f(((x/5)+cx),((y/5)+cy),((z/5)+cz));
        }
        glEnd();

    }while(count!=0);

    inputFile.close();
}

void init(void)
{
    glClearColor(0.0,0.0,0.0,0.0);
}

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

    glLoadIdentity();
    gluLookAt (0,0,20, 0.0,0.0,0.0, 0.0,1.0,0.0);

    glPushMatrix();

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    GLfloat aMaterial[]={0.1745,0.01175,0.01175,1.0};
    GLfloat bMaterial[]={0.61424,0.04136,0.04136,1.0};
    GLfloat cMaterial[]={0.727811,0.626959,0.626959,1.0};
    GLfloat dMaterial=40;

    glMaterialfv(GL_FRONT,GL_AMBIENT,aMaterial);
    glMaterialfv(GL_FRONT,GL_DIFFUSE,bMaterial);
    glMaterialfv(GL_FRONT,GL_SPECULAR,cMaterial);
    glMaterialf(GL_FRONT,GL_SHININESS,dMaterial);

    readObject();
    glRotatef(spin,0.0,1.0,0.0);

    glPopMatrix();
    glFlush();
}

void idleFunc(void)
{
    spin+=rotation;
    if(spin>360.0)
    {
        spin-=360.0;
    }
    cz+=0.1;
    if(cz=20)
    {
        srand((unsigned int) time(NULL));
        for(int i=0;i<2;i++)
        {
            points[i] = (rand()%20)-10;
        }
        cx = points[0];
        cy = points[1];
        cz = 0;
        GLfloat lightPos[] = {cx,cy,(cz+2),1.0};
        glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
        std::cout << "cx:" << cx;
        std::cout << "cy:" << cy;
        std::cout << "cz:" << cz;
    }
    else
    {
    }
    glutPostRedisplay();
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0,20.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(300,300);
    glutInitWindowPosition(0,0);
    glutCreateWindow("3ds Max loader");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    init();
    glutIdleFunc(idleFunc);
    glutMainLoop();
    return(0);
}

i made a spiky sphere in 3ds max then exported it as .obj and through some python i got an .ogl file i want it to move forward towards the screen before returning to a random point on the x and y axis, but it just keeps bouncing around


Solution

  • cz+=0.1;
    if(cz=20)
    {
      [random generator code]
    }
    

    Two problems here:

    First you're assigning (=) 20 to cz instead of comparing (==) - even worse, this evaluates to true and triggers the code inside the brackets all the time. You should perhaps consider switching the compiler to a more verbose warning mode and/or reading the warnings more carefully.

    Second you should never compare floats with strict equality but using <= or >= because you will most probably miss the point because of floating point arithmetic - see this link f.e. if you're unfamiliar with that problem. So the corrected code should look like this:

    cz += 0.1;
    if (cz >= 20)
    {
       [...]
    }