2012-01-26 4 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 msを待つ場合は、代わりに[NSThread sleepForTimeInterval:0.2]を使用してください。しかし、Object-Cコードを使用するためにファイル拡張子を.mmに変更する必要があります。また、FoundationライブラリをインポートしてNSThreadクラスを使用する必要があります。睡眠について

+0

バックスラッシュは教授のコードから来ています。その下に彼は書いた、彼は彼のシステムのためにそれをどのように置き換えましたか。 – datenwolf

+1

それを指摘してくれてありがとう。実際の答えはObjective-Cを組み込んでいないので、小文字のsleep()が見つかりましたが、数秒で表示されます(アプリは凍っていました(ミリ秒単位だと思っていましたが、200秒間スリープしていました)マイクロセカンドで動作します。 – Derek

0

.. C FROM

#include <unistd.h> //を使用して

sleep(0.2) 

ウィッヒは秒である使用。..ので、2000 miliseconds = 0.2 seconds ...そう がちょうどにスリープ(2000年)からの切り替えsleep(0.2)