?
Solved

3d rotating cube in c++

Posted on 2006-07-14
10
Medium Priority
?
4,136 Views
Last Modified: 2013-12-04
How can i make a 3D cube in C++ and  also  rotate it .. using Turbo C++ 3.0
No VC++ or OpenGL or DirectX please..
I want to make it in simple C++
0
Comment
Question by:shilpi84
  • 5
  • 4
10 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17109895
Are you happy to use GDI/+ for this? Or do you wish to incorporate even your own line/polygon rendering?

How is your math?
You're going to need to be familiar with vectors, matrices, and projections for this.
0
 

Author Comment

by:shilpi84
ID: 17110255
i m familiar with  matrices and projections!the problem is i m not able 2 put my knowledge 2 make a program.dont know how 2 start:(
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17110370
What problem(s) are you having with this, specifically?

How confident are you with C++?
Is your OOP any good?
Have you learnt to multi-thread in C++?
Are you able to use GDI/+ (which you could use for 2D rendering to the screen)?
  If so, then have you learnt any of this before?
Have you figured out how to represent and manipulate the 3D cube programmatically?
And..Do you wish to incorporate some form of lighting into this?
  If so, then are you happy with how exactly you would do this?

Anything else..?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:shilpi84
ID: 17110519
>>How confident are you with C++?

on a scale of 10 i wud give my self 6Have you learnt to multi-thread in C++?.

Is your OOP any good?

i m quiet familiar with oop concepts.

Have you learnt to multi-thread in C++?.

nope but i know multithreading of java.i just took its lesson.

About representing the cube i know how 2 project 3d coordinates onto 2d plane.i reffered baker 4 the rotation and other transformation matrices.
i dont want ne special effects in my cube no lightting and all.actually i m trying 2 make a rotating dice 4 my game.

and plz dont say that u wont be able 2 help me remember u helped me a lot in getting through infosys(puzzles).i really want 2 make this.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17110791
Why not use Java for this (at least, for now) ?
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17110795
(It's entirely up to you... it would just be easier if you learnt to do this sort of thing in a more familiar environment, and then perhaps ported it to C++ at a later stage?)
0
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17110965
/****************************************************************************\
*                                                                            *
*  Cube.cpp                                                                  *
*  CubeU.cpp                                                                 *
*                                                                            *
*  This program draws a cube in 3D world space and allows the user to move   *
*  and rotate the cube through keyboard controls. Each of the six cube faces *
*  is a different color.                                                     *
*                                                                            *
\****************************************************************************/
#include
#pragma hdrstop
#include "CubeU.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnIdle(TObject *Sender, bool &Done)
{
   CheckForMovement();
   Done = False;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
   fg_realize(hPal);
   Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
   hDC = GetDC(Form1->Handle);
   fg_setdc(hDC);
   hPal = fg_defpal();
   fg_realize(hPal);
   fg_vbinit();
   vbDepth = fg_colors();
   fg_vbdepth(vbDepth);
   hVB = fg_vballoc(vbWidth,vbHeight);
   fg_vbopen(hVB);
   fg_vbcolors();
   fg_setcolor(-1);
   fg_fillpage();
   fg_3Dviewport(0,vbWidth-1,0,vbHeight-1,0.5);
   fg_3Drenderstate(FG_ZCLIP);
   Application->OnActivate = OnActivate;
   Application->OnIdle = OnIdle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
   fg_vbscale(0,vbWidth-1,0,vbHeight-1,0,cxClient-1,0,cyClient-1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
   cxClient = ClientWidth;
   cyClient = ClientHeight;
   Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
   fg_vbclose();
   fg_vbfree(hVB);
   fg_vbfin();
   DeleteObject(hPal);
   ReleaseDC(Form1->Handle,hDC);
}
/****************************************************************************\
*                                                                            *
*  CheckForMovement()                                                        *
*                                                                            *
*  The CheckForMovement() function checks for key presses that control the   *
*  cube's movement, and if required redraws the cube at its new position and *
*  orientation. It is called from the application's OnIdle event handler.    *
*                                                                            *
\****************************************************************************/
void __fastcall TForm1::CheckForMovement()
{
   static double xWorld = 0.0, yWorld = 0.0, zWorld = 100.0;
   static int xAngle = 0, yAngle = 0, zAngle = 0;
   static bool Redraw = True;
   bool ShiftKey;
   // check if either shift key is pressed
   ShiftKey = fg_kbtest(42) | fg_kbtest(54);
   // + and - move cube along the z axis (+ is toward viewer, - is
   // away from viewer)
   if (fg_kbtest(74))
   {
      zWorld += 3.0;
      Redraw = True;
   }
   else if (fg_kbtest(78))
   {
      zWorld -= 3.0;
      Redraw = True;
   }
   // left and right arrow keys move cube along x axis
   else if (fg_kbtest(75))
   {
      xWorld -= 3.0;
      Redraw = True;
   }
   else if (fg_kbtest(77))
   {
      xWorld += 3.0;
      Redraw = True;
   }
   // up and down arrow keys move cube along y axis
   else if (fg_kbtest(72))
   {
      yWorld += 3.0;
      Redraw = True;
   }
   else if (fg_kbtest(80))
   {
      yWorld -= 3.0;
      Redraw = True;
   }
   // x rotates counterclockwise around x axis, X rotates clockwise
   else if (fg_kbtest(45))
   {
      if (ShiftKey)
      {
         xAngle += 6;
         if (xAngle >= 360) xAngle -= 360;
      }
      else
      {
         xAngle -= 6;
         if (xAngle < 0) xAngle += 360;
      }
      Redraw = True;
   }
   // y rotates counterclockwise around y axis, Y rotates clockwise
   else if (fg_kbtest(21))
   {
      if (ShiftKey)
      {
         yAngle += 6;
         if (yAngle >= 360) yAngle -= 360;
      }
      else
      {
         yAngle -= 6;
         if (yAngle < 0) yAngle += 360;
      }
      Redraw = True;
   }
   // z rotates counterclockwise around z axis, Z rotates clockwise
   else if (fg_kbtest(44))
   {
      if (ShiftKey)
      {
         zAngle += 6;
         if (zAngle >= 360) zAngle -= 360;
      }
      else
      {
         zAngle -= 6;
         if (zAngle < 0) zAngle += 360;
      }
      Redraw = True;
   }
   // if the cube's position or rotation changed, redraw the cube
   if (Redraw)
   {
      // erase the previous frame from the virtual buffer
      fg_setcolor(-1);
      fg_fillpage();
      // define the cube's new position and rotation in 3D world space
      fg_3Dsetobject(xWorld,yWorld,zWorld,xAngle*10,yAngle*10,zAngle*10);
      // draw the cube
      DrawCube();
      // display what we just drew
      fg_vbscale(0,vbWidth-1,0,vbHeight-1,0,cxClient-1,0,cyClient-1);
      Redraw = False;
   }
}
/****************************************************************************\
*                                                                            *
*  DrawCube()                                                                *
*                                                                            *
*  Draws each of the six cube faces in 3D world space.                       *
*                                                                            *
\****************************************************************************/
void __fastcall TForm1::DrawCube()
{
   register int i;
   int r, g, b;
   static int Colors[] = {84,88,92,96,100,104};
   for (i = 0; i < 6; i++)
   {
      if (vbDepth > 8)
      {
         fg_getrgb(Colors[i],&r,&g,&b);
         fg_setcolorrgb(r,g,b);
      }
      else
      {
         fg_setcolor(Colors[i]);
      }
      fg_3Dpolygonobject((double *)Faces[i],4);
   }
}


Source: http://www.fastgraph.com/help/cube_builder.html
0
 

Author Comment

by:shilpi84
ID: 17111064
InteractiveMind, I think you have a valid point .. i'll make a static cube only..
I got some codes from various sites but i wanted to understand it..
I've made a Snakes n Ladders game in c++ so i'm not going to restart it in Java ..

And how tough is OpenGL/DirectX? And if i start reading tutorials(from d net), abt how long will it take?
0
 

Author Comment

by:shilpi84
ID: 17111067
Which is better DirectX or OpenGL and especially easier to learn
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 1500 total points
ID: 17111196
Many people (including myself) prefer OpenGL to DirectX.
I found OpenGL a lot easier to learn.

If you check out the NeHe tutorials (on OpenGL), then you could perhaps get something working by tonight:
http://nehe.gamedev.net/


[[ But nothing beats a good book! ]]
      (The OpenGL Red Book!!)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question