2015-02-07 69 views
0

我有問題。我在沒有任何庫的情況下編寫了清晰的opengl和C++代碼(過剩,笨拙...),但它不起作用。與Opengl macos失敗

glColor3f(1.0f, 0.0f, 0.0f); 
    glBegin(GL_POLYGON); 
    glVertex2f(-0.5f, -0.5f); 
    glVertex2f(0.5f, -0.5f); 
    glVertex2f(0.5f, 0.5f); 
    glVertex2f(-0.5f, 0.5f); 
    glEnd(); 

和 功能displayCB顯示多邊形不顯示兩幅polygone與

glColor3f(1.0f, 0.0f, 0.0f); 
    glLoadIdentity();         // Reset The Current Modelview Matrix 
     glTranslatef(-1.5f,0.0f,-6.0f);      // Move Left 1.5 Units And Into The Screen 6.0 
     glBegin(GL_TRIANGLES);        // Drawing Using Triangles 
      glVertex3f(0.0f, 1.0f, 0.0f);     // Top 
      glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left 
      glVertex3f(1.0f,-1.0f, 0.0f);     // Bottom Right 
     glEnd();           // Finished Drawing The Triangle 
     glTranslatef(3.0f,0.0f,0.0f);      // Move Right 3 Units 
     glBegin(GL_QUADS);         // Draw A Quad 
      glVertex3f(-1.0f, 1.0f, 0.0f);     // Top Left 
      glVertex3f(1.0f, 1.0f, 0.0f);     // Top Right 
      glVertex3f(1.0f,-1.0f, 0.0f);     // Bottom Right 
      glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left 
     glEnd(); 

你能幫助我嗎?

編譯:G ++ -g -o opengl1 opengl1.cxx -lGLU -lGL -lX11

#include <stdio.h> 
    #include <stdlib.h> 
    #include <ctype.h> 
    #include <sys/time.h> 
    #define GL_GLEXT_PROTOTYPES 
    #define GLX_GLXEXT_PROTOTYPES 
    #include <GL/gl.h> 
    #include <GL/glu.h> 
    #include <GL/glx.h> 

    struct MyWin 
    { 
     Display *display; 
     Window win; 
     bool  displayed; 
     int  width; 
     int  height; 
    }; 


    const int WIN_XPOS = 256; 
    const int WIN_YPOS = 64; 
    const int WIN_XRES = 600; 
    const int WIN_YRES = 600; 
    const int NUM_SAMPLES = 4; 


    MyWin  Win; 


    double elapsedMsec(const struct timeval &start, const struct timeval &stop) 
    { 
     return ((stop.tv_sec - start.tv_sec ) * 1000.0 + 
       (stop.tv_usec - start.tv_usec)/1000.0); 
    } 


    double elapsedUsec(const struct timeval &start, const struct timeval &stop) 
    { 
     return ((stop.tv_sec - start.tv_sec ) * 1000000.0 + 
      (stop.tv_usec - start.tv_usec)); 
    } 


    /// check() - Check for GL errors, and report any queued 

    void check(const char hdr[] = "") 
{ 
    int err; 

    while ((err = glGetError()) != GL_NO_ERROR) 
    fprintf(stderr, "OpenGL Error at %s: %s\n", hdr, gluErrorString(err)); 
} 



void displayCB() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    //--------------------------------------------------- 
    // FIXME: Insert GL draw code here 
    //--------------------------------------------------- 
    glColor3f(1.0f, 0.0f, 0.0f); 
    glLoadIdentity();         // Reset The Current Modelview Matrix 
    glTranslatef(-1.5f,0.0f,-6.0f);      // Move Left 1.5 Units And Into The Screen 6.0 
    glBegin(GL_TRIANGLES);        // Drawing Using Triangles 
     glVertex3f(0.0f, 1.0f, 0.0f);     // Top 
     glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left 
     glVertex3f(1.0f,-1.0f, 0.0f);     // Bottom Right 
    glEnd();           // Finished Drawing The Triangle 
    glTranslatef(3.0f,0.0f,0.0f);      // Move Right 3 Units 
    glBegin(GL_QUADS);         // Draw A Quad 
     glVertex3f(-1.0f, 1.0f, 0.0f);     // Top Left 
     glVertex3f(1.0f, 1.0f, 0.0f);     // Top Right 
     glVertex3f(1.0f,-1.0f, 0.0f);     // Bottom Right 
     glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left 
    glEnd();    
    // glBegin(GL_POLYGON); 
    // glVertex2f(-0.5f, -0.5f); 
    // glVertex2f(0.5f, -0.5f); 
    // glVertex2f(0.5f, 0.5f); 
    // glVertex2f(-0.5f, 0.5f); 
    // glEnd(); 
    // Display it 
    glXSwapBuffers(Win.display, Win.win); 

    check("displayCB()"); 
} 

