You are on page 1of 15

BasicOp

penGLinVisu
ualC++

COMPPUTERGGRAPHICSSUSING

in
Mr.Nattap
pholJasungnu
uen andPoollsakKoseeyap
porn,Ph.D
DepartmeentofTeacherrTraininginElectricalEn
ngineering
KingMo
ongkutsInstituteofTechn
nologyNorth
hBangkok

OpenGL (Oppen Graphicss Library)


2
3 OppenGL

OpenGGL



OpenGL

OpeenGL
(Portabillity)



OppenGL

OpeenGL

OpenGLL incluude headder gl.h


OpenGL
gl 1-1 glCClearColor()
GL__ GL_COOLOR_BUFFFER_BIT
suffix
gglColor3f 3
RGB glColor44f RGBA f



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

OpenGGL Microosoft Visual C++ Applicationss Wizard

Single Docum
ment OpennGL

RC
R (Renderinng Context)
(Deevice Contextt)
() OpenGL RC

BasicOpenGLinVisualC++

1 Single Document Applications Wizard


1.1 Graphic2D Microsoft Visual C++ MFC
Application OK 1-2

1-2 MFC Application


1.2 Application Type Single Document Document/View
architecture English (United States) 1-3

1-3 Application Type Single Document

BasicOpenGLinVisualC++

1.3 Tap User Interface Features Main frame styles Toolbars


1-4

1-4 Main frame styles


1.4 Tap Advanced Features ActiveX Controls Common
Control Manifest 1-5

1-5 Advanced Features


1.5 Tap Generated Classes Tap CChildView Generated Class:
CChildView CWnd Finish 1-6

BasicOpenGLinVisualC++

1-6 Generated Classes


Build > Build Graphic2D Run
Graphic2D Single Document 1-7

1-7 Graphic2D Single Document


2 OpenGL Visual C++
OpenGL Visual C++
opengl32.lib , glu32.lib

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++

2.2 opengl32.lib glu32.lib Project > Properties


Graphic2D Property Pages Linker -> Gerneral 1-8
2.3 Additional Library Directories C:\Program Files\Microsoft
Visual Studio .NET 2003\Vc7\PlatformSDK\Lib OK

1-8 opengl32.lib glu32.lib


2.4 Input Additional Dependencies Opengl32.lib
glu32.lib OK 1-9

1-9 Additional Dependencies

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;
.
}

3.2 bool SetWindowPixelFormat(HDC hDC) bool CreateViewGLContext(HDC hDC)


CChildView 1-10

1-10 CChildView
CChildView
bool SetWindowPixelFormat(HDC hDC) bool CreateViewGLContext(HDC hDC)

BasicOpenGLinVisualC++

bool CChildView::SetWindowPixelFormat(HDC hDC)


boolCChildView::SetWindowPixelFormat(HDChDC)
{

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++

bool CChildView::CreateViewGLContext(HDC hDC)

boolCChildView::CreateViewGLContext(HDChDC)

m_hGLContext=wglCreateContext(hDC);
if(m_hGLContext==NULL)

returnFALSE;
}

if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)

returnFALSE;

returnTRUE;

3.3 (Member Variables) m_hGLContext,m_GLPixelIndex


Constructer CChildView
CChildView::CChildView()
{

m_hGLContext=NULL;

m_GLPixelIndex=0;
}

3.4 Window Style (Client) OpenGL


PreCreateWindow
BOOLCChildView::PreCreateWindow(CREATESTRUCT&cs)
{

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++

3.5 OnCreate Window Message WM_CREATE Add OnCreate


CChildView 1-11

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

void CChildView::OnSize(UINTnType,int cx,intcy)


{

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++

3.7 OnnDestroy Window Message WM_DESSTROY

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

Buiild > Buildd Graphic2DD


Ctrl +F55 2 OpennGL
Microsoft Visual C+++ 1--15

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

You might also like