delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/04/20:00:49

From: Dusty <dusti2 AT pipeline DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Whats wrong with this code??
Date: Thu, 04 Jun 1998 19:03:00 -0400
Organization: MindSpring Enterprises
Lines: 179
Message-ID: <357727A4.A4DDD451@pipeline.com>
NNTP-Posting-Host: user-38ldbq9.dialup.mindspring.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

A couple of days ago I started on my first 3d program in which it's only

function was to convert eight 3d vertices to 2d coordinates, and connect

them with lines in such a way that it formed a cube. I am using Allegro
to provide the graphics functions like blitting, lines, ect. Sounds
simple
and straight forward right? Well it's been anything but simple and easy.

My problem is that I can't get anything of the lines displayed. I know
that the project_points() function works because I made a program that
outputs the resulting numbers, and when graphed and connected make a
nice
cube. Could someone please help a beginner, and tell my what I am doing
wrong. Thanks in advance.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "allegro.h"

#define  SCREENW         640
#define  SCREENH         480
#define  FOCAL_DISTANCE  256
#define  Xorigin         SCREENW / 2
#define  Yorigin         SCREENH / 2
#define  NUM_VERTS       7
#define  NUM_SQR         3



typedef struct CUBE
{
        int x,y,z;
}CUBE;


typedef struct SCREEN_POINTS
{
        int x,y;
} SCREEN_POINTS;


CUBE points[7];
SCREEN_POINTS screenp[7];

void init()

{

points[0].x =  32;
points[0].y =  32;
points[0].z =  12;

points[1].x =  32;
points[1].y =  17;
points[1].z =  12;

points[2].x =  62;
points[2].y =  17;
points[2].z =  12;

points[3].x =  62;
points[3].y =  32;
points[3].z =  12;

points[4].x =  42;
points[4].y =  42;
points[4].z =  32;

points[5].x =  42;
points[5].y =  27;
points[5].z =  32;

points[6].x =  72;
points[6].y =  27;
points[6].z =  32;

points[7].x =  72;
points[7].y =  42;
points[7].z =  32;

}


void project_points()
{
int c;

   init();

   for(c=0; c<=NUM_VERTS; c++)
      {

       //if(!points[c].z)
         //       points[c].z=1;
         screenp[c].x=FOCAL_DISTANCE * points[c].x /
(points[c].z+Xorigin);

         screenp[c].y=FOCAL_DISTANCE * points[c].y /
(points[c].z+Yorigin);


      }

}

void draw(BITMAP *buff)
{
        int j;

         project_points();


          for(j=0; j<=NUM_VERTS; j++) //all this stuff does is connect
the
                                      //dots.
            {

             if(j=NUM_SQR)
                {
                    line(buff, screenp[NUM_SQR].x, screenp[NUM_SQR].y,
                               screenp[0].x,screenp[0].y,15);
                }

            if(j<NUM_SQR)
                {
                    line(buff, screenp[j].x, screenp[j].y,
screenp[j+1].x, screenp[j+1].y,15);
                    line(buff, screenp[j].x, screenp[j].y,
screenp[j+4].x, screenp[j+4].y,15);
                }

              if(j>NUM_SQR)
                {

                     if(j!=NUM_VERTS) line(buff, screenp[j].x,
screenp[j].y, screenp[j+1].x, screenp[j+1].y,15);
                     if(j=NUM_VERTS) line(buff, screenp[j].x,
screenp[j].y, screenp[4].x, screenp[4].y,15);
                }
     }

}

BITMAP *bmp;
int f;

main()
{

   allegro_init();
   install_keyboard();
   set_gfx_mode(GFX_AUTODETECT,SCREENW,SCREENH,0,0);
   set_pallete(desktop_pallete);
   bmp = create_bitmap(SCREENW,SCREENH);

   f=2;


   do
   {

    clear(bmp);
    draw(bmp);
    blit(bmp,screen,0,0,0,0,SCREENW,SCREENH);
    if (key[KEY_Y]) f=3;
    }
    while(f==2); //run forever

    allegro_exit();

    }





- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019