Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » OpenGL » General » Downloads
 
Add your own
alternative version

Basic Curves And Surfaces Modeler

, 18 Apr 2012
A basic demo of modeling curves and surfaces in OpenGL.
cadsurfexe.zip
CadSurfexe
CadSurf.exe
VKGeom.dll
VKGraphic.dll
cadsurf_demo.zip
cadsurf_src.zip
CadSurf_src
VKernel
VKGraphic
VKGraphic.dsp
VKGraphic.dsw
VKGraphic.plg
VKGeom
VKGeom.dsp
VKGeom.dsw
cadsurf
CadSurf.clw
CadSurf.dsp
cadsurf.dsw
Thumbs.db
res
bmp00001.bmp
cadsurf.ico
CADSURFDOC.ICO
new_splash.bmp
new_splash.PNG
new_splash_small.bmp
tb_geom.bmp
Thumbs.db
toolbar.bmp
toolbar1.bmp
untitled.bmp
XPStyle.manifest
CadSurf_VS2008.zip
CadSurf
cadsurf
CadSurf.suo
res
bmp00001.bmp
cadsurf.ico
CADSURFDOC.ICO
new_splash.bmp
new_splash.PNG
new_splash_small.bmp
tb_geom.bmp
Thumbs.db
toolbar.bmp
toolbar1.bmp
untitled.bmp
VKernel
VKGeom
VKGraphic
VKGraphic.plg
#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include "stdafx.h"

#include "Bsp.h"
#include "MMath.h"

void bspline(int n, int t, point *control, point *output, int num_output)

/*********************************************************************

Parameters:
  n          - the number of control points minus 1
  t          - the degree of the polynomial plus 1
  control    - control point array made up of point stucture
  output     - array in which the calculate spline points are to be put
  num_output - how many points on the spline are to be calculated

Pre-conditions:
  n+2>t  (no curve results if n+2<=t)
  control array contains the number of points specified by n
  output array is the proper size to hold num_output point structures


**********************************************************************/

{
  int *u;
  double increment,interval;
  point calcxyz;
  int output_index;

  u=new int[n+t+1];
  compute_intervals(u, n, t);

  increment=(double) (n-t+2)/(num_output-1);  // how much parameter goes up each time
  interval=0;


  for (output_index=0; output_index<num_output-1; output_index++)
  {
    compute_point(u, n, t, interval, control, &calcxyz);
    output[output_index].x = calcxyz.x;
    output[output_index].y = calcxyz.y;
    output[output_index].z = calcxyz.z;
    interval=interval+increment;  // increment our parameter
  }
  output[num_output-1].x=control[n].x;   // put in the last point
  output[num_output-1].y=control[n].y;
  output[num_output-1].z=control[n].z;

  delete u;
}

double blend(int k, int t, int *u, double v)  // calculate the blending value
{
  double value;

  if (t==1)			// base case for the recursion
  {
    if ((u[k]<=v) && (v<u[k+1]))
      value=1;
    else
      value=0;
  }
  else
  {
    if ((u[k+t-1]==u[k]) && (u[k+t]==u[k+1]))  // check for divide by zero
      value = 0;
    else
    if (u[k+t-1]==u[k]) // if a term's denominator is zero,use just the other
      value = (u[k+t] - v) / (u[k+t] - u[k+1]) * blend(k+1, t-1, u, v);
    else
    if (u[k+t]==u[k+1])

      value = (v - u[k]) / (u[k+t-1] - u[k]) * blend(k, t-1, u, v);
    else
      value = (v - u[k]) / (u[k+t-1] - u[k]) * blend(k, t-1, u, v) +
	      (u[k+t] - v) / (u[k+t] - u[k+1]) * blend(k+1, t-1, u, v);
  }
  return value;
}

void compute_intervals(int *u, int n, int t)   // figure out the knots
{
  int j;

  for (j=0; j<=n+t; j++)
  {
    if (j<t)
      u[j]=0;
    else
    if ((t<=j) && (j<=n))
      u[j]=j-t+1;
    else
    if (j>n)
      u[j]=n-t+2;  // if n-t=-2 then we're screwed, everything goes to 0
  }
}

void compute_point(int *u, int n, int t, double v, point *control,
			point *output)
{
  int k;
  double temp;

  // initialize the variables that will hold our outputted point
  output->x=0;
  output->y=0;
  output->z=0;

  for (k=0; k<=n; k++)
  {
    temp = blend(k,t,u,v);  // same blend is used for each dimension coordinate
    output->x = output->x + (control[k]).x * temp;
    output->y = output->y + (control[k]).y * temp;
    output->z = output->z + (control[k]).z * temp;
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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 Ltd
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Apr 2012
Article Copyright 2003 by Sharjith
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid