Click here to Skip to main content
Click here to Skip to main content

Basic Curves And Surfaces Modeler

By , 18 Apr 2012
 

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)

About the Author

Sharjith
Engineer Tata Technologies Inc
United States United States
Member
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNice postmemberShahriar Iqbal Chowdhury19 Oct '12 - 11:09 
thanks for sharing
QuestionQuestion on 3D fitmemberninpo14 Jun '12 - 7:10 
I noticed that the fit command doesn't seem to work if you say use the cylindrical surfaces and rotate the surfaces on screen via the mouse and then you hit the fit button... The surface or the view scene is still off of the screen. How do you fix that issue?
 
Enjoyed this, love playing with the concepts which is also a great post...
AnswerRe: Question on 3D fitmemberSharjith14 Jun '12 - 10:50 
I tried to use the bounding box and its projections for the fit algo. However the implementation is not fully correct. The best possible solution would be to use bounding spheres instead of bounding boxes which will give less accurate fits but more consistent throughout all rotations.
RegardsSmile | :)
N. Sharjith

GeneralRe: Question on 3D fitmemberninpo14 Jun '12 - 13:43 
Oooo got it... I was just going to say that I just projected the opposite corners and checked those as well and then picked the worst case... and it seemed to fix that... Thanks! Great learning tool..!
GeneralRe: Question on 3D fitmemberSharjith14 Jun '12 - 15:05 
Great, hope it is helpful to you! It would be great if you can share the patch that fixed it.
RegardsSmile | :)
N. Sharjith

GeneralRe: Question on 3D fit [modified]memberninpo14 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:
 
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:59.

GeneralRe: Question on 3D fitmemberSharjith15 Jun '12 - 1:30 
Thank you very much Ninpo! Using the Bounding box gives the most accurate fit in any projection view, exactly fitting the object into the screen. The sphere method is quick and dirty and will give inaccurate fits in many cases, though it makes sure that the object doesn't go out of the screen limits. Imagine a bounding sphere of a cylinder with 100 length and 10 diameter. The bounding sphere will be of 100 diameter and will fit the cylinder completely into the screen. However in a projection view showing the 10 diameter circular end of the cylinder, the fit will not be realistic. So the box method is the best. Thank you again for sharing the code, I will update the fix soon.
RegardsSmile | :)
N. Sharjith

GeneralMy vote of 5memberVolynsky Alex23 May '12 - 3:44 
Excellent job Sharjith!!!
Thanks!!
GeneralMy vote of 3memberUnque23 Apr '12 - 20:52 
Nice article.
QuestionWho can tell me?memberYangTze16 Apr '12 - 15:39 
1)
void CCadSurfDoc::OnCircles()
{
...
CGLFont *myFont = new CGLFont((LPCTSTR)str, P);
dContext->Display(myFont);
...
delete myFont;
...
} // !!!!!CRASH!!!!!
 
2)
void CCadSurfDoc::OnCircles()
{
...
CGLFont myFont((LPCTSTR)str, P);
dContext->Display(&myFont);
...
} // !!!! OK !!!!
 
3)
void CCadSurfDoc::OnCircles()
{
...
CGLFont XmyFont((LPCTSTR)str, P);
CGLFont myFont = (CGLFont*)XmyFont.Copy();
dContext->Display(myFont);
...
delete myFont;
...
} // !!!! OK !!!!
 
Why?
I LOVE CODING!

AnswerRe: Who can tell me?memberSharjith16 Apr '12 - 16:01 
Not an answer to this though... but looks like you ported the project to one of the latest Visual Studios on which this strange behaviour happens. It worked fine on VS6. I think it is time to investigate, fix and upload the latest code built in latest VS. Linux version written in Qt3 doesn't seem to have this problem either.
RegardsSmile | :)
N. Sharjith

GeneralRe: Who can tell me? [modified]memberYangTze17 Apr '12 - 0:43 
Thanks! Smile | :)
I use MS VS.Net 2003.
I LOVE CODING!


modified 17 Apr '12 - 7:37.

