2015-02-07 2 views
0

У меня проблема. Я написал код на понятном opengl и C++ без каких-либо библиотек (glut, glew ...), но он не работает. Функции дисплея displayCB многоугольник сOpengl macos fail

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(); 

и DonT отображения двух многоугольников с

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(); 

Можете ли вы мне помочь?

COMPILE С: г ++ -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, который использовался до OpenGL 3.0. Современный OpenGL, то есть OpenGL 3.0 или новее, а также OpenGL ES - использует значительно отличающийся API, который имеет очень мало общего с немедленным режимом. Я настоятельно рекомендую вам полностью перейти к немедленному режиму; на данный момент это мертвая технология. Есть много хороших учебников там для OpenGL 3+, в том числе:

+0

Это не обязательно использовать 'glFrustum()' или 'gluPerspective () ', если вы используете координаты клипа. –

+0

@duskwuff спасибо. Могу ли я сделать приложение, как это http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/, но в Mac OS системе? Без каких-либо библиотек. – Max

+0

nehe.gamedev.net устарел? – Max

Смежные вопросы