繁体   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