GeneralRe: Who can tell me?memberSharjith18 Apr '12 - 9:48 
In the file GLFont.h export the entire class to the dll instead of the individual public methods. To do so you need to take off the VKGRAPHIC_API that comes before all the public methods and add it before the class name. That is...
 
Change this...
 
class CGLFont : public CGLObject  
{
public:
	VKGRAPHIC_API CGLFont(const string&, const CPoint3D&, const char *tf = "MS Sans Serif", int ht = 16,
	  int wt = 0, DWORD it = 0, const TextAlign& = LEFT);
	VKGRAPHIC_API virtual ~CGLFont();
	VKGRAPHIC_API void DrawString();
	VKGRAPHIC_API virtual CGLObject* Copy() const;
	VKGRAPHIC_API virtual void DefineDisplay();
	VKGRAPHIC_API virtual void Display(const GLDisplayMode& = GLWIREFRAME);
	VKGRAPHIC_API virtual void Hilight(const GLDisplayMode&);
private:
...
}
 

to this...
 
class VKGRAPHIC_API CGLFont : public CGLObject
{
public:
    CGLFont(const string&, const CPoint3D&, const char *tf = "MS Sans Serif", int ht = 16,
      int wt = 0, DWORD it = 0, const TextAlign& = LEFT);
    virtual ~CGLFont();
    void DrawString();
    virtual CGLObject* Copy() const;
    virtual void DefineDisplay();
    virtual void Display(const GLDisplayMode& = GLWIREFRAME);
    virtual void Hilight(const GLDisplayMode&);
private:
...
}
 
And it should work fine with delete called legitimately on all myFont objects.
RegardsSmile | :)
N. Sharjith

GeneralRe: Who can tell me?memberYangTze22 Apr '12 - 15:33 
Thanks!
Now,it worked fine on VS2003.
I LOVE CODING!

Questioncan you tell me the usage of class in demo project?pdf file will be OK!membercpluspluswiser7 Feb '12 - 19:50 
thanks.
Now ,i am trying to use VKGeom,VKGraphic and CGLView to draw 3d curve without opengl basement,but i can't make it.my question is about:
1.Z Axis direction adjustment.
2.Z Axis deepth.
3.The Origin adjustment.
 
if anybody can do me a favour,contact me 396806883@qq.com,lookfar@163.com.thanks a lot.
AnswerRe: can you tell me the usage of class in demo project?pdf file will be OK!memberSharjith8 Feb '12 - 1:08 
Hello,
 
Can you please specify which curve are you trying to draw and what class are you trying to use? If you are trying to make a Bezier or B-Spline curve just create the control points and pass the list of control points to the constructor. I can tell you more if you can tell me what exactly is your question rather that just telling me what is it about.
RegardsSmile | :)
N. Sharjith

GeneralRe: can you tell me the usage of class in demo project?pdf file will be OK!memberMember 915959 Feb '12 - 1:54 
thank for reply.
my curve is very simple,it's based on some lines,and not special kind.
I now meet a problem about axis rotating.I haved used the following variables:
'xRot,yRot,zRot' to adjust the direction of axis,but it's confused me much.my aim is like:X axis from left to right,Y axis is rotated by X -45℃,Z axis vertical down.
how do i use 'xRot,yRot,zRot' to make the axis's direction?
GeneralRe: can you tell me the usage of class in demo project?pdf file will be OK!memberSharjith9 Feb '12 - 16:35 
My friend, I am more than ready to help you but please explain which class and methods are you using. Your explanation doesn't give me an accurate picture of what you are trying to do and where you are getting stuck.
RegardsSmile | :)
N. Sharjith

GeneralRe: can you tell me the usage of class in demo project?pdf file will be OK!memberMember 9159522 Feb '12 - 15:30 
How do adjust the direction of the x,y,z Axis.Now three Axis lines are displayed in the center of window,How can i modify the angle between x and y Axis.
Questionplease help : how to draw BSpline by using mouse ???membera04.lqd27 Nov '09 - 6:40 
then save the curve.
 
it is hard exercise for me.... Dead | X|
 
please help !!!
 
