Click here to Skip to main content
15,886,519 members
Articles / Desktop Programming / MFC

Basic Curves And Surfaces Modeler

Rate me:
Please Sign up or sign in to vote.
4.17/5 (40 votes)
18 Apr 2012CPOL3 min read 246.6K   16.4K   117   53
A basic demo of modeling curves and surfaces in OpenGL.

Sample Image - CadSurf.jpg

Introduction

This is a basic surface modeler made using MFC and OpenGL on VC6. The geometry interface and graphics interface are separated so that you can simply define your curve or surface without worrying about the display. The display is in a generalized form i.e. if you derive your own curve from CCurve and override the PointAtPara and NormalAt methods along with other mandatory methods, you can create an OpenGL curve as follows:

Some where in your project file you create your own derived classes' headers and source files...

//header file
#include "Curve.h"
class myCurve : public CCurve
{
....
};

//cpp file
#include "myCurve.h"
CPoint3D myCurve::PointAtPara(double upar)
{
  double x, y, z;
  x = 2*sin(uPar)......
  y = .......
  z = .....
  CPoint3D P(x,y,z);
  return P;
}

Some where in your CDocument code...

#include "myCurve.h"
void CMyProjDoc::OnCurve()
{
    myCurve crv(...);
    CGLCurve* myglCurve = new CGLCurve(&crv)
    dContext->Display(myglCurve);
    delete myglCurve;
}

Now dContext is the display context object (CGLDisplayContext) that manages all the display functions, so this is created in the constructor of the document. Similarly for surfaces also. There are currently almost all the general curves and surfaces implemented. Curves are: line, circle, ellipse, parabola, hyperbola, Bezier and bspline. Surfaces are: plane, cylinder, cone, sphere, torus, extruded, revolved, ruled and pipe.

The display of all curves are managed by the CGLCurve class and that of surfaces are by the CGLSurface class. Points and other geometric helpers like axes, coordinate systems etc. are also modeled. This is implemented in non interactive mode for demo. You can modify the source to make an interactive application.

There are some modifications, dated 23/2/2003, made in the CGLView class which is the basic class for OpenGL viewing. Earlier it was derived from CView and the CCadSurfView class was derived from CGLView. The OpenGL manipulation methods were called from the CCadSurfView class's methods. e.g. mouse implementations etc. Like this: CGLView::RotateView(). Some how, I felt that there is no isolation for the OpenGL view. Hence with a better Object Oriented approach, I made the CGLView class, an independent class and the constructor of the class is passed with pointer to CWnd. The private data member in CGLView is the pointer to the CWnd which is initialized with OpenGL settings in the c'tor. Now an object of this CGLView is private member for CCadSurfView class which is dynamically initialized in the OnCreate() method of CCadSurfView and is deleted in OnDestroy() method. Now the methods are called using the object of the CGLView class instead of calling it directly as base class methods.--Like this:

CCadSurfView::OnCreate(...)
{
  myView = new CGLView(this, GetDocument()->DisplayContext());
}

void CCadSurfView::OnMouseMove(...)
{
  .....
  myView->RotateView(....);
}

void CCadSurfView::OnSize(...)
{
  ...
  myView->Resize(cx, cy);
  CCadSurfView::OnSize.....
  ...
}

...CCadSurfView::OnDestroy(...)
{
  ...
  delete myView;
  ...
}

Whereas when it was derived earlier from CView, the calls were like this:

void CCadSurfView::OnMouseMove(...)
{
  ...
  CGLView::RotateView(...);
  ...
}

This application has been developed on a PIII 800Mhz m/c with 256MB RAM, RIVA TNT 32MB VRAM AGP and W2K. Not tested on other platforms. Graphics is heavy and requires good graphics card. This is not just a tutorial but an ongoing project development! Contributions are also welcome. Best Of Luck and please do let me know about comments and suggestions. Thank You.

History

27 March 2007: I have split project into two parts

  1. Cadsurf Application,
  2. VKernel (VKGeom.dll and VKGraphic.dll DLLs). There are now two separate DLLs for geometry and graphics.

18th April 2012: Added solution for VS2008.

Added an additional demo of user defined surface showing Klein Bottle variant. The UserSurface.h and .cpp file in the CadSurf project shows the real implementation of Object Oriented Programming. And much more!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Comments and Discussions

 
QuestionUnable to contact you - please supply email address Pin
dh10010-Apr-16 7:46
dh10010-Apr-16 7:46 
AnswerRe: Unable to contact you - please supply email address Pin
Sharjith20-Sep-17 2:42
professionalSharjith20-Sep-17 2:42 
Question[My vote of 2] Weak as an article... Pin
Dave Kreskowiak24-Jul-13 2:01
mveDave Kreskowiak24-Jul-13 2:01 
AnswerRe: [My vote of 2] Weak as an article... Pin
Sharjith24-Jul-13 18:23
professionalSharjith24-Jul-13 18:23 
QuestionFixing the MFC and AERO issue? Pin
ninpo12-Jun-13 6:12
ninpo12-Jun-13 6:12 
AnswerRe: Fixing the MFC and AERO issue? Pin
Sharjith12-Jun-13 18:16
professionalSharjith12-Jun-13 18:16 
GeneralNice post Pin
Shahriar Iqbal Chowdhury/Galib19-Oct-12 11:09
professionalShahriar Iqbal Chowdhury/Galib19-Oct-12 11:09 
QuestionQuestion on 3D fit Pin
ninpo14-Jun-12 7:10
ninpo14-Jun-12 7:10 
AnswerRe: Question on 3D fit Pin
Sharjith14-Jun-12 10:50
professionalSharjith14-Jun-12 10:50 
GeneralRe: Question on 3D fit Pin
ninpo14-Jun-12 13:43
ninpo14-Jun-12 13:43 
GeneralRe: Question on 3D fit Pin
Sharjith14-Jun-12 15:05
professionalSharjith14-Jun-12 15:05 
GeneralRe: Question on 3D fit Pin
ninpo14-Jun-12 18:14
ninpo14-Jun-12 18:14 
Since I used your wisdom on OpenGL and my trusty red book at my side, and an inquisitive nature and a LOT of Trial and error . I would be happy to post. For those less inclined, the area of the fit that does the grunt work of what Sharjith post is the code CGLView::FitView() ...

