Click here to Skip to main content
15,114,643 members
Articles / Web Development / IIS
Posted 22 Nov 1999


57 bookmarked

An OpenGL ActiveX Control Developed with MFC

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
22 Nov 19993 min read

VB test of the 3D OCX <!-- Article Starts -->

The article focuses on how to develop a OpenGL ActiveX Control (OCX) with MFC, which can be used in VB application or HTML, and how to use the OpenGL ActiveX Control in VB and HTML to develop a 3D application and Internet Webpage. To simplify the demo code, I borrow the 3D Font class which has been posted at Codeguru.

Some key points in developing OpenGL ActiveX Control

Context Device

The ActiveX control will be show in the container client, so the Context Device in which the control is drawn is the container Context Device. The OpenGL control must have a Context Device pointer. Set it as the container Context Device pointer when the control is created. The container Context Device pointer can be found by calling the COleControl member function "GetDC". The code is :

int CGL3dOcxCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;
	// TODO: Add your specialized creation code here
	return 0;

void CGL3dOcxCtrl::SetupEnv(void)
	//get device context 
	m_pDC = GetDC();


On the OpenGL control, I designed the string shown in the 3DFont object as the real property. The container can process the string through the property implementation "GetContent" and "SetContent". The sample code is:

BSTR CGL3dOcxCtrl::GetContent() 
	// TODO: Add your property handler here
	CString strResult = m_Font.GetText();

	return strResult.AllocSysString();

void CGL3dOcxCtrl::SetContent(LPCTSTR lpszNewValue) 
	// TODO: Add your property handler here
	wglMakeCurrent(m_pDC->m_hDC, m_hRC);
	m_Font.CreateFont(m_pDC, "Arial Black");
	wglMakeCurrent(m_pDC->m_hDC, NULL);

In the SetContent function, after resetting the string in 3D Font class, call "Refresh" to repaint the control


The important method in this OpenGL control is "GLRender". When the container response the paint/draw event, the container can call this method to repaint the control.

In method of "GLRender", there are two important functionality, one is set rendering viewport based on the control size in container, the other is rendering.

Unlike normal MS-Windows application which can prceoss WM_SIZE message, the control size is maniplated by container, and the rendering viewport can be set easily in rendering action.

The rendering is similar as the rendering function in normal OpenGL program.

void CGL3dOcxCtrl::GLRender() 
	// TODO: Add your dispatch handler code here
	int cx, cy;
	GetControlSize(&cx, &cy);
	if(m_cx != cx || m_cy != cy)
	   m_cx = cx;
	   m_cy = cy;



void CGL3dOcxCtrl::SetViewPort(void)
	wglMakeCurrent(m_pDC->m_hDC, m_hRC);
	glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 7.0);
	glViewport(0, 0, m_cx, m_cy);
	wglMakeCurrent(m_pDC->m_hDC, NULL);

void CGL3dOcxCtrl::dcRender(void)
	wglMakeCurrent(m_pDC->m_hDC, m_hRC);
	//clear color buffer
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	//set light model
	glLightfv(GL_LIGHT0, GL_AMBIENT, m_Lightambient);//set light ambient
	glLightfv(GL_LIGHT0, GL_DIFFUSE, m_Lightdiffuse);//set light specular
	glLightfv(GL_LIGHT0, GL_SPECULAR, m_Lightspecular);//set light specular
	glLightfv(GL_LIGHT0, GL_POSITION, m_Lightposition);//set light position



	glTranslatef(-1.2f, -0.1f, -4.2f);


	wglMakeCurrent(m_pDC->m_hDC, NULL);

The other method is "SetStrText" which allows container to set the string of the 3D Font like the Property function "SetContent"


The most important event in this OpenGL control is the "GLDraw". By triggering "GLDraw" event, container can repaint the control.


The ActiveX Control Test Container in the VC++ package can be used to register this OCX, or if this source code is complied in your PC with VC++ IDE, it can be registered automatically

The VB demo for using the OpenGL Control

