2011-04-19 4 views
2

자바를 사용하여 OpenGL을 배웠습니다. 이제 C++로 전환하려고합니다. 윈도우를 설치하고 쿼드를 렌더링하려하지만 표시되지 않습니다. 방금 검은 색 화면이 나타납니다. OpenGL Superbible의 예제를 시작으로 사용하고 있습니다. 나는 모든 윈도우 설치 물건이 작동 확신OpenGL이 내 쿼드를 그려주지 않습니다

#include <stdio.h> 

#define GLEW_STATIC 
#include "include\GL\glew.h" 
#include "include\GL\wglew.h" 

#pragma comment(lib, "lib\\glew32s.lib") 
#pragma comment(lib, "opengl32.lib") 
#pragma comment(lib, "glu32.lib") 

HWND   g_hWnd; 
HGLRC  g_hRC; 
HDC   g_hDC; 
HINSTANCE g_hInstance; 
WNDCLASS  g_windClass; 
RECT   g_windowRect; 
bool   g_ContinueRendering; 

void RenderScene(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 

    glBegin(GL_QUADS); 
    glVertex3f(-10.0f, -10.0f, 5.0f); 
    glVertex3f(-10.0f, 10.0f, 5.0f); 
    glVertex3f(10.0f, 10.0f, 5.0f); 
    glVertex3f(10.0f, -10.0f, 5.0f); 
    glEnd(); 
    printf("%s", gluErrorString(glGetError())); 
    SwapBuffers(g_hDC); 
} 

