Sign in to follow this  
tppramod

Whats wrong with this code! - Rotations

Recommended Posts

tppramod    122
Hi all, Here is a code for 3D rotation on X-axis, Y-axis and Z-axis for a cube. Out of which only Z-axis is working fine! X-Axis & Y-axis is also functioning but i don't know why it is poping out of the screen when the value degree increases. The main function is DrawVertex(). Can anyone tell me where i going wrong. This program is in mode 13h having screen resolution 320 x 200 pixels and is writen in Turbo C. i have commented X-Axis & Y-Axis so that only Z-Axis works. Press ESC to come out of the program. Here is the simple code.
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <memory.h>
#include <dos.h>
#include <stdio.h>
#include <math.h>


//3D CUBE DEMONSTRATION - NOT WORKING! PLEASE HELP!

//ALL VARIABLS
typedef unsigned char byte;
byte *On_Screen=(byte *)MK_FP(0xA000,0);

int X, Y, Z;
int X1, Y1, Z1;
int X2, Y2, Z2;
int X3, Y3, Z3;

int   XOrigin=160, YOrigin=100;  //Are the coordinates of the center of the video display
int   FocalDistance=200;         //Range 80-200 - Distance from the viewer to the screen
float RadiansPerDegree=0.0174533;
float Angle;

int No_Of_Vertex=8;
int ScreenX[8], ScreenY[8];    //For storing 2D Screen Coordinates

float Cube[8][3]      = {
			  {-50, 50,-50 }, //1
			  { 50, 50,-50 }, //2
			  {-50,-50,-50 }, //3
			  { 50,-50,-50 }, //4
			  {-50, 50, 50 }, //5
			  { 50, 50, 50 }, //6
			  {-50,-50, 50 }, //7
			  { 50,-50, 50 }, //8
			};

//KEYBOARD VARIABLES
int   ascii, scan, cont=1;
union REGS ii,oo;
char  kb;



//---------------------------- Screen Codes Begins --------------------------
void EnterMode13H(void)
{union REGS regs;regs.x.ax = 0x0013;int86(0x10, ®s, ®s);}

void LeaveMode13H(void)
{union REGS regs;regs.x.ax = 0x0003;int86(0x10, ®s, ®s);}
//------------------------------------------------------------------------------

//------------------------- Plot-Read Pixels Begins -------------------------
void PlotPixel (signed int x, signed int y, signed char c)
  {
    if ((x < 320) && (y < 200))
     { On_Screen[(y << 8) + (y << 6) + x] = c; }
  }

//------------------------------------------------------------------------------

//------------------------------- DrawCube BEGINS ------------------------------

DrawVertex()
  {
    int Vertices, Degrees;
    float ScaleFactor;
    PlotPixel(160,100,14); //MIDDLE OF THE SCREEN

    for (Degrees=0;Degrees<360;Degrees++)
      {
	for (Vertices=0;Vertices<8;Vertices++)  //DRAW VERTICES
	  {
	    Angle = (RadiansPerDegree*Degrees);
	    X =  Cube[Vertices][0];
	    Y =  Cube[Vertices][1];
	    Z =  Cube[Vertices][2];

	    Z += 500;
/*
	    //X-Axis <---- DISABLED
	    X =  X;
	    Y = (cos(Angle)*Y) - (sin(Angle)*Z);  
	    Z = (sin(Angle)*Y) + (cos(Angle)*Z);  

	    //Y-Axis <---- DISABLED
	    X =  (cos(Angle)*X) + (sin(Angle)*Z);
	    Y =  Y;
	    Z = -(sin(Angle)*X) + (cos(Angle)*Z);
*/
	    //Z-Axis <--- ENABLED - WORKING FINE!
	    X = (cos(Angle)*X) - (sin(Angle)*Y);
	    Y = (sin(Angle)*X) + (cos(Angle)*Y);
	    Z =  Z;

	    if (Z == 0) { Z = 1;} // Avoid division by zero

	    //3D to 2D Transformation
	    ScreenX[Vertices]=FocalDistance*X/Z+XOrigin;
	    ScreenY[Vertices]=FocalDistance*Y/Z+YOrigin;

	    PlotPixel(ScreenX[Vertices],ScreenY[Vertices],12);
	  }

	delay(30);

	for (Vertices=0;Vertices<8;Vertices++)
	  {
	    PlotPixel(ScreenX[Vertices],ScreenY[Vertices],0);
	  }
      }
  }
//------------------------------- DrawCube ENDS ------------------------------
void main()
  {
    EnterMode13H();
    DrawVertex();
    do
      {
	if (kbhit())
	  {
	    ii.h.ah=0;
	    int86 (22,&ii,&oo);
	    ascii=oo.h.al;
	    scan=oo.h.ah;

	    switch (scan)
	      {
		case  1: //ESC   KEY PRESSED
		cont = 0; farfree(On_Screen); LeaveMode13H();
		break;
	      }
	  }
      }
    while (cont == 1);
  }


Thanks & Regards, pramod [Edited by - tppramod on June 30, 2005 12:28:52 AM]

Share this post


Link to post
Share on other sites
smart_idiot    1298
I think you might need to use a temporary. For example, rotating around Z, you modify X and then use the new value when modifying Y. You want to use the original value of X, not the new value.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this