In the VB test demo, the OpenGL control is added into the form as an OLE component GL3DOCXLib.GL3dOcx GL3dOcx1

There are also two editboxes in the from, one is used to change the string in 3D Font by method "SetStrText", the other is used to change the string by property implementation "SetContent"

In the demo, the code processing the "GLDraw" event is

Private Sub GL3dOcx1_GLDraw()
End Sub

The code triggering "GLDraw" event are

Private Sub Form_Paint()
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyReturn Then
      GL3dOcx1.Content = Text1.Text
      Text2.Text = Text1.Text
   End If
End Sub

Private Sub Text1_LostFocus()
   GL3dOcx1.Content = Text1.Text
   Text2.Text = Text1.Text
End Sub

Private Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyReturn Then
      GL3dOcx1.Content = Text2.Text
      Text1.Text = Text2.Text
   End If
End Sub

Private Sub Text2_LostFocus()
   GL3dOcx1.Content = Text2.Text
   Text1.Text = Text2.Text
End Sub

The HTML demo

From the tag, the HTML can find and load the OpenGL control. The GUID of is the key point with which the HTML document can get the control. the tag set the control property.

For the homepage with the OpenGL control, VBscript must be used to handle the control. GL3dOcx_GLDraw() in the BVscript is the function used to process "GLDraw" event.

NOTE: I tested it in IE3.0/5.0, Netscape Navigator3.0/Communicator4.6. IE3.0/5.0 support it. Not tested in Netscape Navigator3.0/Communicator4.6.


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

No Biography provided

Comments and Discussions

Praiseexcellent! Pin
Southmountain29-Jan-21 5:43
MemberSouthmountain29-Jan-21 5:43 
QuestionUsing the control with C# Pin
dinesh.17krishnan17-Dec-14 20:21
Memberdinesh.17krishnan17-Dec-14 20:21 
Questionhi! Pin
nevstops4-Jan-13 17:47
Membernevstops4-Jan-13 17:47 
GeneralBetter resizing Pin
xawari13-Jul-07 0:40
Memberxawari13-Jul-07 0:40 
Generaln00bism..... it wouldn't compile for me.... =( Pin
Stephen Sweeney7-Jun-04 4:23
MemberStephen Sweeney7-Jun-04 4:23 
QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pin
werter13-May-04 22:10
Memberwerter13-May-04 22:10 
General&amp;#24590;&amp;#20040;&amp;#32852;&amp;#31995;&amp;#20320;&amp;#21602;??How to contact you???Zhaohui Xing Pin
look4u20-Aug-03 4:24
Memberlook4u20-Aug-03 4:24 
GeneralDoes not work Pin
Jonathan de Halleux25-Feb-03 1:29
MemberJonathan de Halleux25-Feb-03 1:29 
GeneralUps! Pin
Nikolai Teofilov6-Feb-03 10:43
MemberNikolai Teofilov6-Feb-03 10:43 
GeneralRe: Ups! Pin
Weidong Fang8-Feb-03 15:39
MemberWeidong Fang8-Feb-03 15:39 
QuestionHow to display Chinese? Pin
Anonymous5-Oct-02 20:25
MemberAnonymous5-Oct-02 20:25 
QuestionWhat about OnDrawMetafile Pin
2-Jul-02 4:28
suss2-Jul-02 4:28 
GeneralAre you sure???... Pin
4-Jun-02 12:58
suss4-Jun-02 12:58 
Generalocx crashes in ActiveX Control testcontainer Pin
xyto23-Aug-00 5:58
Memberxyto23-Aug-00 5:58 
GeneralRe: ocx crashes in ActiveX Control testcontainer Pin
18-Dec-00 13:21
suss18-Dec-00 13:21 
GeneralRe: ocx crashes in ActiveX Control testcontainer Pin
Masoud Samimi1-Jan-01 11:45
MemberMasoud Samimi1-Jan-01 11:45 
GeneralRe: ocx crashes in ActiveX Control testcontainer Pin
Jacksonh26-Jan-02 13:37
MemberJacksonh26-Jan-02 13:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.