Click here to Skip to main content
15,896,278 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 248.8K   16.4K   117  
A basic demo of modeling curves and surfaces in OpenGL.
// GLCamera.cpp: implementation of the CGLCamera class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "GLCamera.h"
#include <MMath.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SF3dVector F3dVector ( GLdouble x, GLdouble y, GLdouble z )
{
    SF3dVector tmp;
    tmp.x = x;
    tmp.y = y;
    tmp.z = z;
    return tmp;
}

SF3dVector AddF3dVectors (SF3dVector* u, SF3dVector* v)
{
    SF3dVector result;
    result.x = u->x + v->x;
    result.y = u->y + v->y;
    result.z = u->z + v->z;
    return result;
}

void AddF3dVectorToVector ( SF3dVector * Dst, SF3dVector * V2)
{
    Dst->x += V2->x;
    Dst->y += V2->y;
    Dst->z += V2->z;
}


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

CGLCamera::CGLCamera()
{
    //Init with standard OGL values:
    Position = F3dVector (	0.0, 
	    0.0,
	    0.0);
    ViewDir = F3dVector(	0.0,
	    0.0,
	    -1.0);
    ViewDirChanged = false;
    //Only to be sure:
    RotatedX = RotatedY = RotatedZ = 0.0;
}

void CGLCamera::GetViewDir( void )
{
    SF3dVector Step1, Step2;
    //Rotate around Y-axis:
    Step1.x = cos( (RotatedY + 90.0) * PIdiv180);
    Step1.z = -sin( (RotatedY + 90.0) * PIdiv180);
    //Rotate around X-axis:
    double cosX = cos (RotatedX * PIdiv180);
    Step2.x = Step1.x * cosX;
    Step2.z = Step1.z * cosX;
    Step2.y = sin(RotatedX * PIdiv180);
    //Rotation around Z-axis not yet implemented, so:
    ViewDir = Step2;
}

void CGLCamera::Move (SF3dVector Direction)
{
    AddF3dVectorToVector(&Position, &Direction );
}

void CGLCamera::RotateY (GLdouble Angle)
{
    if( RotatedY > 360 || RotatedY < -360 )
	RotatedY = 0;
    RotatedY += Angle;
    ViewDirChanged = true;
}

void CGLCamera::RotateX (GLdouble Angle)
{
    if( RotatedY > 360 || RotatedY < -360 )
	RotatedX = 0;
    RotatedX += Angle;
    ViewDirChanged = true;
}

void CGLCamera::Render( void )
{
    ::glRotated(-RotatedX , 1.0, 0.0, 0.0);
    ::glRotated(-RotatedY , 0.0, 1.0, 0.0);
    ::glRotated(-RotatedZ , 0.0, 0.0, 1.0);
    ::glTranslated( -Position.x, -Position.y, -Position.z );
}

void CGLCamera::MoveForwards( GLdouble Distance )
{
    if (ViewDirChanged) GetViewDir();
    SF3dVector MoveVector;
    MoveVector.x = ViewDir.x * -Distance;
    MoveVector.y = ViewDir.y * -Distance;
    MoveVector.z = ViewDir.z * -Distance;
    AddF3dVectorToVector(&Position, &MoveVector );
}

void CGLCamera::StrafeRight ( GLdouble Distance )
{
    if (ViewDirChanged) GetViewDir();
    SF3dVector MoveVector;
    MoveVector.z = -ViewDir.x * -Distance;
    MoveVector.y = 0.0;
    MoveVector.x = ViewDir.z * -Distance;
    AddF3dVectorToVector(&Position, &MoveVector );
}

void CGLCamera::ResetCamera()
{
    Position = F3dVector(0.0, 0.0,	0.0);
    ViewDir = F3dVector(0.0, 0.0, -1.0);
    ViewDirChanged = false;
    RotatedX = RotatedY = RotatedZ = 0.0;
}

void CGLCamera::SetPosition(SF3dVector pos)
{
    Position.x = pos.x;
    Position.y = pos.y;
    Position.z = pos.z;
}

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)


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