发信人: rhine (有雨无风), 信区: BorlandDev
标 题: OpenGL在C++Builder中的应用(2)
发信站: 哈工大紫丁香 (Sun Aug 27 06:38:01 2000), 转信
发信人: jinchao (Sonic), 信区: BCB
发信站: BBS 水木清华站 (Fri Jul 30 08:58:52 1999) WWW-POST
(二)第一个OpenGL程序
记得以前学习编程时,每学一门新语言,我总先按书上编一个最简单的例子上机通过
,然后再开始学习它的语法,这样有利于感性的培养。这里我也先给出一个最简单OpenGL
程序,它只是在窗口上画出一个矩形,如果你能调试出正确结果,那恭喜你迈出第一步。
(1). 选择File->New Application,新建一个程序,把它保存到一目录下
(2). 打开主窗体头文件Unit1.h,修改如下:
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <gl\gl.h> /*记得加上这两行*/
#include <gl\glu.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
HGLRC hRC; /* 新加的两个成员 */
void DrawWithOpenGL();
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
(3). 打开主窗体文件Unit1.cpp修改如下
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::DrawWithOpenGL()
{
HDC hDC;
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);glVertex3f(1.0f,1.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);glVertex3f(3.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);glVertex3f(3.0f,3.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,3.0f,0.0f);
glEnd();
glFlush();
hDC = wglGetCurrentDC();
SwapBuffers(hDC);
}
//------------- 对应窗体的OnResize事件,下面几个函数类同-------
void __fastcall TForm1::FormResize(TObject *Sender)
{
HDC hDC;
hDC=GetDC(this->Handle);
wglMakeCurrent(hDC,hRC);
glViewport(0,0,this->ClientWidth,this->ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f,4.0f,0.0f,4.0f,0.0f,4.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
wglMakeCurrent(NULL,NULL);
ReleaseDC(this->Handle,hDC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
wglDeleteContext(hRC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,0,0,0,0,0,0,
32,
0,0,
PFD_MAIN_PLANE,
0,
0,0,0
};
HDC hDC=GetDC(this->Handle);
int pixelFormat;
if((pixelFormat=ChoosePixelFormat(hDC,&pfd))==0)
{
MessageBox(NULL,"ChoosePixelFormat Error!","Error",MB_OK);
return;
}
if(SetPixelFormat(hDC,pixelFormat,&pfd)==false)
{
MessageBox(NULL,"SetPixelFormat Error!","Error",MB_OK);
return ;
}
hRC=wglCreateContext(hDC);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
ReleaseDC(this->Handle,hDC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
HDC hDC=GetDC(this->Handle);
wglMakeCurrent(hDC,hRC);
DrawWithOpenGL();
wglMakeCurrent(hDC,NULL);
ReleaseDC(this->Handle,hDC);
}
//---------------------------------------------------------------------------
(4). 现在可以编译通过,注意系统起码要在16bits真彩模式下,看到矩形内部的色
彩过度了吗,这是OpenGL良好的插值运算的结果,是不是有些心动了呢?
--
念天地之悠悠,
独沧然而泪下,
知我者谓我心忧,
不知我者谓我何求......
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: rhine.bbs@smth.org]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:9.307毫秒