www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/09/18:40:50

Message-Id: <199609092216.AAA29084@obelix.tele.net>
From: "Remo Fleckinger" <remo AT remo DOT vol DOT at>
To: "DJGPP" <djgpp AT delorie DOT com>
Subject: Hi Merlin!
Date: Tue, 10 Sep 1996 00:16:37 +0200
MIME-Version: 1.0

This is a multi-part message in MIME format.

------=_NextPart_000_01BB9EAD.54F9ABC0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Here's again Remo.

I thank you very much for your help. I've got the Allegro library now and I
used it in my programm. I think it works now very well. I included the
source code in this E-Mail and may, if you have some time and you're
interrested in it, you will have a look at it. The funktion which
initializes the graphics is called
"Init_Grafik ()" and the funktion which does the page flipping thing is
called
"Wechsle_Seite ()". (This is German and means something like change page)

May you can tell me if this is page flipping like you told me?
You can put it in your sources collection if you like it.
By the way... Do you have an own E-Mail address?

Hm... enough questions so far.
Thanx,
<RF>

P.S.: My E-Mail address: remo AT remo DOT vol DOT at

------=_NextPart_000_01BB9EAD.54F9ABC0
Content-Type: application/octet-stream; name="3dgraf.c"
Content-Transfer-Encoding: quoted-printable
Content-Description: 3dgraf.c (C Datei)
Content-Disposition: attachment; filename="3dgraf.c"

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include <allegro.h>

#include "3dgraf.h"


/* Globale Variablen */
/* ----------------- */

int mode;
BITMAP *buffer1;
BITMAP *buffer2;
BITMAP *aktuell;


/* Definition der Objekte */
/* ---------------------- */

struct Punkt Punkt[8] =3D {{-40, -40, -40},
                         { 40, -40, -40},
                         { 40,  40, -40},
                         {-40,  40, -40},
                         {-40, -40,  40},
                         { 40, -40,  40},
                         { 40,  40,  40},
                         {-40,  40,  40}};

struct Punkt_2D Punkt2D[8];
struct Punkt_2D Punkt2D2[8];

struct Linie Linie[12] =3D {{0, 1}, {1, 2},
                          {2, 3}, {3, 0},
                          {4, 5}, {5, 6},
                          {6, 7}, {7, 4},
                          {0, 4}, {1, 5},
                          {2, 6}, {3, 7}};

struct Objekt Wuerfel =3D
{
  "Wuerfel",       /* Name des Objekts               */
  8, Punkt,        /* Anz. Punkte, zeiger auf daten  */
  Punkt2D,         /* Feld f=81r Bildschirmkoordinaten */
  12, Linie,       /* Anz. Linien, zeiger auf daten  */
  0, NULL,         /* Anz. Fl=84chen, zeiger auf daten */
  100, 100, 100,   /* Skalierung in prozent          */
  180, 180, 180,   /* Translation x, y, z (punkte)   */
  0, 0, 0,         /* Rotation x, y, z (grad)        */
  0                /* Matrix des W=81rfels             */
};

struct Objekt Wuerfel2 =3D
{
  "Wuerfel",       /* Name des Objekts               */
  8, Punkt,        /* Anz. Punkte, zeiger auf daten  */
  Punkt2D2,        /* Feld f=81r Bildschirmkoordinaten */
  12, Linie,       /* Anz. Linien, zeiger auf daten  */
  0, NULL,         /* Anz. Fl=84chen, zeiger auf daten */
  100, 100, 100,   /* Skalierung in prozent          */
  180, 180, 180,   /* Translation x, y, z (punkte)   */
  0, 0, 0,         /* Rotation x, y, z (grad)        */
  0                /* Matrix des W=81rfels             */
};

struct Objekt *AlleObj[2] =3D {&Wuerfel, &Wuerfel2};

struct Welt Welt =3D
{
  2,               /* Anzahl Objekte          */
  AlleObj,         /* Zeiger auf Objekte      */
  {180, 180, -500}  /* Fluchtpunkt des Systems */
};


/* Die Hauptroutine */
/* ---------------- */

