Skip to main content
gamedev.net
🔒 Locked

3d wireframe cube...

Started by jbruno 0 replies 400+ views
Original Post
jbruno
jbruno
I am having a really hard time figuring this out. It has nothing to do with the math or getting the cube on the screen that is easy. I am having problems with functions.
#include 
#include 
#include 
#include 

#define v 5
#define PI 3.14
#define a PI/36

const int xcoord = 0, ycoord = 1, zcoord = 2, maxcoord = 3;

void draw_cube(double cube[][maxcoord]);
// Draws cube on screen using coordinates passed from array

int main()
{
    double d = 2;
    double cube[8][3] = { {-1, 1, 1},
                         {1, 1, 1},
                         {1, -1, 1},
                         {-1, -1, 1},
                         {-1, 1, -1},
                         {1, 1, -1},
                         {1, -1, -1},
                         {-1, -1, -1} };
    char c;

    initwindow(400, 400); // Opens window to draw cube in

    for(int row = 0; row <= 7; row++) // Loop to convert 3d point to 2d point
    {
        cube[row][xcoord] = cube[row][xcoord] * (v / (v + d - cube[row][zcoord])); // Converts each 3d x coordinate stored in array cube to 2d
        cube[row][xcoord] = (cube[row][xcoord] * 100) + 200; // Converts from cartesian system used to do calculations to pixel coordinates
        cube[row][ycoord] = cube[row][ycoord] * (v / (v + d - cube[row][zcoord])); // Converts each 3d y coordinate stored in array cube to 2d
        cube[row][ycoord] = (cube[row][ycoord] * -100) + 200; // Converts from cartesian system used to do calculations to pixel coordinates
    }

    draw_cube(cube[][maxcoord]);

    getch();
}

void draw_cube(double cube[][maxcoord])
{
    line(cube[0][xcoord], cube[0][ycoord], cube[1][xcoord], cube[1][ycoord]);
    line(cube[1][xcoord], cube[1][ycoord], cube[2][xcoord], cube[2][ycoord]);
    line(cube[2][xcoord], cube[2][ycoord], cube[3][xcoord], cube[3][ycoord]);
    line(cube[3][xcoord], cube[3][ycoord], cube[0][xcoord], cube[0][ycoord]);
    line(cube[4][xcoord], cube[4][ycoord], cube[5][xcoord], cube[5][ycoord]);
    line(cube[5][xcoord], cube[5][ycoord], cube[6][xcoord], cube[6][ycoord]);
    line(cube[6][xcoord], cube[6][ycoord], cube[7][xcoord], cube[7][ycoord]);
    line(cube[7][xcoord], cube[7][ycoord], cube[4][xcoord], cube[4][ycoord]);
    line(cube[0][xcoord], cube[0][ycoord], cube[4][xcoord], cube[4][ycoord]);
    line(cube[1][xcoord], cube[1][ycoord], cube[5][xcoord], cube[5][ycoord]);
    line(cube[2][xcoord], cube[2][ycoord], cube[6][xcoord], cube[6][ycoord]);
    line(cube[3][xcoord], cube[3][ycoord], cube[7][xcoord], cube[7][ycoord]);
}
 
I need to be able to call this function to be able to finish the program. It just won''t work no matter what I try. I can''t see any other way to do this. Your help will be greatly appreciated.
Advanced Bug
Advanced Bug
Try change

draw_cube(cube[][maxcoord]);

to

draw_cube(cube);

in your int main() function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.