thank very much.
AnswerRe: please help : how to draw BSpline by using mouse ???memberRichard MacCutchan27 Nov '09 - 7:15 
So even using someone else's code you are incapable of completing the exercise given by your teacher. How you expect to pass your course is a mystery.
GeneralCompilation errormemberguims377009 Jul '08 - 6:45 
I would like to operate this sofware but when I build, I get this error(I use Visual C++ 6):
 
Linking...
LINK : fatal error LNK1104: cannot open file "VKGeom.lib"
Error executing link.exe.
 

CadSurf.exe - 1 error(s), 0 warning(s)
 
Could anyone help me?
 
Guims37700
GeneralMaterialDlg.cpp compilation errormemberhumorstar16 Sep '07 - 17:02 
When I open the cadsurf project in Visual C++ 2005, it has one compilation error in materialDlg.cpp as follows:
 
cadsurf\materialdlg.cpp(39) : error C2440: 'static_cast' : cannot convert from 'void (__thiscall CMaterialDlg::* )(UINT)' to 'BOOL (__thiscall CCmdTarget::* )(UINT)'
None of the functions with this name in scope match the target type
 
How to fix this problem?
 
Thanks for the nice work,

AnswerRe: MaterialDlg.cpp compilation errormemberSharjith20 Sep '07 - 4:43 
Stricter C++ adherence in (MFC) Visual Studio 2005 will not allow compilation unless many changes are made to the code. Please use Visual C++ 6 for building
 
Regds
N. SharjithSmile | :)
QuestionProblem in toolbar icon??memberHienBK992 Jan '07 - 14:54 
When I open this program and click some shape icons, this program will be stop immediately,is anyone same to me???
AnswerRe: Problem in toolbar icon??memberandyliujun5 Apr '07 - 2:28 
I also can't run it !Why not someone give the answer?

QuestionHave you used OpenCasCade?memberninja120 Apr '06 - 20:27 
Hi Sharjith
 
Have you ever experimented with OpenCasCade? Any comments on the same.
 
Nitin
GeneralCAD/CAM SoftwaresussStéphan15 Sep '05 - 22:35 
I'm all alone to do my own CAD/CAM software, and I would appreciate some kind of help.
If you are interrested to work on this kind of project please contact me.
QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox?memberwerter13 May '04 - 21:16 

How to run 2 different(or same) OpenGL objects in one DialogBox?
IDC_STATIC1 is a first OpenGL objects Scene window.
IDC_STATIC2 is a second OpenGL objects Scene window.
IDC_STATIC1 and IDC_STATIC2 ,they are placed on IDD_DIALOG1.
 
code:
pclStatic = (CStatic *)GetDlgItem(IDC_OpenGL_Window);
pclGlView = new CGlView(pclStatic);
pclGlView->OnCreate();
pclGlView->InitGL();
pclGlView->DrawGLScene();
 
pclStatic1 = (CStatic *)GetDlgItem(IDC_OpenGL_Window);
pclGlView1 = new CGlView(pclStatic1);
pclGlView1->OnCreate();
pclGlView1->InitGL();
pclGlView1->DrawGLScene();
 
Will not involve both windows (IDC_STATIC2,IDC_STATIC1)
Generalit is an example developed based on opencascadememberxazdw24 Mar '04 - 23:02 
If you are interested in it,please click www.opencascade.com to down the opencascade that is a large 3D modeling platform in CAD/CAM. the new version
is 5.1;);PBig Grin | :-D Smile | :)
 
I come from Xi'an Jiaotong university.
www.xjtu.edu.cn
GeneralHelp mememberrockers13 Apr '03 - 6:58 
How to draw line3D or cycle 3D by Mouse

GeneralRe: Help memembersataoa20 Apr '06 - 20:43 
Hello,
To do the things more positively. Just started u r coding and ask if the problem.
GeneralIt crash for me toosussAnonymous26 Feb '03 - 7:14 
If you start drawing something the program crash.
For example choosing the icon points cause the program to crash
in DefineDisplay at glDisable (GL_POINT_SMOOTH);
 