void main (void)
{
  int zentrumx =3D 180;
  int zentrumz =3D 180;
  int winkel1 =3D 270;
  int winkel2 =3D 90;
  int addwink =3D 1;
  int radius  =3D 150;
  float radians =3D 0;
  int i       =3D 0;
  int addi    =3D 1;

  clrscr ();
  Init_Grafik ();

  while (!kbhit ())
	{
    i +=3D addi;
    if (i > 360) i -=3D 360;

    radians =3D ((float)winkel1 / 180) * PI;
    winkel1 +=3D addwink;
    Wuerfel.Rotation.x =3D i;
    Wuerfel.Rotation.y =3D i;
    Wuerfel.Translation.z =3D (int) (zentrumz + (cos (radians) * =
radius));
    Wuerfel.Translation.x =3D (int) (zentrumx + (sin (radians) * =
radius));
    Bearbeite_Objekt (&Welt, &Wuerfel);

    radians =3D ((float)winkel2 / 180) * PI;
    winkel2 +=3D addwink;
    Wuerfel2.Rotation.x =3D 360 - i;
    Wuerfel2.Rotation.y =3D 360 - i;
    Wuerfel2.Translation.z =3D (int) (zentrumz + (cos (radians) * =
radius));
    Wuerfel2.Translation.x =3D (int) (zentrumx + (sin (radians) * =
radius));
    Bearbeite_Objekt (&Welt, &Wuerfel2);

    Wechsle_Seite ();
		Zeichne_Welt (&Welt);
	}

  aktuell =3D screen;
  clear (screen);
  destroy_bitmap (buffer2);
  destroy_bitmap (buffer1);

	getch ();
}


/*** Die Routinen ***/
/*** ------------ ***/


/* Ein Objekt auf den Bildschirm bringen */
/* ------------------------------------- */

void Schreibe_Objekt (struct Objekt *Objekt)
{
	int i, ii, anz;

	printf ("Name des Objektes: %s\n", Objekt->Name);

	anz =3D Objekt->Anz_Punkte;  /* Punkte ausgeben */
	printf ("Anzahl der Punkte: %3d\n", anz);
	if (anz > 0)
	{
		for (i=3D0; i<anz; i++)
		{
			printf ("%3d, %3d, %3d\n", Objekt->Punkte[i].x, Objekt->Punkte[i].y, =
Objekt->Punkte[i].z);
		}
	}

	anz =3D Objekt->Anz_Linien;  /* Linien ausgeben */
	printf ("Anzahl der Linien: %3d\n", anz);
	if (anz > 0)
	{
		for (i=3D0; i<anz; i++)
		{
			printf ("%2d->%2d  ", Objekt->Linien[i].Start, =
Objekt->Linien[i].Ende);
			if ((((i+1) % 3) =3D=3D 0) && (i !=3D 0)) printf ("\n");
		}
		if ((i % 3) !=3D 0) printf ("\n");
	}

	anz =3D Objekt->Anz_Flaechen;  /* Fl=84chen ausgeben */
	printf ("Anzahl der Fl=84chen: %d\n", anz);
	if (anz > 0)
	{
		for (i=3D0; i<anz; i++)
		{
			for (ii=3D0; ii<Objekt->Flaechen[i].Anz_Punkte; ii++)
			{
				printf ("%d, ", Objekt->Flaechen[i].Feld[ii]);
			}
			printf ("\n");
		}
	}

	printf ("Skalierung: %d, %d, %d\n", Objekt->Skalierung.x, =
Objekt->Skalierung.y, Objekt->Skalierung.z);

	printf ("Translation: %d, %d, %d\n", Objekt->Translation.x, =
Objekt->Translation.y, Objekt->Translation.z);

	printf ("Rotation: %d, %d, %d\n", Objekt->Rotation.x, =
Objekt->Rotation.y, Objekt->Rotation.z);
}


/* Eine Matrix auf den Bildschirm bringen */
/* -------------------------------------- */

void Schreibe_Matrix (float Matrix[4][4])
{
	int x, y;

	for (y=3D0; y<4; y++)
	{
		for (x=3D0; x<4; x++) printf ("%3d ", (int) Matrix[x][y]);
		printf ("\n");
	}
}


/* Einer Matrix die Einheitsmatrix zuweisen */
/* ---------------------------------------- */

