2012-01-26 44 views
1

我在一個我們將學習OpenGL的課程中。這位教授正在使用Visual Studio,但對於我安裝的Parallels來說工作不太好,所以我只是決定使用XCode(我更喜歡它)。我有基本的示例代碼工作,但我遇到了教授給我們的示例。那就是:OpenGL:從Visual Studio源代碼到XCode 4?

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

我拿出進口報表,並與進口例如XCode的OpenGL的代碼替換他們:

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

而且我沒有得到任何錯誤消息,但是當我運行該應用程序,然後單擊該窗口,它不會啓動它應該的動畫。 (我知道它的工作原理,因爲在Visual Studio中的類所有其他計算機上它工作得很好。)

它確實註冊通過打印屏幕上點擊:「當鼠標後,動畫開始」

但是之後,它只是給我旋轉的死亡沙灘球,直到我停止它通過XCode運行。那麼還有什麼我必須調整才能實現這個目標?

回答

0

首先,你爲什麼要爲你的導入gl.h使用反斜槓?其次,你應該將你的代碼鏈接到GLUT和OpenGL庫,所以你應該包含/導入它們作爲OpenGL/gl.h和GLUT/glut.h。但是你的主要問題是睡眠功能。它在mac上不存在,因此,您的代碼在嘗試動畫時會崩潰。如果您想等待200毫秒,請使用[NSThread sleepForTimeInterval:0.2]。但是,您必須將文件擴展名更改爲.mm才能使用Objective-C代碼,並且還需要導入基礎庫以使用NSThread類。

+0

反斜槓來自教授的代碼。他在下面寫道,他是如何替換他的系統的。 – datenwolf

+1

謝謝你指出。沒有合併Objective-C的實際答案我發現是小寫睡眠(),但這是在幾秒鐘內(應用程序凍結,因爲我認爲它是以毫秒爲單位,但我睡了200秒)或usleep(),以微秒運行。 – Derek

0

關於睡眠..使用

#include <unistd.h> //從C 並使用

sleep(0.2) 

至極是在秒..所以2000 miliseconds = 0.2 seconds ..所以 剛剛從睡眠(2000)切換到睡眠(0.2)