/////////////////////////////////////////////////////////////////////////////// 
// Window has changed size, or has just been created. In either case, we need 
// to use the window dimensions to set the viewport and the projection matrix. 
void ChangeSize(int w, int h) 
{ 
    glViewport(0, 0, w, h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(90.0f, float(w)/float(h), 1.0f, 500.0f); 
} 

/////////////////////////////////////////////////////////////////////////////// 
// Callback functions to handle all window functions this app cares about. 
// Once complete, pass message on to next app in the hook chain. 
LRESULT CALLBACK WndProc( HWND hWnd,  // Handle For This Window 
          UINT uMsg,  // Message For This Window 
          WPARAM wParam,  // Additional Message Information 
          LPARAM lParam)  // Additional Message Information 
{ 
    unsigned int key = 0; 

    // Handle relevant messages individually 
    switch(uMsg) 
    { 
    case WM_ACTIVATE: 
    case WM_SETFOCUS: 
     RenderScene(); 
     return 0; 
    case WM_SIZE: 
     ChangeSize(LOWORD(lParam),HIWORD(lParam)); 
     RenderScene(); 
     break; 
    case WM_CLOSE: 
     g_ContinueRendering = false; 
     PostQuitMessage(0); 
     return 0; 
    default: 
     // Nothing to do now 
     break; 
    } 

    // Pass All Unhandled Messages To DefWindowProc 
    return DefWindowProc(hWnd,uMsg,wParam,lParam); 
} 

bool setupWindow(int nWidth, int nHeight) 
{ 
    bool bRetVal = true; 

    int nWindowX = 0; 
    int nWindowY = 0; 
    int nPixelFormat = -1; 
    PIXELFORMATDESCRIPTOR pfd; 

    DWORD dwExtStyle; 
    DWORD dwWindStyle; 

    HINSTANCE g_hInstance = GetModuleHandle(NULL); 

    TCHAR szWindowName[50] = TEXT("Block Redux"); 
    TCHAR szClassName[50] = TEXT("OGL_CLASS"); 

    // setup window class 
    g_windClass.lpszClassName = szClassName;    // Set the name of the Class 
    g_windClass.lpfnWndProc = (WNDPROC)WndProc; 
    g_windClass.hInstance  = g_hInstance;    // Use this module for the module handle 
    g_windClass.hCursor  = LoadCursor(NULL, IDC_ARROW);// Pick the default mouse cursor 
    g_windClass.hIcon   = LoadIcon(NULL, IDI_WINLOGO);// Pick the default windows icons 
    g_windClass.hbrBackground = NULL;      // No Background 
    g_windClass.lpszMenuName = NULL;      // No menu for this window 
    g_windClass.style   = CS_HREDRAW | CS_OWNDC |  // set styles for this class, specifically to catch 
           CS_VREDRAW;     // window redraws, unique DC, and resize 
    g_windClass.cbClsExtra = 0;       // Extra class memory 
    g_windClass.cbWndExtra = 0;       // Extra window memory 

    // Register the newly defined class 
    if(!RegisterClass(&g_windClass)) 
     bRetVal = false; 

    dwExtStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; 
    dwWindStyle = WS_OVERLAPPEDWINDOW; 
    ShowCursor(TRUE); 

    g_windowRect.left = nWindowX; 
    g_windowRect.right = nWindowX + nWidth; 
    g_windowRect.top = nWindowY; 
    g_windowRect.bottom = nWindowY + nHeight; 

    // Setup window width and height 
    AdjustWindowRectEx(&g_windowRect, dwWindStyle, FALSE, dwExtStyle); 

    //Adjust for adornments 
    int nWindowWidth = g_windowRect.right - g_windowRect.left; 
    int nWindowHeight = g_windowRect.bottom - g_windowRect.top; 

    // Create window 
    g_hWnd = CreateWindowEx(dwExtStyle,  // Extended style 
          szClassName, // class name 
          szWindowName, // window name 
          dwWindStyle |   
          WS_CLIPSIBLINGS | 
          WS_CLIPCHILDREN,// window stlye 
          nWindowX,  // window position, x 
          nWindowY,  // window position, y 
          nWindowWidth, // height 
          nWindowHeight, // width 
          NULL,   // Parent window 
          NULL,   // menu 
          g_hInstance, // instance 
          NULL);   // pass this to WM_CREATE 

    // now that we have a window, setup the pixel format descriptor 
    g_hDC = GetDC(g_hWnd); 

    // Set a dummy pixel format so that we can get access to wgl functions 
    SetPixelFormat(g_hDC, 1,&pfd); 
    // Create OGL context and make it current 
    g_hRC = wglCreateContext(g_hDC); 
    wglMakeCurrent(g_hDC, g_hRC); 

    if (g_hDC == 0 || 
     g_hDC == 0) 
    { 
     bRetVal = false; 
     printf("!!! An error occured creating an OpenGL window.\n"); 
    } 

    // Setup GLEW which loads OGL function pointers 
    GLenum err = glewInit(); 
    if (GLEW_OK != err) 
    { 
     /* Problem: glewInit failed, something is seriously wrong. */ 
     bRetVal = false; 
     printf("Error: %s\n", glewGetErrorString(err)); 
    } 
    const GLubyte *oglVersion = glGetString(GL_VERSION); 
    printf("This system supports OpenGL Version %s.\n", oglVersion); 

    // Now that extensions are setup, delete window and start over picking a real format. 
    wglMakeCurrent(NULL, NULL); 
    wglDeleteContext(g_hRC); 
    ReleaseDC(g_hWnd, g_hDC); 
    DestroyWindow(g_hWnd); 

    // Create the window again 
    g_hWnd = CreateWindowEx(dwExtStyle,  // Extended style 
          szClassName, // class name 
          szWindowName, // window name 
          dwWindStyle |   
          WS_CLIPSIBLINGS | 
          WS_CLIPCHILDREN,// window stlye 
          nWindowX,  // window position, x 
          nWindowY,  // window position, y 
          nWindowWidth, // height 
          nWindowHeight, // width 
          NULL,   // Parent window 
          NULL,   // menu 
          g_hInstance, // instance 
          NULL);   // pass this to WM_CREATE 

    g_hDC = GetDC(g_hWnd); 

    int nPixCount = 0; 

    // Specify the important attributes we care about 
    int pixAttribs[] = 
    { 
     WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering 
     WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window 
     WGL_COLOR_BITS_ARB,  24, // 8 bits of each R, G and B 
     WGL_DEPTH_BITS_ARB,  16, // 16 bits of depth precision for window 
     WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // Double buffered context 
     WGL_SAMPLE_BUFFERS_ARB, GL_TRUE, // MSAA on 
     WGL_SAMPLES_ARB,  8, // 8x MSAA 
     WGL_PIXEL_TYPE_ARB,  WGL_TYPE_RGBA_ARB, // pf should be RGBA type 
     0 // NULL termination 
    }; 

    // Ask OpenGL to find the most relevant format matching our attribs 
    // Only get one format back. 
    wglChoosePixelFormatARB(g_hDC, &pixAttribs[0], NULL, 1, &nPixelFormat, (UINT*)&nPixCount); 

    if(nPixelFormat == -1) 
    { 
     // Couldn't find a format, perhaps no 3D HW or drivers are installed 
     g_hDC = 0; 
     g_hDC = 0; 
     bRetVal = false; 
     printf("!!! An error occurred trying to find a pixel format with the requested attribs.\n"); 
    } 
    else 
    { 
     // Got a format, now set it as the current one 
     SetPixelFormat(g_hDC, nPixelFormat, &pfd); 

     GLint attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, 
          WGL_CONTEXT_MINOR_VERSION_ARB, 3, 
          0 }; 

     g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); 
     if (g_hRC == NULL) 
     { 
      printf("!!! Could not create an OpenGL 3.3 context.\n"); 
      attribs[3] = 2; 
      g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); 
      if (g_hRC == NULL) 
      { 
       printf("!!! Could not create an OpenGL 3.2 context.\n"); 
       attribs[3] = 1; 
       g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); 
       if (g_hRC == NULL) 
       { 
        printf("!!! Could not create an OpenGL 3.1 context.\n"); 
        attribs[3] = 0; 
        g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); 
        if (g_hRC == NULL) 
        { 
         printf("!!! Could not create an OpenGL 3.0 context.\n"); 
         printf("!!! OpenGL 3.0 and higher are not supported on this system.\n"); 
        } 
       } 
      } 
     } 

     wglMakeCurrent(g_hDC, g_hRC); 
    } 

    if (g_hDC == 0 || 
     g_hDC == 0) 
    { 
     bRetVal = false; 
     printf("!!! An error occured creating an OpenGL window.\n"); 
    } 

    // If everything went as planned, display the window 
    if(bRetVal) 
    { 
     ShowWindow(g_hWnd, SW_SHOW); 
     SetForegroundWindow(g_hWnd); 
     SetFocus(g_hWnd); 
     g_ContinueRendering = true; 
    } 

    return bRetVal; 
} 

