开发者

where do i put in my rotate function

#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);
}

iat the moment it's just moving forward, i want it to spin too, but everywhere i put it, it's either not spinning or it makes the sphere look like it just blew up ( like when i put it after glEnd())


I would create an update loop where you could call all of your logic functions, and create a new seperate Rotate() function then call it there. The general format for games programming I usually follow is:

main()
{
  init();
  while(gameOn)
  {
    update();
    draw();
  }
}
init()
{
  //create textures, etc.
}
update()
{
  //do logic
}
draw()
{
  //display on screen after update
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