OpenGL:从Visual Studio源代码到XCode 4?

时间:2012-01-26 02:31:23

标签: c++ xcode opengl

我正在上课,我们将学习OpenGL。教授正在使用Visual Studio,但是我的Parallels安装效果不佳,所以我决定使用XCode(无论如何我都喜欢)。我已经有基本的示例代码工作,但我遇到了运行教授给我们的例子的问题。这是:

#include <glut.h>           //must be included for OpenGL
#include <gl\gl.h>              //must be included for OpenGL

#include <time.h>           //must be included for time functions
#include <iostream>         //must be included for console input/output
using namespace std;

#define WINDOW_WID 800
#define WINDOW_HEI 600

int randomPx, randomPy;
/////////////////////////////////////////

void myInit(void) 
{
    randomPx = 400;
    randomPy = 300;
    glClearColor (1.0, 1.0, 1.0, 0.0);   // set background color to black
    glShadeModel (GL_FLAT);
}



void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);        // clear the screen    

    glColor3f(1,0,0);                   //set the drawing color
    glPointSize(10);                    //set the point size
    glBegin(GL_POINTS);
        glVertex2d(randomPx,randomPy);          //Set the position of the vertex
    glEnd();

    glutSwapBuffers ();                 //put everything on your screen
}


void myReshape ( int w, int h)
{
    glViewport (0, 0, w, h);
    glMatrixMode (GL_PROJECTION);           // set "camera type"
    glLoadIdentity ();                      // clear the matrix

    glOrtho(0.0, w, 0.0, h, -1.0, 1.0);     // viewing transformation 
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
}

void Animation()
{
    srand(time(NULL)+rand());               //set the seed for your rand function
    randomPx = rand()%WINDOW_WID;
    randomPy = rand()%WINDOW_HEI;
    Sleep(200);                             //put the program to sleep for 200 ms

    myDisplay();
}

void myMouse(int button, int state, int x, int y)
{    
    y = WINDOW_HEI - y;
    switch (button) 
    {
        case GLUT_LEFT_BUTTON:              //when left mouse button is clicked
            if (state == GLUT_DOWN)         
            {
                cout << "When mouse is up, animation starts\n";
            }
            else if(state == GLUT_UP)
            {
                glutIdleFunc(Animation);    //do Animation when idle
            }
            break;
        case GLUT_RIGHT_BUTTON:             //when right mouse button is clicked
            if (state == GLUT_DOWN)
            {
                glutIdleFunc(NULL);         //do nothing when idle
            }
            break;
        case GLUT_MIDDLE_BUTTON:
            if (state == GLUT_DOWN)
            {
                exit (-1);                  //exit your program
            }
            break;
    }
    myDisplay();
} 

void myMotion(int x, int y)
{   
    y = WINDOW_HEI - y;


    myDisplay();
}

void myKeyboard(unsigned char key, int x, int y)
{
    //TODO 

    myDisplay();
}

/*
 * Request double buffer display mode.
 * Register mouse input callback functions
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);                            // initialize the toolkit
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);     // set display mode
    // glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);     // set display mode
    glutInitWindowSize (WINDOW_WID, WINDOW_HEI);                    // set screen window size
    glutInitWindowPosition (100, 100);       // set window position on screen
    glutCreateWindow (argv[0]);              // open the screen window
    myInit ();
    glutDisplayFunc(myDisplay);              // register redraw function
    glutReshapeFunc(myReshape);              // register reshape function

    glutMouseFunc(myMouse); //GLUT provides a way for you to register the function that will be responsable for processing events generated by mouse clicks.
    glutMotionFunc(myMotion); //There are two types of motion that GLUT handles: active and passive motion. Active motion occurs when the mouse is moved and a button is pressed. 
    glutKeyboardFunc(myKeyboard);
    //glutPassiveMotionFunc(myPassiveMotion); //Passive motion is when the mouse is moving but no buttons are pressed. If an application is tracking motion, an event will be generated per frame during the period that the mouse is moving.
    //glutEntryFunc(processMouseEntry); //GLUT is also able to detect when the mouse leaves or enters the window region. A callback function can be registered to handle these two events.

    glutMainLoop();                          // go into a perpetual loop
    return 0;
}

我取出了imports语句,并将其替换为导入,例如XCode OpenGL代码:

#include <iostream>
#include <GLUT/glut.h>
#include <time.h>

我没有收到任何错误消息,但是当我运行应用程序并单击窗口时,它不会启动它应该的动画。 (我知道它的工作原理,因为在Visual Studio中,它在类中的其他所有计算机上运行正常。)

它通过打印输出来注册屏幕上的点击:“当鼠标启动时,动画开始”

但在那之后,它只是给了我Spinning Beachball of Death,直到我阻止它通过XCode运行。那么我还需要调整一些其他方法来实现这个目标吗?

2 个答案:

答案 0 :(得分:0)

首先,为什么要为导入gl.h使用反斜杠?其次,您应该将代码链接到GLUT和OpenGL库,因此您应该将它们包含/导入为OpenGL / gl.h和GLUT / glut.h。但你的主要问题是睡眠功能。它在mac上不存在,因此,当您尝试设置动画时,代码会崩溃。如果您要等待200毫秒,请使用[NSThread sleepForTimeInterval:0.2]。但是您必须将文件扩展名更改为.mm才能使用objective-c代码并导入Foundation库以使用NSThread类。

答案 1 :(得分:0)

关于睡眠..使用

#include <unistd.h> //来自C. 并使用

sleep(0.2) 

这是秒......所以2000 miliseconds = 0.2 seconds ..所以 只是从睡眠(2000)切换到睡眠(0.2)