Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.graphics.algorithms > #879
| From | David Melik <dchmelik@hipplanet.com> |
|---|---|
| Newsgroups | comp.graphics.algorithms, comp.sys.ibm.pc.demos |
| Subject | Re: figured part out; new questions |
| Date | 2012-06-22 04:33 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <js1l5t$ems$1@dont-email.me> (permalink) |
| References | <js189n$aso$1@dont-email.me> <js1gs8$n8h$1@dont-email.me> |
Cross-posted to 2 groups.
//cube.cpp
//Here is an example of what I did since my last post. It shows a small
but visible cube with slightly warped perspective close to (1,1). What
do I need to do to bring this out to the middle of the screen and have
normal-looking perspective? (see comments in last post)
//#define gcc
//#define tc
#define wc
#include <conio.h> //getch(), Turbo C clrscr()
#ifdef wc
#include <graph.h> //Watcom _getactivepage(), _getvisualpage(),
_setactivepage(), _setvisualpage()
#include <i86.h> //Watcom delay()
#endif
#ifdef tc
#include <graphics.h> //Turbo C initgraph()
#endif
#include <math.h> //ceil(), cos(), floor(), sin(), sqrt()
#include <stdio.h> //printf()
#include <stdlib.h> //abs()
void put_pixel(int x, int y, int colour);
void line(int x0, int y0, int x50, int y50, int colour);
void draw_square(int square[][4], int colour);
void draw_cube(int cube[][8], int colour);
void clrscr();
int main(void)
{
int i;
//define polyhedra
int cube_100[3][8]={{-50, -50, 50, 50, -50, -50, 50, 50},
{-50, 50, 50, -50, -50, 50, 50, -50},
{-50, -50, -50, -50, 50, 50, 50, 50}};
int cube_a[3][8]={{-50, -50, 50, 50, -50, -50, 50, 50},
{-50, 50, 50, -50, -50, 50, 50, -50},
{-50, -50, -50, -50, 50, 50, 50, 50}};
float x,y,z;
//define 3D angles and rotation matrices
//alpha, beta, lambda
//float a=0.785398; //pi/4;
//float b=0.785398;
float a=0, b=0, l=0.785398;
float mR3a[3][3]={{50, 0, 0}, {0, cos(b), sin(b)}, {0, -sin(l), cos(l)}};
float mR3b[3][3]={{cos(a), 0, -sin(a)}, {0, 1, 0}, {sin(l), 0, cos(l)}};
float mR3l[3][3]={{cos(a), sin(a), 0}, {-sin(b), cos(b), 0}, {0, 0, 1}};
float mR3r[3][3]={{cos(b)*sin(l), cos(b)*sin(l),
-sin(b)},{sin(a)*sin(b)*cos(l)-cos(b)*sin(l),
sin(a)*sin(b)*sin(l)+cos(a)*cos(b),sin(a)*cos(b)},{cos(a)*sin(b)*cos(l)+sin(a)*sin(l),cos(a)*sin(b)*sin(l)-sin(a)*cos(b),
cos(a)*cos(b)}};
//clearscreen, set video mode 640x480x16; return error if impossible
//initgraph(VGA, 2, ""); //almost right in Turbo C
clrscr();
if(_setvideomode(_VRES16COLOR)==0)
{
printf("%s", "Cannot switch to 640x480x16 colour mode.\r\n");
return 1;
}
for(i=0;i<=7;i++)
{
cube_a[0][i]+=316;
cube_a[1][i]+=236;
cube_a[2][i]+=301;
}
draw_cube(cube_a,2);
getch();
//increment lambda and rotate cube on that or also other angles
for(l=0;l<=6.283185;l+=0.104720)
{
mR3r[0][0]=cos(b)*sin(l);
mR3r[0][1]=cos(b)*sin(l);
mR3r[0][2]=-sin(b);
mR3r[1][0]=sin(a)*sin(b)*cos(l)-cos(b)*sin(l);
mR3r[1][1]=sin(a)*sin(b)*sin(l)+cos(a)*cos(b);
mR3r[1][2]=sin(a)*cos(b);
mR3r[2][0]=cos(a)*sin(b)*cos(l)+sin(a)*sin(l);
mR3r[2][1]=cos(a)*sin(b)*sin(l)-sin(a)*cos(b);
mR3r[2][2]=cos(a)*cos(b);
for(i=0; i<=7; i++)
{
x=mR3r[0][0]*cube_100[0][i]+mR3r[0][1]*cube_100[1][i]+mR3r[0][2]*cube_100[2][i];
y=mR3r[1][0]*cube_100[0][i]+mR3r[1][1]*cube_100[1][i]+mR3r[1][2]*cube_100[2][i];
z=mR3r[2][0]*cube_100[0][i]+mR3r[2][1]*cube_100[1][i]+mR3r[2][2]*cube_100[2][i];
cube_a[0][i]=(int)x;
cube_a[1][i]=(int)y;
cube_a[2][i]=(int)z;
}
for(i=0;i<=7;i++)
{
cube_a[0][i]+=316;
cube_a[1][i]+=236;
cube_a[2][i]+=301;
}
delay(10);
clrscr();
draw_cube(cube_a, 2);
}
//wait; set video mode to text; exit
getch();
_setvideomode(_TEXTC80);
return 0;
}
#ifdef wc
void clrscr()
{
//Watcom clearscreen
_clearscreen(_GCLEARSCREEN);
}
void put_pixel(int x, int y, int colour)
{
_moveto(x, y);
_setcolor(colour);
_setpixel(x, y);
}
void line(int x0, int y0, int x1, int y1, int colour)
{
_moveto(x0,y0);
_setcolor(colour);
_lineto(x1,y1);
}
#endif
void draw_cube(int cube[][8], int colour)
{
line(50*cube[0][0]/cube[2][0],50*cube[1][0]/cube[2][0],50*cube[0][1]/cube[2][1],50*cube[1][1]/cube[2][1],
colour);
line(50*cube[0][0]/cube[2][0],50*cube[1][0]/cube[2][0],50*cube[0][3]/cube[2][3],50*cube[1][3]/cube[2][3],
colour);
line(50*cube[0][0]/cube[2][0],50*cube[1][0]/cube[2][0],50*cube[0][4]/cube[2][4],50*cube[1][4]/cube[2][4],
colour);
line(50*cube[0][1]/cube[2][1],50*cube[1][1]/cube[2][1],50*cube[0][2]/cube[2][2],50*cube[1][2]/cube[2][2],
colour);
line(50*cube[0][1]/cube[2][1],50*cube[1][1]/cube[2][1],50*cube[0][5]/cube[2][5],50*cube[1][5]/cube[2][5],
colour);
line(50*cube[0][2]/cube[2][2],50*cube[1][2]/cube[2][2],50*cube[0][6]/cube[2][6],50*cube[1][6]/cube[2][6],
colour);
line(50*cube[0][3]/cube[2][3],50*cube[1][3]/cube[2][3],50*cube[0][2]/cube[2][2],50*cube[1][2]/cube[2][2],
colour);
line(50*cube[0][3]/cube[2][3],50*cube[1][3]/cube[2][3],50*cube[0][7]/cube[2][7],50*cube[1][7]/cube[2][7],
colour);
line(50*cube[0][4]/cube[2][4],50*cube[1][4]/cube[2][4],50*cube[0][7]/cube[2][7],50*cube[1][7]/cube[2][7],
colour);
line(50*cube[0][5]/cube[2][5],50*cube[1][5]/cube[2][5],50*cube[0][6]/cube[2][6],50*cube[1][6]/cube[2][6],
colour);
line(50*cube[0][5]/cube[2][5],50*cube[1][5]/cube[2][5],50*cube[0][4]/cube[2][4],50*cube[1][4]/cube[2][4],
colour);
line(50*cube[0][7]/cube[2][7],50*cube[1][7]/cube[2][7],50*cube[0][6]/cube[2][6],50*cube[1][6]/cube[2][6],
colour);
}
Back to comp.graphics.algorithms | Previous | Next — Previous in thread | Next in thread | Find similar
questions about rotating cube graphics in C++ (aiming for C) David Melik <dchmelik@hipplanet.com> - 2012-06-22 00:53 -0700
figured part out; new questions David Melik <dchmelik@hipplanet.com> - 2012-06-22 03:19 -0700
Re: figured part out; new questions David Melik <dchmelik@hipplanet.com> - 2012-06-22 04:33 -0700
Re: figured part out; new questions Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-06-24 00:40 +0200
Re: figured part out; new questions David Melik <dchmelik@hipplanet.com> - 2012-06-23 19:29 -0700
Re: figured part out; new questions Nobody <nobody@nowhere.com> - 2012-06-25 22:40 +0100
Re: figured part out; new questions Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-06-26 00:20 +0200
Re: figured part out; new questions legalize+jeeves@mail.xmission.com (Richard) - 2012-06-29 18:59 +0000
Re: figured part out; new questions David Melik <dchmelik@hipplanet.com> - 2012-06-23 19:36 -0700
Re: figured part out; new questions JohnF <john@please.see.sig.for.email.com> - 2012-07-01 13:50 +0000
Re: figured part out; new questions David Melik <dchmelik@hipplanet.com> - 2012-07-08 22:36 -0700
csiph-web