簡體   English   中英

OpenGL:從Visual Studio源代碼到XCode 4?

[英]OpenGL: From Visual Studio source code to XCode 4?

我正在上一堂課,我們將學習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;
}

我取出了import語句,並用導入替換了它們,例如XCode OpenGL代碼:

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

而且我沒有收到任何錯誤消息,但是當我運行該應用程序並單擊窗口時,它沒有啟動應有的動畫。 (我知道它是可行的,因為在該類中每台其他計算機上的Visual Studio中,它都可以正常工作。)

它確實通過打印輸出在屏幕上注冊點擊:“當鼠標向上時,動畫開始”

但是在那之后,它給了我死亡的旋轉沙灘球,直到我停止通過XCode運行它。 那么,我還需要調整一些東西才能使它正常工作嗎?

關於睡眠..使用

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

sleep(0.2) 

wich is at seconds .. so 2000 miliseconds = 0.2 seconds ..所以只需從sleep(2000)切換到sleep(0.2)

首先,為什么要在導入gl.h中使用反斜杠? 其次,應將代碼鏈接到GLUT和OpenGL庫,因此應將它們包括/導入為OpenGL / gl.h和GLUT / glut.h。 但是您這里的主要問題是睡眠功能。 它在Macs上不存在,因此,當您嘗試制作動畫時,您的代碼將崩潰。 如果要等待200毫秒,請改用[NSThread sleepForTimeInterval:0.2] 但是您必須將文件擴展名更改為.mm才能使用Objective-C代碼,並且還必須導入Foundation庫以使用NSThread類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM