Professional Documents
Culture Documents
penGLinVisu
ualC++
COMPPUTERGGRAPHICSSUSING
in
Mr.Nattap
pholJasungnu
uen andPoollsakKoseeyap
porn,Ph.D
DepartmeentofTeacherrTraininginElectricalEn
ngineering
KingMo
ongkutsInstituteofTechn
nologyNorth
hBangkok
OpenGGL
OpenGL
OpeenGL
(Portabillity)
OppenGL
OpeenGL
8 1
suffix
ANSSI C OppenGL Type Definition
D
1-1 OpenGGL
BasicOp
penGLinVisu
ualC++
1 Suffixx
glVertex2i(1,
g
, 3) glVVertex2f(1.0, 3.0)
OpenGLL suffix v
OpeenGL
Statte Machine
State
Variablee
OpenGLL Utility Libbrary
GLU OpenGGL
Mipmappping Texture Mapping,
M
Orthogonnal Peerspective,
Polygon Tessellation,
,
GLU
gluu
include header glu.h
1
Single Docum
ment OpennGL
RC
R (Renderinng Context)
(Deevice Contextt)
() OpenGL RC
BasicOpenGLinVisualC++
BasicOpenGLinVisualC++
BasicOpenGLinVisualC++
2.1 stdafx.h
#define_AFX_ALL_WARNINGS
#include<afxwin.h>//MFCcoreandstandardcomponents
#include<afxext.h>//MFCextensions
#include<gl\gl.h>
#include<gl\glu.h>
#include<afxdtctl.h>
//MFCsupportforInternetExplorer4Common
#ifndef_AFX_NO_AFXCMN_SUPPORT
#include<afxcmn.h>
//MFCsupportforWindowsCommonControls
#endif//_AFX_NO_AFXCMN_SUPPORT
BasicOpenGLinVisualC++
BasicOpenGLinVisualC++
3
OpenGL RC (Rendering Context)
(Device Context) ()
OpenGL RC
SetWindowPixelFormat CreateViewGLContext
3.1 Member Variable protected CChildView ChildView.h
#pragma once
//CChildView.h
classCChildView:publicCWnd
{
.
protected:
HGLRCm_hGLContext;
intm_GLPixelIndex;
.
}
1-10 CChildView
CChildView
bool SetWindowPixelFormat(HDC hDC) bool CreateViewGLContext(HDC hDC)
BasicOpenGLinVisualC++
PIXELFORMATDESCRIPTORpixelDesc;// PIXELFORMATDESCRIPTOR
pixelDesc.nSize=sizeof(PIXELFORMATDESCRIPTOR);// sizeof
pixelDesc.nVersion=1;// Version
pixelDesc.dwFlags=PFD_DRAW_TO_WINDOW|//
PFD_DRAW_TO_BITMAP| //
PFD_SUPPORT_OPENGL| // OpenGL
PFD_SUPPORT_GDI|
//Support GDI
PFD_STEREO_DONTCARE; //
pixelDesc.iPixelType=PFD_TYPE_RGBA;
// RGBA , ,,
pixelDesc.cColorBits=32; // RGB
pixelDesc.cRedBits=8;
pixelDesc.cRedShift=16;
pixelDesc.cGreenBits=8;
pixelDesc.cGreenShift=8;
pixelDesc.cBlueBits=8;
pixelDesc.cBlueShift=0;
pixelDesc.cAlphaBits=0;
pixelDesc.cAlphaShift=0;
pixelDesc.cAccumBits=64;
pixelDesc.cAccumRedBits=16;
pixelDesc.cAccumGreenBits=16;
pixelDesc.cAccumBlueBits=16;
pixelDesc.cAccumAlphaBits=0;
pixelDesc.cDepthBits=32;
pixelDesc.cStencilBits=8;
pixelDesc.cAuxBuffers=0;
pixelDesc.iLayerType=PFD_MAIN_PLANE;
pixelDesc.bReserved=0;
pixelDesc.dwLayerMask=0;
pixelDesc.dwVisibleMask=0;
pixelDesc.dwDamageMask=0;
// pixelDesc
m_GLPixelIndex=ChoosePixelFormat(hDC,&pixelDesc);
if(m_GLPixelIndex==0)//Let'schooseadefaultindex.
{
m_GLPixelIndex=1;
if(DescribePixelFormat(hDC,m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
returnFALSE;
}
}
if(SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc)==FALSE)
{
returnFALSE;
}
returnTRUE;
BasicOpenGLinVisualC++
boolCChildView::CreateViewGLContext(HDChDC)
m_hGLContext=wglCreateContext(hDC);
if(m_hGLContext==NULL)
returnFALSE;
}
if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
returnFALSE;
returnTRUE;
m_hGLContext=NULL;
m_GLPixelIndex=0;
}
if(!CWnd::PreCreateWindow(cs))
returnFALSE;
cs.dwExStyle|=WS_EX_CLIENTEDGE;
cs.style&=~WS_BORDER;
//
cs.style|=(WS_CLIPCHILDREN|WS_CLIPSIBLINGS);
cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL,IDC_ARROW),
reinterpret_cast<HBRUSH>(COLOR_WINDOW+1),NULL);
returnTRUE;
}
BasicOpenGLinVisualC++
1-11 OnCreate
int CChildView::OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CWnd::OnCreate(lpCreateStruct)==1)
return1;
HWNDhWnd=GetSafeHwnd();
HDChDC=::GetDC(hWnd);
if(SetWindowPixelFormat(hDC)==false)
return0;
if(CreateViewGLContext(hDC)==FALSE)
return0;
return0;
}
OpenGL
SetWindowPixelFormat CreateViewGLContext hDC
(Device Context)
3.6 OnSize Window Message WM_SIZE
1-12
1-12 OnSize
CWnd::OnSize(nType,cx,cy);
GLsizeiwidth,height;
GLdoubleaspect;
width=cx;
height=cy;
if(cy==0)
aspect=(GLdouble)width;
else
aspect=(GLdouble)width/(GLdouble)height;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0*aspect,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
10
BasicOp
penGLinVisu
ualC++
1-14
void CCh
hildView::OnDestroy()
{
CWnd::OnDestroy();
if(wglGetCurrentContext())!=NULL)
wgllMakeCurrent((NULL,NULL);
if(m_hGLCon
ntext!=NULL)
wgllDeleteContextt(m_hGLContexxt);
m_h
hGLContext=N
NULL;
}
}
1-14 OnDDestroy
3.8
OnPaint
Client
Single
S Docum
ment 1-15
vo
oidCChildVieew::OnPaint())
{
CPainttDCdc(this);//devicecontextforpain
nting
dIdentity();
glLoad
glCleaar(GL_COLOR
R_BUFFER_BIT
T);
glBegiin(GL_TRIAN
NGLE_STRIP);
glColo
or4f(1.0f,0.0ff,0.0f,1.0f);
glVerttex2f(100.0f,50.0f);
glColo
or4f(0.0f,1.0ff,0.0f,1.0f);
glVerttex2f(450.0f,400.0f);
glColo
or4f(0.0f,0.0ff,1.0f,1.0f);
glVerttex2f(450.0f,50.0f);
glEnd();
glFlussh();
1--15
OpenGL
11
1
BasicOp
penGLinVisu
ualC++
OppenGL
2 1-16 , 1-17
voidCC
ChildView::On
nPaint()
{
CPaintDCdcc(this);//deevicecontextforpainting
glLoadIdenttity();
glClear(GL_COLOR_BUFF
FER_BIT);
//
GLfloatx=((float)(rand())%255)/255;
GLfloaty=((float)(rand()%255)/255
5;
GLfloatz=((float)(rand())%255)/255;
glColor3f(x,,y,z);
//
glRectd(100
0,100,400,400);
glFlush();
voidCCh
hildView::OnPaaint()
{
CPaintDCdc((this);//deviccecontextforp
painting
glLoadIdentitty();
glClear(GL_COLOR_BUFFER
R_BIT);
glBegin(GL_Q
QUADS);//
glColor4f(1.0
0f,0.0f,0.0f,1.0
0f);
glVertex2f(10
00.0f,500.0f);//
glColor4f(0.0
0f,1.0f,0.0f,1.0
0f);
glVertex2f(10
00.0f,100.0f); //
glColor4f(0.0
0f,0.0f,1.0f,1.0
0f);
glVertex2f(55
50.0f,100.0f); //
glColor4f(0.0
0f,1.0f,1.0f,1.0
0f);
glVertex2f(55
50.0f,500.0f); //
glEnd();
glFlush();
}
1-16
1-17
1
12
2
BasicOpenGLinVisualC++
2 3
3
2 3
1 OnSize
OnPaint 3
OnSize
voidCChildView::OnSize(UINTnType,intcx,int cy)
{
CWnd::OnSize(nType,cx,cy);
GLsizeiwidth,height;
GLdoubleaspect;
width=cx;
height=cy;
if(cy==0)
aspect=(GLdouble)width;
else
aspect=(GLdouble)width/(GLdouble)height;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,aspect,1,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_BACK);
}
OnPaint
voidCChildView::OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
//TODO:Addyourmessagehandlercodehere
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f,0.0f,6.0f);
//Translatetocenterscreen.
glScalef(1.2f,1.2f,1.2f);
//Increaseallsidesto20%.
glRotatef(50,1.0f,0.0f,0.0f);
//Rotatetriangleonxaxis.
glRotatef(10,0.0f,1.0f,0.0f);
//Rotatetriangleonyaxis.
glRotatef(50,0.0f,0.0f,1.0f);
//Rotatetriangleonzaxis.
..
13
BasicOp
penGLinVisu
ualC++
..
glBegin(GL__TRIANGLES);
glColor3f(0..0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0
0f);
glColor3f(1..0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0f);
glColor3f(0..0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
//
//
//
glColor3f(0..0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0
0f);
glColor3f(1..0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glColor3f(0..0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f));
//
//
glColor3f(0..0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0
0f);
glColor3f(1..0f,0.0f,0.0f);
glVertex3f(1
1.0f,1.0f,1.0f));
glColor3f(1..0f,1.0f,1.0f);
glVertex3f(1
1.0f,1.0f,1.0f);
//
//
//
glColor3f(0..0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0
0f);
glColor3f(1..0f,0.0f,0.0f);
glVertex3f(1
1.0f,1.0f,1.0f));
glColor3f(1..0f,1.0f,1.0f);
glVertex3f(1
1.0f,1.0f,1.0f);
glEnd();
//
//
//
//
glPopMatrixx();
glFlush();
//DonotcaallCView::OnP
Paint()forpain
ntingmessagess
OnSize OnPaint
Build
B > Buildd Graphic3DD
3
2-1
14
4
BasicOp
penGLinVisu
ualC++
2--1 3
3
voidCC
ChildView::OnPaint()
{
vicecontextforrpainting
CPaintDCdc(this);//dev
or(0.0f,0.0f,0.0ff,0.0f);
glClearColo
glClear(GL__COLOR_BUFFE
ER_BIT|GL_DE
EPTH_BUFFER
R_BIT);
glLoadIdentity();
glPushMatrrix();
//Createnewobjectanddefinetherotaationandtranslation.
GLUquadriccObj*SphereObj;
SphereObj=
=gluNewQuadric();
glPushMatrrix();
glTranslated(0.0,0.0,4.5);
glRotatef(30,1.0,0.0,0.0);
glRotatef(30,0.0,1.0,0.0);
glRotatef(30,0.0,0.0,1.0);
glColor3f(0.85f,1.0f,0.9f));
//Createw
wireframe.
gluQuadricD
DrawStyle(Sph
hereObj,GLU_L
LINE);
gluSphere(SSphereObj,1.3
3,9,13);
//CreateCoordinate
glBegin(GL__LINES);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0,1.5,0);
glVertex3f(0.0,2.0,0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(2.0,0.0,0);
glVertex3f(2.0,0.0,0);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0,0.0,2.0);
glVertex3f(0.0,0.0,2.0);
glEnd();
glPopMatrixx();
glFlush();
//DonotcaallCView::OnP
Paint()forpain
ntingmessagess
}
15
5