|
간단한 예제를 올립니다.
도움이 되었으면 좋겠군요.
#include <vcl.h>
#include <math.h>
#include "gl.h"
#include "glu.h"
#include "glaux.h"
#pragma hdrstop
#include "Unit1.h"
#define PI 3.14159265358979323846264338327950288419
#define Radian(Deg) (PI/180*(Deg))
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
GLInit();
}
//---------------------------------------------------------------------------
void TForm1::GLInit(void)
{
int pf;
hDC = GetDC(Panel1->Handle);
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cDepthBits = 16;
pfd.cColorBits = 32; //true color
pf = ChoosePixelFormat(hDC, &pfd);
if (pf == 0) { MessageBox(NULL, "ChoosePixelFormat() failed: Cannot find a suitable pixel format.", "Error", MB_OK); return; }
if (SetPixelFormat(hDC, pf, &pfd) == FALSE) { MessageBox(NULL, "SetPixelFormat() failed: Cannot set format specified.", "Error", MB_OK); return; }
DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
ReleaseDC(hDC, Handle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
static int nWidth, nHeight;
static int nAspect;
nWidth = Panel1->Width;
nHeight = Panel1->Height;
glViewport(0.0, 0.0, nWidth, nHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(nHeight == 0) nHeight = 1;
nAspect = nWidth / nHeight;
gluPerspective(45.0f, nAspect, 1.0f, 200.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
DrawScene();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
}
//---------------------------------------------------------------------------
void TForm1::DrawScene(void)
{
//우선 지우고
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
//그림을 그린다.
glColor3f(1.0f,0.0f,0.0f);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -100.0f);
auxSolidSphere(100.0f);
glFlush();
SwapBuffers(wglGetCurrentDC());
}
//---------------------------------------------------------------------------
곽병주 님이 쓰신 글 :
: 지금 패널위에 오픈지엘 RC를 만들어 거기에다 3차원 화면을 그리고 있습니다.
: 그런데 문제는 이것이
: 리사이즈할때 그냥 패널이 보여지고 있습니다.
: 다른데서 찾아보고 해서
: WM_ERASEBKGND메시지를 처리하여
: 아무것도 안하게 했고
: 또한 패어런트 폼과 패널의 더블 버퍼링을 TRUE로 해놓았지만
: 리사이즈할때 마우스를 드래그 하고(마우스 다운은 하지않고)
: 있으면 랜더링 컨텍스트가 사라지고 패널만 보입니다.
: 물론 폼 리사이징 할때 거기서 렌더링 하고 SWAPBUFFER를 해놓았는데도
: 나중에 다시 폼이 패널을 그리는것 같습니다.
:
: 다른 코드를 찾아보니 Application->idle= IdleLoop를 하는것을 보았는데
: 이코드를 둘 경우 시피유 부하가 올라가더군요.
: 물론 이때도 리사이징 할때 렌더링 화면은 보이지 않습니다.
: 참고로 계속 리사이징을 하면 깜박거리는 모습을 볼수 있습니다.
:
: 한수 지도 바랍니다.
: 그럼 미리 감사드리며 꾸벅
|