//---------------------------------------------------------------------------- 

void keyboardCB(KeySym sym, unsigned char key, int x, int y, 
       bool &setting_change) 
{ 
    switch (tolower(key)) 
    { 
    case 27: 
     // ESCape - We're done! 
     exit (0); 
     break; 

    case 'k': 
     printf("You hit the 'k' key\n"); 
     break; 

    case 0: 
     switch (sym) 
     { 
     case XK_Left : 
      printf("You hit the Left Arrow key\n"); 
      break; 

     case XK_Right : 
      printf("You hit the Right Arrow key\n"); 
      break; 
     } 
     break; 
    } 
} 

//---------------------------------------------------------------------------- 

void reshapeCB(int width, int height) 
{ 
    Win.width = width; 
    Win.height = height; 
} 

//---------------------------------------------------------------------------- 

XVisualInfo *chooseVisual(Display *display) 
{ 
    int attribs [ 100 ] ; 
    int n = 0 ; 

    // Request 24-bit color with alpha 
    attribs [n++] = GLX_RGBA ; 
    attribs [n++] = GLX_RED_SIZE ; attribs [n++] = 8 ; 
    attribs [n++] = GLX_GREEN_SIZE ; attribs [n++] = 8 ; 
    attribs [n++] = GLX_BLUE_SIZE ; attribs [n++] = 8 ; 

    // Request 24-bit depth buffer 
    attribs [n++] = GLX_DEPTH_SIZE ; attribs [n++] = 24 ; 

    // Request 4 multisamples per pixel 
    attribs [n++] = GLX_SAMPLE_BUFFERS ; attribs [n++] = 1 ; 
    attribs [n++] = GLX_SAMPLES  ; attribs [n++] = NUM_SAMPLES ; 

    // Request double-buffering 
    attribs [n++] = GLX_DOUBLEBUFFER ; 
    attribs [n++] = None ; 

    return glXChooseVisual(display, DefaultScreen(display), attribs); 
} 

//---------------------------------------------------------------------------- 

void createWindow() 
{ 
    // Init X and GLX 
    Win.displayed = false; 
    Display *display = Win.display = XOpenDisplay(":0.0"); 
    if (!display) 
    printf("Cannot open X display\n"); 

    int screen = DefaultScreen(display); 
    Window root_win = RootWindow(display, screen); 

    if (!glXQueryExtension(display, 0, 0)) 
    printf("X Server doesn't support GLX extension\n"); 

    // Pick a visual 
    XVisualInfo *visinfo = chooseVisual(display); 
    if (visinfo == 0) 
    printf("glXChooseVisual failed\n"); 

    // Describe the visual 
    printf("Window Visual 0x%.2x\n", unsigned(visinfo->visualid)); 

    // Create the X window 
    XSetWindowAttributes winAttr ; 

    winAttr.event_mask = StructureNotifyMask | KeyPressMask ; 
    winAttr.background_pixmap = None ; 
    winAttr.background_pixel = 0 ; 
    winAttr.border_pixel  = 0 ; 

    winAttr.colormap = XCreateColormap(display, root_win, 
             visinfo->visual, AllocNone); 

    unsigned int mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask; 

    Window win = Win.win = XCreateWindow (display, root_win, 
             WIN_XPOS, WIN_YPOS, 
             WIN_XRES, WIN_YRES, 0, 
             visinfo->depth, InputOutput, 
             visinfo->visual, mask, &winAttr) ; 

    XStoreName(Win.display, win, "My GLX Window"); 

    // Create an OpenGL context and attach it to our X window 
    GLXContext context = glXCreateContext(display, visinfo, NULL, 1) ; 

    if (! glXMakeCurrent(display, win, context)) 
    printf("glXMakeCurrent failed.\n"); 

    if (! glXIsDirect (display, glXGetCurrentContext())) 
    printf("Indirect GLX rendering context obtained\n"); 

    // Display the window 
    XMapWindow(display, win); 

    if (! glXMakeCurrent(display, win, context)) 
    printf("glXMakeCurrent failed.\n"); 

    check("createWindow()"); 

    printf("Window Size = %d x %d\n", WIN_XRES, WIN_YRES); 
    printf("Window Samples = %d\n", NUM_SAMPLES); 
} 

