OpenGL不使用立即模式或VBO渲染任何内容

时间:2014-04-13 20:05:48

标签: visual-studio opengl

我在一天前在Visual Studio中开始了一个新项目,但我无法使用VBO或立即模式绘制任何内容。我认为问题在于我的初始化。奇怪的是,我仍然可以使用glClearColor()更改背景颜色,但没有其他工作。这是我的即时模式代码:

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0,1.0,0.1,100.0);
 glViewport(0,0,512,512);

 glEnable(GL_DEPTH_TEST);
 glDepthFunc(GL_LEQUAL);
 glMatrixMode(GL_MODELVIEW);

绘图代码:

        glBegin(GL_TRIANGLES);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f(0.0f,0.0f,0.0f);
        glVertex3f(1.0f,0.0f,0.0f);
        glVertex3f(1.0f,1.0f,0.0f);
        glEnd();

        glClearColor(1.0f,0.0f,0.0f,0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SwapBuffers(hdc);

我的窗口设置:

void InitOpenGL(HDC* hdc,HWND* hwnd)
{
static PIXELFORMATDESCRIPTOR pfd =
{
    sizeof(pfd),
    1,
    PFD_DRAW_TO_WINDOW |
    PFD_SUPPORT_OPENGL |
    PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,
    32,
    0,0,0,0,0,0,
    8,
    0,
    0,
    0,0,0,0,
    8,
    0,0,
    PFD_MAIN_PLANE,
    0,
    0,0,0
};

int pix;
HGLRC hrc;

*hdc = GetDC(*hwnd);
pix = ChoosePixelFormat(*hdc,&pfd);
SetPixelFormat(*hdc,pix,&pfd);
hrc = wglCreateContext(*hdc);
wglMakeCurrent(*hdc,hrc);
glewInit();

}



BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance;

   hWnd = CreateWindow(szWindowClass, szTitle, WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
      CW_USEDEFAULT, 0, 768, 768, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   InitOpenGL(&hdc,&hWnd);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RENDERENGINE));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_RENDERENGINE);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

1 个答案:

答案 0 :(得分:2)

在绘图程序结束时清除帧缓冲区将删除之前绘制的任何内容。

在开头清除缓冲区:

    glClearColor(1.0f,0.0f,0.0f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(1.0f,0.0f,0.0f);
    glVertex3f(1.0f,1.0f,0.0f);
    glEnd();



    SwapBuffers(hdc);

修改:the projection Matrix

gluPerspective

如您所见,眼睛位置为0,0,0,绿色星形是您实际绘制的位置。我让你找到一个更正确的位置......