/////////////////////////////////////////////////////////////////////////////// 
// Cleanup window, OGL context and related state 
// Called on exit and on error 
bool KillWindow() 
{ 
    bool bRetVal = true; 

    //Cleanup OGL RC 
    if(g_hRC) 
    { 
     wglMakeCurrent(NULL, NULL); 
     wglDeleteContext(g_hRC); 
     g_hRC = NULL; 
    } 

    // release the DC 
    if(g_hDC) 
    { 
     ReleaseDC(g_hWnd, g_hDC); 
     g_hDC = NULL; 
    } 

    // Destroy the window 
    if(g_hWnd) 
    { 
     DestroyWindow(g_hWnd); 
     g_hWnd = NULL;; 
    } 

    // Delete the window class 
    TCHAR szClassName[50] = TEXT("OGL_CLASS"); 
    UnregisterClass(szClassName, g_hInstance); 
    g_hInstance = NULL; 
    ShowCursor(TRUE); 
    return bRetVal; 
} 

/////////////////////////////////////////////////////////////////////////////// 
// Main rendering loop 
// Check for window messages and handle events, also draw scene 
void mainLoop() 
{ 
    MSG msg; 

    // Check for waiting mssgs 
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) 
    { 
     if (msg.message==WM_QUIT) 
     { 
      g_ContinueRendering = false; 
     } 
     else 
     { 
      // Deal with mssgs 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 
    else 
    { 
     RenderScene();  

    } 
} 

int main(int argc, char **argv) 
{ 
    if(setupWindow(800, 600)) 
    { 
     ChangeSize(800, 600); 

     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
     glEnable(GL_BLEND); 
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 

     while (g_ContinueRendering) 
     { 
      mainLoop(); 
     } 
    } 
    KillWindow(); 
    return 0; 
} 

:

여기 내 코드입니다. glClearColor를 변경하면 창 색상이 변경됩니다.

다른 사람이 무엇이 잘못되었는지 보길 바랍니다.

답변

3

당신을 뒤에 그려야합니다. 쿼드의 Z 값은 +5이지만 OpenGL의 경우 z 방향으로 전방이 음수입니다. Z 값을 -5로 변경하면 뭔가를 볼 수있을 것으로 생각됩니다.

또는 추가

glPushMatrix(); 
glTranslatef(0,0,-20.0f); 
//Your code for drawing. 
glBegin(GL_QUADS); 
... 
glEnd(); 
//Added 
glPopMatrix(); 
+0

하하하. 믿을 수 없어. 보낸 시간, 왜 내가 처음 생각하지 않았는지 모르겠다. – terryhau

+0

항상 그런 일이 일어난다. 그렇다고해서 OpenGL 프로그램을 어둡게 만들 때 좌절하지는 않습니다. – JCooper

0

나는 엉덩이에서 단지 쏘고있어.하지만 꼭지점의 순서를 바꾸려고 했니? 이것은 backface culling의 범인이 될 수 있습니다.

+0

가 주변에 여전히 아무것도 전환하지했습니다. – terryhau

4

코드를 보면 OpenGL 3.x를 사용하고 있습니다.

GL_QUADS은 (는) 더 이상 사용되지 않으므로 삼각형을 사용해보세요.

또한 최소 요구 사항 인 CreateContextAttrib을 전달해야합니다. 가능한 경우 새로운 버전이 자동으로 제공됩니다.

+0

감사합니다. 이것은 또한 문제의 일부였습니다. 이유는 궁금 해서요. @ben 3.3으로 설정하면 잘못된 연산 오류가 나옵니다. 또 하나. 1.0처럼 CreateContextAttrib을 전달하면 deprecated가 작동합니다. 그게 나에게 1.0 컨텍스트와 새로운 버전이 아니라는 것을 의미합니까? – terryhau

+0

@Terryhau : 버전 3.0을 요청하면 호환성 문맥이 생깁니다. > = 3.0을 요청하면 기본적으로 핵심 컨텍스트가 생성됩니다. 어쨌든 당신은 최신 버전을 얻을 수 있습니다 (모든 새로운 것들이 가능합니다). 사용되지 않는 기능과 새로운 기능이 모두 필요하면'CreateContextAttrib'에 추가 속성을 전달하여 버전과 관계없이 호환성 문맥을 켤 수 있습니다. 세부 사항은 [여기] (http://www.opengl.org/registry/specs/ARB/wgl_create_context.txt) –

+0

감사합니다. 좋아. – terryhau

관련 문제