There he gets the Boundary box of all elements of the screen returned ... HOWEVER, it only tests for the front left bottom corner and the back right top corner... Looking at the simple case, rectangle the bottom left, top right corner are tested. if we rotate the rectangle 45 degrees CW the top-left (2) corner and bottom Right (1) corner CAN be off of the screen so in essence clips the other two othgonal corners.

2-------------- 3
| |
| |
0 ------------- 1

To resolve we need to test those other corners also, so the case where:

C++
CViewBoundingBox B = m_pSelectElement->GetBoundingBox();
lx = B.XMax(); ly = B.YMax(); lz = B.ZMax();
sx = B.XMin(); sy = B.YMin(); sz = B.ZMin();

//need to check the other corners as well it could be off the screen..
lx2 = B.XMax(); ly2 = B.YMin(); lz2 = B.ZMax();
sx2 = B.XMin(); sy2 = B.YMax(); sz2 = B.ZMin();

..
..
..

gluProject(lx, ly, lz, mvmatrix, projmatrix, viewport,
				&mx, &my, &mz);

gluProject(sx, sy, sz, mvmatrix, projmatrix, viewport,
				&cx, &cy, &cz);

//need to check the oppisite back corners as well for the bounding to find out if we are off the screen...  Add these corners as well
gluProject(lx2, ly2, lz2, mvmatrix, projmatrix, viewport,
				&mx2, &my2, &mz2);

gluProject(sx2, sy2, sz2, mvmatrix, projmatrix, viewport,
				&cx2, &cy2, &cz2);

...
...
...
...

CRect rcRect;
m_pViewWnd->GetClientRect(&rcRect);

volRectTwo.SetRect(cx2,cy2,mx2,my2);
volRectTwo.NormalizeRect();

volRect.SetRect(cx,cy,mx,my);
volRect.NormalizeRect();

//check if either is out of the view client rect
//NOTE: adjust as needed in short IF your volRect or volRect2 is larger then rcRect you will have problems with the fit Also if you resize and the view rectangle w<h then you need to adjust for that as well
//check if either is out of the view client rect
if( rcRect.Width()>rcRect.Height() )
{
    if( volRectTwo.Height()>rcRect.Height() && volRectTwo.Height()>volRect.Height() )
		volRect=volRectTwo;//REPLACE IT
}
else
{
     if( volRectTwo.Width()>rcRect.Width() && volRectTwo.Width()>volRect.Width() )
		volRect=volRectTwo;//REPLACE IT
}

Fit3d(volRect);


Sharijith is correct if you use a sphere it would be correct, the correct way is to get the center of the elements project to the near and far clip plane, create a normal vector and use the radius and the center point of the sphere to create your points for checking..create 3d points project those and check those to the client rect.

modified 15-Jun-12 10:59am.

GeneralRe: Question on 3D fit Pin
Sharjith15-Jun-12 1:30
professionalSharjith15-Jun-12 1:30 
GeneralRe: Question on 3D fit Pin
Member 941335229-Sep-17 6:04
Member 941335229-Sep-17 6:04 
GeneralMy vote of 5 Pin
Volynsky Alex23-May-12 3:44
professionalVolynsky Alex23-May-12 3:44 
GeneralMy vote of 3 Pin
Unque23-Apr-12 20:52
Unque23-Apr-12 20:52 
QuestionWho can tell me? Pin
YangTze16-Apr-12 15:39
YangTze16-Apr-12 15:39 
AnswerRe: Who can tell me? Pin
Sharjith16-Apr-12 16:01
professionalSharjith16-Apr-12 16:01 
GeneralRe: Who can tell me? Pin
YangTze17-Apr-12 0:43
YangTze17-Apr-12 0:43 
GeneralRe: Who can tell me? Pin
Sharjith18-Apr-12 9:48
professionalSharjith18-Apr-12 9:48 
GeneralRe: Who can tell me? Pin
YangTze22-Apr-12 15:33
YangTze22-Apr-12 15:33 
Questioncan you tell me the usage of class in demo project?pdf file will be OK! Pin
cpluspluswiser7-Feb-12 19:50
cpluspluswiser7-Feb-12 19:50 
AnswerRe: can you tell me the usage of class in demo project?pdf file will be OK! Pin
Sharjith8-Feb-12 1:08
professionalSharjith8-Feb-12 1:08 
GeneralRe: can you tell me the usage of class in demo project?pdf file will be OK! Pin
Member 915959-Feb-12 1:54
Member 915959-Feb-12 1:54 
GeneralRe: can you tell me the usage of class in demo project?pdf file will be OK! Pin
Sharjith9-Feb-12 16:35
professionalSharjith9-Feb-12 16:35 

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.