//---------------------------------------------------------------------------- 

void processXEvents(Atom wm_protocols, Atom wm_delete_window) 
{ 
    bool setting_change = false; 

    while (XEventsQueued(Win.display, QueuedAfterFlush)) 
    { 
    XEvent event; 

    XNextEvent(Win.display, &event); 

    if(event.xany.window != Win.win) 
     continue; 

    switch (event.type) 
    { 
     case MapNotify: 
     { 
      Win.displayed = true; 
      break; 
     } 
     case ConfigureNotify: 
     { 
      XConfigureEvent &cevent = event.xconfigure; 
      reshapeCB(cevent.width, cevent.height); 
      break; 
     } 
     case KeyPress: 
     { 
      char  chr; 
      KeySym symbol; 
      XComposeStatus status; 

      XLookupString(&event.xkey, &chr, 1, &symbol, &status); 

      keyboardCB(symbol, chr, event.xkey.x, event.xkey.y, 
         setting_change); 
      break; 
     } 
     case ClientMessage: 
     { 
      if (event.xclient.message_type  == wm_protocols && 
       Atom(event.xclient.data.l[0]) == wm_delete_window) 
      { 
      //printf("Received WM_DELETE_WINDOW\n"); 
      exit(0); 
      } 
      break; 
     } 
    } 
    } 
} 

//---------------------------------------------------------------------------- 

void mainLoop() 
{ 
    // Register to receive window close events (the "X" window manager button) 
    Atom wm_protocols  = XInternAtom(Win.display, "WM_PROTOCOLS" , False); 
    Atom wm_delete_window = XInternAtom(Win.display, "WM_DELETE_WINDOW", False); 
    XSetWMProtocols(Win.display, Win.win, &wm_delete_window, True); 

    while (1) 
    { 
    // Redraw window (after it's mapped) 
    if (Win.displayed) 
     displayCB(); 

    // Update frame rate 
    static timeval last_xcheck = {0,0}; 
    struct timeval now; 
    gettimeofday(&now, 0); 

    // Check X events every 1/10 second 
    if (elapsedMsec(last_xcheck, now) > 100) 
    { 
     processXEvents(wm_protocols, wm_delete_window); 
     last_xcheck = now; 
    } 
    } 
} 

//---------------------------------------------------------------------------- 

int main(int argc, char *argv[]) 
{ 
    // Init globals 
    Win.width = WIN_XRES, Win.height = WIN_YRES; 

    // Create context and window 
    createWindow(); 

    // Init OpenGL 
    glViewport (0, 0, Win.width, Win.height); 
    glColorMask (1,1,1,1); 
    glClearColor(0,0,1,1); 
    glClear  (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Go 
    printf("Valid keys: Left, Right, k, ESC\n"); 
    printf("Press ESC to quit\n"); 

    mainLoop(); 

    return 0; 
} 

回答

1

這裏有三個問題。

第一:你還沒有成立了GL視(例如,使用glFrustum()gluPerspective()),所以投影矩陣沒有設置適當做3D圖形。因此,試圖在3D中繪製模型將無法正常工作。

第二個:您在這裏得到的代碼是針對X11窗口系統編寫的,而不是Mac OS X.雖然它可以在Mac OS上編譯和排序,但它不會在本機運行,也不會表現得特別好。您真的很想使用一些接口,它允許您與本機Mac OS X GL實現(如SDL,GLUT/GLEW或AGL)進行交互。 (請記住,沒有使用GL「沒有庫」這樣的事情,OpenGL本身就是一個庫,就像libX11一樣;使用額外的實用程序庫來刪除某些OS的內容並沒有什麼不對, )

第三個:您目前正在學習如何使用OpenGL 3.0中的OpenGL即時模式。現代OpenGL - 即OpenGL 3.0或更高版本以及OpenGL ES - 使用與直接模式幾乎沒有共同之處的截然不同的API。我強烈建議您完全跳過即時模式;在這一點上它是一種死亡技術。有一些很好的教程在那裏對OpenGL 3+,其中包括:

+0

這是沒有必要使用'glFrustum()'或'gluPerspective( )'如果你使用的是剪輯空間座標。 – 2015-02-07 20:11:01

+0

@duskwuff謝謝。 我可以使應用程序,像這樣http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/,但在Mac OS系統?沒有任何庫。 – Max 2015-02-08 00:01:40

+0

nehe.gamedev.net已過時? – Max 2015-02-08 00:11:54