void Matrix_leeren (float Matrix[4][4])
{
	int x, y;

	for (y=3D0; y<4; y++) for (x=3D0; x<4; x++) Matrix[x][y] =3D =
Einheitsmatrix[x][y];
}


/* Eine Matrix mit Nullen f=81llen */
/* ----------------------------- */

void Matrix_nullen (float Matrix[4][4])
{
	int x, y;

	for (y=3D0; y<4; y++) for (x=3D0; x<4; x++) Matrix[x][y] =3D =
Nullmatrix[x][y];
}


/* Multiplikation zweier Matrizen */
/* ------------------------------ */

void Matrix_X_Matrix (float ziel[4][4], float quelle[4][4])
{
	int x, y, z;
  float zwischen[4][4];

	for (y=3D0; y<4; y++)
	{
		for (x=3D0; x<4; x++)
		{
			zwischen[x][y] =3D 0;
			for (z=3D0; z<4; z++)     zwischen[x][y] +=3D ziel[z][y] * =
quelle[x][z];
		}
	}

  for (x=3D0; x<4; x++) for (y=3D0; y<4; y++) ziel[x][y] =3D =
zwischen[x][y];
}


/* Multiplikation eines Punktes mit einer Matrix */
/* --------------------------------------------- */

void Punkt_X_Matrix (float Punkt[4], float Matrix[4][4])
{
  float zwischen[4];

	zwischen[0] =3D Punkt[0] * Matrix[0][0] + Punkt[1] * Matrix[0][1] + =
Punkt[2] * Matrix[0][2] + Punkt[3] * Matrix[0][3];
	zwischen[1] =3D Punkt[0] * Matrix[1][0] + Punkt[1] * Matrix[1][1] + =
Punkt[2] * Matrix[1][2] + Punkt[3] * Matrix[1][3];
	zwischen[2] =3D Punkt[0] * Matrix[2][0] + Punkt[1] * Matrix[2][1] + =
Punkt[2] * Matrix[2][2] + Punkt[3] * Matrix[2][3];
	zwischen[3] =3D Punkt[0] * Matrix[3][0] + Punkt[1] * Matrix[3][1] + =
Punkt[2] * Matrix[3][2] + Punkt[3] * Matrix[3][3];

  Punkt[0] =3D zwischen[0];
	Punkt[1] =3D zwischen[1];
	Punkt[2] =3D zwischen[2];
  Punkt[3] =3D zwischen[3];
}


/* Rotation eines Objektes */
/* ----------------------- */

void Rotation (struct Objekt *Objekt)
{
	float a, b, c, cosa, cosb, cosc, sina, sinb, sinc, Matrix[4][4];

	a =3D ((float)Objekt->Rotation.x / 180) * PI;  /* Berechnung des =
Radians */
	b =3D ((float)Objekt->Rotation.y / 180) * PI;  /* der Winkel */
	c =3D ((float)Objekt->Rotation.z / 180) * PI;

	sina =3D sin (a); /* Berechnung des Sinus eines jeden Winkels */
	sinb =3D sin (b);
	sinc =3D sin (c);

	cosa =3D cos (a); /* Berechnung des Cosinus eines jeden Winkels */
	cosb =3D cos (b);
	cosc =3D cos (c);

	Matrix[0][0] =3D cosb * cosc;  /* Berechnung der Rotationsmatrix */
	Matrix[1][0] =3D cosb * sinc;  /* f=81r alle Achsen */
	Matrix[2][0] =3D sinb * -1;
	Matrix[3][0] =3D 0;

	Matrix[0][1] =3D sina * sinb * cosc - cosa * sinc;
	Matrix[1][1] =3D sina * sinb * sinc + cosa * cosc;
	Matrix[2][1] =3D sina * cosb;
	Matrix[3][1] =3D 0;

	Matrix[0][2] =3D cosa * sinb * cosc + sina * sinc;
	Matrix[1][2] =3D cosa * sinb * sinc - sina * cosc;
	Matrix[2][2] =3D cosa * cosb;
	Matrix[3][2] =3D 0;

	Matrix[0][3] =3D 0;
	Matrix[1][3] =3D 0;
	Matrix[2][3] =3D 0;
	Matrix[3][3] =3D 1;

	Matrix_X_Matrix (Objekt->Matrix, Matrix);
}


/* Die Skalierung eines Objektes */
/* ----------------------------- */

void Skalierung (struct Objekt *Objekt)
{
	int x, y;
	float Matrix[4][4];

	Matrix_leeren (Matrix);

  Matrix[0][0] =3D (float)Objekt->Skalierung.x / 100;
  Matrix[1][1] =3D (float)Objekt->Skalierung.y / 100;
  Matrix[2][2] =3D (float)Objekt->Skalierung.z / 100;

	Matrix_X_Matrix (Objekt->Matrix, Matrix);
}


/* Die Verschiebung eines Objektes */
/* ------------------------------- */

void Translation (struct Objekt *Objekt)
{
	int x, y;
	float Matrix[4][4];

	Matrix_leeren (Matrix);

	Matrix[0][3] =3D (float)Objekt->Translation.x;
	Matrix[1][3] =3D (float)Objekt->Translation.y;
	Matrix[2][3] =3D (float)Objekt->Translation.z;

	Matrix_X_Matrix (Objekt->Matrix, Matrix);
}


/* Projezieren eines Objektes auf die Ebene */
/* ---------------------------------------- */

void Projektion (struct Welt *Welt, struct Objekt *Objekt)
{
	float Punkt[4], Matrix[4][4];
	int i;

	Matrix_nullen (Matrix);

	Matrix[0][0] =3D Welt->Beob.z * -1;  /* Zuweisung der =
Transformationsmatrix */
	Matrix[1][1] =3D Matrix[0][0];
	Matrix[0][2] =3D Welt->Beob.x;
	Matrix[1][2] =3D Welt->Beob.y;
	Matrix[3][2] =3D 1;
	Matrix[3][3] =3D Matrix[0][0];

	Matrix_X_Matrix (Objekt->Matrix, Matrix);

	for (i=3D0; i<Objekt->Anz_Punkte; i++)
	{
		Punkt[0] =3D Objekt->Punkte[i].x;
		Punkt[1] =3D Objekt->Punkte[i].y;
		Punkt[2] =3D Objekt->Punkte[i].z;
		Punkt[3] =3D 1;

		Punkt_X_Matrix (Punkt, Objekt->Matrix);

		Objekt->Bildschirm[i].x =3D Punkt[0] / Punkt[3];
		Objekt->Bildschirm[i].y =3D Punkt[1] / Punkt[3];
	}
}


/* Alle Transformationen eines Objektes durchf=81hren */
/* ------------------------------------------------ */

void Transformation (struct Welt *Welt, struct Objekt *Objekt)
{
	Skalierung (Objekt);
	Translation (Objekt);
	Rotation (Objekt);
	Projektion (Welt, Objekt);
}


/* Die Grafik intialisieren */
/* ------------------------ */

int Init_Grafik (void)
{
  int graf;

  allegro_init ();

  graf =3D set_gfx_mode (GFX_MODEX, 360, 360, 360, 720);
  if (graf < 0)
  {
    printf ("ALLEGRO SET_GFX_MODE FEHLER: %s", allegro_error);
    exit (TRUE);
  }

  buffer1 =3D create_sub_bitmap (screen, 0, 0, 360, 360);
  buffer2 =3D create_sub_bitmap (screen, 0, 361, 360, 360);

  return (TRUE);
}


/* Ein Objekt bearbeiten */
/* --------------------- */

void Bearbeite_Objekt (struct Welt *Welt, struct Objekt *Objekt)
{
  /* Die Reihenfolge scheint wichtig zu sein! */

  Matrix_leeren (Objekt->Matrix);
  Skalierung (Objekt);
  Rotation (Objekt);
  Translation (Objekt);
  Projektion (Welt, Objekt);
}


/* Eine Welt auf den Bildschirm bringen */
/* ------------------------------------ */

void Zeichne_Welt (struct Welt *Welt)
{
	int i, ii, start, ende;
	struct Objekt *Objekt;

	for (i=3D0; i<Welt->Anz_Objekte; i++)
	{
    Objekt =3D Welt->Objekte[i];
		for (ii=3D0; ii<Objekt->Anz_Linien; ii++)
		{
			start =3D Objekt->Linien[ii].Start;
			ende  =3D Objekt->Linien[ii].Ende;
      line (aktuell, Objekt->Bildschirm[start].x, =
Objekt->Bildschirm[start].y, Objekt->Bildschirm[ende].x, =
Objekt->Bildschirm[ende].y, 1);
		}
	}
}


/* Double Buffering Routine */
/* ------------------------ */

void Wechsle_Seite (void)
{
  if (mode =3D=3D 0)
  {
    scroll_screen (0, 361);
    aktuell =3D buffer1;
  }

  if (mode =3D=3D 1)
  {
    scroll_screen (0, 0);
    aktuell =3D buffer2;
  }

  clear (aktuell);
  mode =3D (mode + 1) % 2;
}

------=_NextPart_000_01BB9EAD.54F9ABC0
Content-Type: application/octet-stream; name="3dgraf.h"
Content-Transfer-Encoding: quoted-printable
Content-Description: 3dgraf.h (H Datei)
Content-Disposition: attachment; filename="3dgraf.h"

/* Definition der Grundstrukturen einer 3D-Grafik */
/* ---------------------------------------------- */

struct Punkt_2D  /* Ein zweidimensionaler Punkt f=81r den Bildschirm */
{
	int x;
	int y;
};

struct Punkt  /* Ein dreidimensionaler Punkt f=81r die Objekte */
{
	int x;
	int y;
	int z;
};

struct Linie  /* Die Verbindungen der Punkte im Objekt */
{
	int Start;
	int Ende;
};

struct Flaeche  /* Die Definition einer Fl=84che eines Objektes */
{
	int Farbe;
	int Anz_Punkte;
	int *Feld;
};

struct Objekt  /* Die Grundstruktur eines Objektes */
{
	char *Name;

	int Anz_Punkte;
	struct Punkt *Punkte;
	struct Punkt_2D *Bildschirm;

	int Anz_Linien;
	struct Linie *Linien;

	int Anz_Flaechen;
	struct Flaeche *Flaechen;

	struct Punkt Skalierung;
	struct Punkt Translation;
	struct Punkt Rotation;
	float Matrix[4][4];
};

struct Welt  /* Die Welt in der sich alle Objekte befinden */
{
	int Anz_Objekte;
  struct Objekt **Objekte;

	struct Punkt Beob;
};


/* Definition einiger wichtiger Matrizen */
/* ------------------------------------- */

float Einheitsmatrix[4][4] =3D {{1, 0, 0, 0},
															{0, 1, 0, 0},
															{0, 0, 1, 0},
															{0, 0, 0, 1}};

float Nullmatrix[4][4] =3D {{0, 0, 0, 0},
													{0, 0, 0, 0},
													{0, 0, 0, 0},
													{0, 0, 0, 0}};


/* Definition der Prozeduren */
/* ------------------------- */

void Matrix_X_Matrix (float ziel[4][4], float quelle[4][4]);
void Punkt_X_Matrix (float Punkt[4], float Matrix[4][4]);
void Matrix_leeren (float Matrix[4][4]);
void Matrix_nullen (float Matrix[4][4]);
void Schreibe_Matrix (float Matrix[4][4]);
void Schreibe_Objekt (struct Objekt *Objekt);
int  Init_Grafik (void);
void Bearbeite_Objekt (struct Welt *Welt, struct Objekt *Objekt);
void Zeichne_Welt (struct Welt *Welt);
void Wechsle_Seite (void);  /* F=81r Page Flipping */

void Transformation (struct Welt *Welt, struct Objekt *Objekt);  /* Alle =
Transformationen durchf=81hren */
void Rotation (struct Objekt *Objekt);  /* Rotation eines Objektes */
void Skalierung (struct Objekt *Objekt);  /* Verzerren eines Objektes */
void Translation (struct Objekt *Objekt);  /* Verschieben eines Objektes =
*/
void Projektion (struct Welt *Welt, struct Objekt *Objekt);  /* =
Projektion eines Objektes auf die Ebene */


/* Programmtechnische Definitionen */
/* ------------------------------- */

#define NL printf ("\n");  /* Neue Zeile auf Bildschirm beginnen */
#define UBYTE unsigned char
#define FALSE 0

------=_NextPart_000_01BB9EAD.54F9ABC0--

- Raw text -


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