T
TComponent
Unregistered / Unconfirmed
GUEST, unregistred user!
我把一个Delphi的OpenGL程序改成CB的
总是不对
不知怎么回事
源程序如下
BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;
ppfd = &pfd;
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->iPixelType = PFD_TYPE_RGBA;
ppfd->cColorBits = 8;
//ppfd->cRedBits = 8;
//ppft->cRedShift = 8;
//ppfd->cGreenBits = 8;
//ppft->cGreenShift = 8;
//ppfd->cBlueBits = 8;
//ppft->cBlueShift = 8;
//ppfd->cAlphaBits = 8;
//ppft->cAlphaShift = 8;
ppfd->cAccumBits = 0;
//ppfd->cAccumRedBits =0;
//ppfd->cAccumGreenBits =0;
//ppfd->cAccumBlueBits =0;
//ppfd->cAccumAlphaBits =0;
ppfd->cDepthBits = 16;
ppfd->cStencilBits = 0;
//ppfd->cAuxBuffers
//ppfd->iLayerType
//ppfd->bReserved
ppfd->dwLayerMask = PFD_MAIN_PLANE;
//ppfd->dwVisibleMask
//ppfd->dwDamageMask
if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ghDC = GetDC(Handle);
if (!bSetupPixelFormat(ghDC))
Close();
ghRC = wglCreateContext(ghDC);
if(ghRC == NULL)
ShowMessage("~ ghRC == NULL");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(Handle, ghDC);
}
//---------------------------------------------------------------------------
void TForm1:rawScene(void)
{
//TODO: Add your source code here
if(wglMakeCurrent(ghDC, ghRC)==false)
ShowMessage("Could not MakeCurrent 1");
glClearColor(1.0,1.0,1.0,0.0);
glClear( GL_COLOR_BUFFER_BIT);
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-0.5,0.5,0.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.5,0.5,0.0);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.5,-0.5,0.0);
glEnd;
if(wglMakeCurrent(ghDC, ghRC)==false)
ShowMessage("Could not MakeCurrent 2");
SwapBuffers(ghDC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
DrawScene();
}
//---------------------------------------------------------------------------
请问合适调用wglMakeCurrent
运行时DrawScene第一次调用没问题
第二次一般总是同不过(result==false)
那位大侠帮我看看
不胜感激
总是不对
不知怎么回事
源程序如下
BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;
ppfd = &pfd;
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->iPixelType = PFD_TYPE_RGBA;
ppfd->cColorBits = 8;
//ppfd->cRedBits = 8;
//ppft->cRedShift = 8;
//ppfd->cGreenBits = 8;
//ppft->cGreenShift = 8;
//ppfd->cBlueBits = 8;
//ppft->cBlueShift = 8;
//ppfd->cAlphaBits = 8;
//ppft->cAlphaShift = 8;
ppfd->cAccumBits = 0;
//ppfd->cAccumRedBits =0;
//ppfd->cAccumGreenBits =0;
//ppfd->cAccumBlueBits =0;
//ppfd->cAccumAlphaBits =0;
ppfd->cDepthBits = 16;
ppfd->cStencilBits = 0;
//ppfd->cAuxBuffers
//ppfd->iLayerType
//ppfd->bReserved
ppfd->dwLayerMask = PFD_MAIN_PLANE;
//ppfd->dwVisibleMask
//ppfd->dwDamageMask
if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ghDC = GetDC(Handle);
if (!bSetupPixelFormat(ghDC))
Close();
ghRC = wglCreateContext(ghDC);
if(ghRC == NULL)
ShowMessage("~ ghRC == NULL");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(Handle, ghDC);
}
//---------------------------------------------------------------------------
void TForm1:rawScene(void)
{
//TODO: Add your source code here
if(wglMakeCurrent(ghDC, ghRC)==false)
ShowMessage("Could not MakeCurrent 1");
glClearColor(1.0,1.0,1.0,0.0);
glClear( GL_COLOR_BUFFER_BIT);
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-0.5,0.5,0.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.5,0.5,0.0);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.5,-0.5,0.0);
glEnd;
if(wglMakeCurrent(ghDC, ghRC)==false)
ShowMessage("Could not MakeCurrent 2");
SwapBuffers(ghDC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
DrawScene();
}
//---------------------------------------------------------------------------
请问合适调用wglMakeCurrent
运行时DrawScene第一次调用没问题
第二次一般总是同不过(result==false)
那位大侠帮我看看
不胜感激