However it looks as a very good piece of code.
 
I'm looking forward for the full working version...
GeneralRe: It crash for me toosussAnonymous26 Feb '03 - 16:27 
Sleepy | :zzz: Sleepy | :zzz: Poke tongue | ;-P
GeneralRe: It crash for me toomemberSharjith27 Feb '03 - 22:52 
Hi,
Thanks for the feedback, the problem is probably with the graphics card u have. Please let me know your systems config.
 
Regards
N.Sharjith
GeneralStill crashingmemberpochadri25 Feb '03 - 19:07 
It is still crashing.please upload the correct version soon
 
Thank you

GeneralRe: Still crashingmemberSharjith27 Feb '03 - 23:03 
Hi,
Please try running the pogram after recompiling by commenting out the code parts for displaying the fonts in all the geometry creation tasks in the CCadSurfDoc events.
The fonts give trouble on graphic cards with no OpenGL support and low vram. The culprit is probably wglFonts.
 
I recommend a graphics card with full OpenGL drivers loaded and a vram of atleast 32mb.
 
Regards
N.Sharjith
 


GeneralcrashmemberJon19 Feb '03 - 1:00 
If you run the exe and drag a selection over the axes the program crashes.
GeneralRe: crashmemberSharjith24 Feb '03 - 23:55 
Hi Jon,
Thankyou for the feedback. I think it is some thing related to the graphics card you have. I have come across the problems like selection not happening, crashing if fonts displayed etc. on other computers with no AGP cards or low level cards. The graphics card must have full OpenGL support and enough video memory. The project is updated now. You may download the fresh one. All the best!
 
Regards
N. SharjithSmile | :)
GeneralBig Honking PicturememberEd Gadziemski18 Feb '03 - 4:08 
That must be the largest screenshot ever in a CodeProject article. The source code downloads are huge, too. Are you seeking the award for biggest picture and files?
GeneralRe: Big Honking PicturememberRick York18 Feb '03 - 6:41 
This is what happens when the .APS, .NCB, and .OPT files are included in the archive which is unnecessary since they are all regeneratable.
 
Sharjith : you should add the height and width attributes to the img tag so that the image is displayed at the correct size.
 

The Ten Commandments For C Programmers

GeneralRe: Big Honking PicturesussAnonymous26 Feb '03 - 16:26 
Dead | X| Confused | :confused:
GeneralRe: Big Honking PicturememberChopper18 Feb '03 - 6:52 
And I bet the line with Windows Task Bar is conceptually necessary.... Smile | :)

 
Ultimate all-in-one XP-Style UI multiplatform solution: Tooltips, XP Menus, Hyperlinks, Drawing Graphics and formatted documents, plus powerful binary resource reuse. All Free 100%, visit www.tooltips.net now. It can be used in VC++, C#, Java, VB, Delphi, Borland C++, Borland C++ Builder, as well as any COM-compatible platform.
GeneralRe: Big Honking PicturememberClaudius Mokler19 Feb '03 - 1:36 
Yeah, pressing the "Print" key is that much cooler than pressing "Alt" + "Print" keys together ...
GeneralRe: Big Honking PicturememberLars Wadefalk26 Feb '03 - 1:22 
Dead | X|
If he WOULD have pressed the "Print" key only, he would have got the entire screen in the dump (which he obviously didn't).
I can't see what's wrong in doing a sufficiently large application dump in the article.
 
It gives you a better idea of what everything is all about...don't you think?

 
Lars Wadefalk
Siemens Laser Analytics

GeneralRe: Big Honking PicturememberClaudius Mokler27 Feb '03 - 0:18 
> If he WOULD have pressed the "Print" key only, he would have got the entire screen in the dump (which he obviously didn't).
He did so originally, but has updated his article since.
 
> I can't see what's wrong in doing a sufficiently large application dump in the article.
No? Who's interested in *your* computer's task bar and why should something that can be said with, say 640x480 pixels done so with a XGA or even UXGA image?
 


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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 18 Apr 2012
Article Copyright 2003 by Sharjith
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid