begin process at 2010 02 10 11:29:54
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > CODE SOURCE D'UN MOTEUR TROIS DIMENSIONS - LES BASES

CODE SOURCE D'UN MOTEUR TROIS DIMENSIONS - LES BASES


 Information sur la source

Note :
7 / 10 - par 1 personne
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Graphique Classé sous :3d, dimension, graphique, moteur Niveau :Débutant Date de création :07/08/2005 Date de mise à jour :11/08/2005 18:20:22 Vu / téléchargé :4 223 / 374

Auteur : LiBe444

Ecrire un message privé
Site perso
Commentaire sur cette source (6)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Cette source n'a d'intérêt que de ses algorithmes et non du rendu, contrairement aux autres sources que j'envoie.
Elle ne présente que les bases en création de moteur trois dimensions.

Je suis comme certains, je débute, je n'ai jamais fait de trois dimensions en dehors de l'utilisation de librairies graphiques "grand public".

Et j'espère aider certains qui souhaitent débuter un grand projet en trois dimensions (comme un jeu de voiture, un jeu de rôle).

Ce moteur n'a que peu de possibilités : créer un espace, un point, une ligne, un rectangle, se déplacer et tourner sur les trois axes.

Source

  • /* Truc est un nom générique donc à remplacer par ce que vous voulez si c'est pour une exploitation plus ambitieuse. */
  • /*******************************/
  • /* Fichier truc.h (à découper)*/
  • /*******************************/
  • #ifndef TRUC_H
  • #define TRUC_H
  • #include <windows.h>
  • #include <math.h>
  • #define NBCHAMPS 7
  • #define TRUC_POINT 0
  • #define TRUC_LIGNE 1
  • #define TRUC_RECTANGLE 2
  • #define TRUC_ABSOLU 0
  • #define TRUC_RELATIF 1
  • #define TRUC_AXE_X 0
  • #define TRUC_AXE_Y 1
  • #define TRUC_AXE_Z 2
  • typedef double TRUCNB ;
  • typedef double TRUCANGLE ;
  • typedef long int TRUCINT ;
  • typedef TRUCNB** TRUCMAT ;
  • typedef struct{
  • TRUCMAT matrice ;
  • TRUCNB posx, posy, posz ;
  • TRUCINT nbvec ;
  • HWND hwnd ;
  • HDC hdc ;
  • RECT rect ;
  • }HTRUC ;
  • typedef struct{
  • TRUCNB x, y, z ;
  • }TRUCPOINT ;
  • void TrucCreerEspace (HWND hwnd, HTRUC* t, RECT rect) ;
  • void TrucGenPoint (HTRUC *t, TRUCNB x, TRUCNB y, TRUCNB z) ;
  • void TrucGenLigne (HTRUC *t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) ;
  • void TrucGenRectangle (HTRUC *t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) ;
  • void TrucGenPolygone (HTRUC* t, TRUCPOINT* lppt, TRUCINT nb) ;
  • void TrucAfficher (HTRUC t) ;
  • void TrucDeplacer (HTRUC *t, TRUCNB x, TRUCNB y, TRUCNB z, TRUCINT mode) ;
  • void TrucTourner (HTRUC* t, TRUCINT axe, TRUCANGLE angle) ;
  • #endif
  • /******************************/
  • /* Fichier truc.c (à découper)*/
  • /******************************/
  • #include "truc.h"
  • /* Formule magique de conversion de coordonnées en trois dimensions pour qu'elles puissent s'afficher sur un écran en deux dimensions */
  • #define FORM(a,b) (t.matrice [a] [b + b / 2 + 2] - (b % 2 == 0 ? t.posx : t.posy)) * (1.0 + (b % 2 == 0 ? (t.rect.right - t.rect.left) : (t.rect.bottom - t.rect.top)) / (t.matrice [a] [(b / 2 + 1) * 3 + 1] - t.posz > 0 ? t.matrice [a] [(b / 2 + 1) * 3 + 1] - t.posz : 1)) + (b % 2 == 0 ? (t.rect.right - t.rect.left) : (t.rect.bottom - t.rect.top)) / 2
  • void TrucCreerEspace (HWND hwnd, HTRUC* t, RECT rect) /* Créer un espace du point de vue structure de données */
  • {
  • t->matrice = (TRUCNB**)malloc (sizeof (TRUCNB*)) ;
  • *(t->matrice) = (TRUCNB*)malloc (sizeof (TRUCNB)) ;
  • t->nbvec = 1 ;
  • t->posx = 0 ;
  • t->posy = 0 ;
  • t->posz = 0 ;
  • t->rect.top = rect.top ;
  • t->rect.bottom = rect.bottom ;
  • t->rect.left = rect.left ;
  • t->rect.right = rect.right ;
  • t->hwnd = hwnd ;
  • t->hdc = GetDC (hwnd) ;
  • }
  • void TrucGenPoint (HTRUC* t, TRUCNB x, TRUCNB y, TRUCNB z) /* Génerer un point dans la matrice */
  • {
  • t->nbvec = t->nbvec + 1 ;
  • t->matrice = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ;
  • t->matrice [t->nbvec - 1] = (TRUCNB*)malloc (NBCHAMPS * sizeof (TRUCNB)) ;
  • t->matrice [t->nbvec - 1] [0] = TRUC_POINT ;
  • t->matrice [t->nbvec - 1] [1] = x ;
  • t->matrice [t->nbvec - 1] [2] = y ;
  • t->matrice [t->nbvec - 1] [3] = z ;
  • }
  • void TrucGenLigne (HTRUC* t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) /* Génerer une ligne dans la matrice */
  • {
  • t->nbvec = t->nbvec + 1 ;
  • t->matrice = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ;
  • t->matrice [t->nbvec - 1] = (TRUCNB*)malloc (NBCHAMPS * sizeof (TRUCNB)) ;
  • t->matrice [t->nbvec - 1] [0] = TRUC_LIGNE ;
  • t->matrice [t->nbvec - 1] [1] = xs ;
  • t->matrice [t->nbvec - 1] [2] = ys ;
  • t->matrice [t->nbvec - 1] [3] = zs ;
  • t->matrice [t->nbvec - 1] [4] = xf ;
  • t->matrice [t->nbvec - 1] [5] = yf ;
  • t->matrice [t->nbvec - 1] [6] = zf ;
  • }
  • void TrucGenRectangle (HTRUC* t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) /* Génerer un rectangle dans la matrice */
  • {
  • t->nbvec = t->nbvec + 1 ;
  • t->matrice = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ;
  • t->matrice [t->nbvec - 1] = (TRUCNB*)malloc (NBCHAMPS * sizeof (TRUCNB)) ;
  • t->matrice [t->nbvec - 1] [0] = TRUC_RECTANGLE ;
  • t->matrice [t->nbvec - 1] [1] = xs ;
  • t->matrice [t->nbvec - 1] [2] = ys ;
  • t->matrice [t->nbvec - 1] [3] = zs ;
  • t->matrice [t->nbvec - 1] [4] = xf ;
  • t->matrice [t->nbvec - 1] [5] = yf ;
  • t->matrice [t->nbvec - 1] [6] = zf ;
  • }
  • void TrucGenPolygone (HTRUC* t, TRUCPOINT* lppt, TRUCINT nb) /* Génerer un polygone dans la matrice */
  • {
  • TRUCINT i;
  • t->nbvec = t->nbvec + 1 ;
  • t->matrice = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ;
  • t->matrice [t->nbvec - 1] = (TRUCNB*)malloc ((3 * nb + 4) * sizeof (TRUCNB)) ;
  • t->matrice [t->nbvec - 1] [0] = TRUC_POLYGONE ;
  • t->matrice [t->nbvec - 1] [1] = 1.0 * nb ;
  • for (i = 0 ; i < nb ; i++)
  • {
  • t->matrice [t->nbvec - 1] [3 * i + 2] = lppt [i].x ;
  • t->matrice [t->nbvec - 1] [3 * i + 3] = lppt [i].y ;
  • t->matrice [t->nbvec - 1] [3 * i + 4] = lppt [i].z ;
  • }
  • }
  • /* La il serait possible de faire une fonction TrucGenBoule. */
  • void TrucAfficher (HTRUC t) /* Afficher tous les objets génerés avec la formule magique (differenciation selon les objets) */
  • {
  • TRUCINT i = 0 ;
  • for (i = 1 ; i < t.nbvec ; i++)
  • {
  • POINT lp [2] ;
  • if (t.matrice [i] [0] == TRUC_POINT && t.matrice [i] [3] - t.posz > 0 && t.matrice [i] [3] - t.posz < 40)
  • {
  • lp [0].x = FORM(i, 0) ;
  • lp [0].y = FORM(i, 1) ;
  • lp [1].x = FORM(i, 0) + 1 ;
  • lp [1].y = FORM(i, 1) + 1 ;
  • Polyline (t.hdc, lp, 2) ;
  • }
  • if (t.matrice [i] [0] == TRUC_LIGNE)
  • {
  • lp [0].x = FORM(i, 0) ;
  • lp [0].y = FORM(i, 1) ;
  • lp [1].x = FORM(i, 2) ;
  • lp [1].y = FORM(i, 3) ;
  • if (t.matrice [i] [3] - t.posz >= 0 || t.matrice [i] [6] - t.posz >= 0) Polyline (t.hdc, lp, 2) ;
  • }
  • if (t.matrice [i] [0] == TRUC_RECTANGLE && t.matrice [i] [6] - t.posz > 0 && t.matrice [i] [3] - t.posz > 0)
  • {
  • POINT lp2 [4] ;
  • lp2 [0].x = FORM(i, 0) ;
  • lp2 [0].y = FORM(i, 1) ;
  • lp2 [1].x = FORM(i, 2) ;
  • lp2 [1].y = lp2 [0].y ;
  • lp2 [2].x = lp2 [1].x ;
  • lp2 [2].y = FORM(i, 3) ;
  • lp2 [3].x = lp2 [0].x ;
  • lp2 [3].y = lp2 [2].y ;
  • Polygon (t.hdc, lp2, 4) ;
  • }
  • if (t.matrice [i] [0] == TRUC_POLYGONE)
  • {
  • TRUCINT j,k;
  • for (j = 0 ; j < t.matrice [i] [1] - 1; j++)
  • {
  • POINT lp2 [2] ;
  • k = 2 * j;
  • lp2 [0].x = FORM(i, k) ; k++ ;
  • lp2 [0].y = FORM(i, k) ; k++ ;
  • lp2 [1].x = FORM(i, k) ; k++ ;
  • lp2 [1].y = FORM(i, k) ; k++ ;
  • if (t.matrice [i] [4 + 3 * j] - t.posz >= 0 || t.matrice [i] [7 + 3 * j] - t.posz >= 0) Polyline (t.hdc, lp2, 2) ;
  • }
  • }
  • }
  • }
  • void TrucDeplacer (HTRUC* t, TRUCNB x, TRUCNB y, TRUCNB z, TRUCINT mode) /* Fonction de déplacement de l'oeil (trivial) */
  • {
  • if (mode == TRUC_ABSOLU)
  • {
  • t->posx = x ;
  • t->posy = y ;
  • t->posz = z ;
  • }else{
  • t->posx += x ;
  • t->posy += y ;
  • t->posz += z ;
  • }
  • InvalidateRect (t->hwnd, &(t->rect), TRUE) ;
  • }
  • void TrucTourner (HTRUC* t, TRUCINT axe, TRUCANGLE angle) /* Fonction de rotation de l'oeil (voir matrices de rotation sur un moteur de recherche) */
  • {
  • TRUCINT i ;
  • for (i = 1 ; i < t->nbvec ; i++)
  • {
  • if (t->matrice [i] [0] == TRUC_POINT)
  • {
  • if (axe == TRUC_AXE_X)
  • {
  • TRUCNB a = t->matrice [i] [2], b = t->matrice [i] [3] ;
  • t->matrice [i] [2] = sin (angle) * (b - t->posz) + cos (angle) * (a - t->posy) + t->posy ;
  • t->matrice [i] [3] = - sin (angle) * (a - t->posy) + cos (angle) * (b - t->posz) + t->posz ;
  • }
  • if (axe == TRUC_AXE_Y)
  • {
  • TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [3] ;
  • t->matrice [i] [1] = - sin (angle) * (b - t->posz) + cos (angle) * (a - t->posx) + t->posx ;
  • t->matrice [i] [3] = sin (angle) * (a - t->posx) + cos (angle) * (b - t->posz) + t->posz ;
  • }
  • if (axe == TRUC_AXE_Z)
  • {
  • TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [2] ;
  • t->matrice [i] [1] = sin (angle) * (b - t->posy) + cos (angle) * (a - t->posx) + t->posx ;
  • t->matrice [i] [2] = - sin (angle) * (a - t->posx) + cos (angle) * (b - t->posy) + t->posy ;
  • }
  • }
  • if (t->matrice [i] [0] == TRUC_LIGNE || t->matrice [i] [0] == TRUC_RECTANGLE)
  • {
  • if (axe == TRUC_AXE_X)
  • {
  • TRUCNB a = t->matrice [i] [2], b = t->matrice [i] [3], c = t->matrice [i] [5], d = t->matrice [i] [6];
  • t->matrice [i] [2] = sin (angle) * (b - t->posz) + cos (angle) * (a - t->posy) + t->posy ;
  • t->matrice [i] [3] = - sin (angle) * (a - t->posy) + cos (angle) * (b - t->posz) + t->posz ;
  • t->matrice [i] [5] = sin (angle) * (d - t->posz) + cos (angle) * (c - t->posy) + t->posy ;
  • t->matrice [i] [6] = - sin (angle) * (c - t->posy) + cos (angle) * (d - t->posz) + t->posz ;
  • }
  • if (axe == TRUC_AXE_Y)
  • {
  • TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [3], c = t->matrice [i] [4], d = t->matrice [i] [6];
  • t->matrice [i] [1] = - sin (angle) * (b - t->posz) + cos (angle) * (a - t->posx) + t->posx ;
  • t->matrice [i] [3] = sin (angle) * (a - t->posx) + cos (angle) * (b - t->posz) + t->posz ;
  • t->matrice [i] [4] = - sin (angle) * (d - t->posz) + cos (angle) * (c - t->posx) + t->posx ;
  • t->matrice [i] [6] = sin (angle) * (c - t->posx) + cos (angle) * (d - t->posz) + t->posz ;
  • }
  • if (axe == TRUC_AXE_Z)
  • {
  • TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [2], c = t->matrice [i] [4], d = t->matrice [i] [5];
  • t->matrice [i] [1] = sin (angle) * (b - t->posy) + cos (angle) * (a - t->posx) + t->posx ;
  • t->matrice [i] [2] = - sin (angle) * (a - t->posx) + cos (angle) * (b - t->posy) + t->posy ;
  • t->matrice [i] [4] = sin (angle) * (d - t->posy) + cos (angle) * (c - t->posx) + t->posx ;
  • t->matrice [i] [5] = - sin (angle) * (c - t->posx) + cos (angle) * (d - t->posy) + t->posy ;
  • }
  • }
  • if (t->matrice [i] [0] == TRUC_POLYGONE)
  • {
  • TRUCINT j;
  • for (j = 0 ; j < t->matrice [i] [1] ; j++)
  • {
  • if (axe == TRUC_AXE_X)
  • {
  • TRUCNB a = t->matrice [i] [3 * j + 3], b = t->matrice [i] [3 * j + 4] ;
  • t->matrice [i] [3 * j + 3] = sin (angle) * (b - t->posz) + cos (angle) * (a - t->posy) + t->posy ;
  • t->matrice [i] [3 * j + 4] = - sin (angle) * (a - t->posy) + cos (angle) * (b - t->posz) + t->posz ;
  • }
  • if (axe == TRUC_AXE_Y)
  • {
  • TRUCNB a = t->matrice [i] [3 * j + 2], b = t->matrice [i] [3 * j + 4] ;
  • t->matrice [i] [3 * j + 2] = - sin (angle) * (b - t->posz) + cos (angle) * (a - t->posx) + t->posx ;
  • t->matrice [i] [3 * j + 4] = sin (angle) * (a - t->posx) + cos (angle) * (b - t->posz) + t->posz ;
  • }
  • if (axe == TRUC_AXE_Z)
  • {
  • TRUCNB a = t->matrice [i] [3 * j + 2], b = t->matrice [i] [3 * j + 3] ;
  • t->matrice [i] [3 * j + 2] = sin (angle) * (b - t->posy) + cos (angle) * (a - t->posx) + t->posx ;
  • t->matrice [i] [3 * j + 3] = - sin (angle) * (a - t->posx) + cos (angle) * (b - t->posy) + t->posy ;
  • }
  • }
  • }
  • }
  • InvalidateRect (t->hwnd, &(t->rect), TRUE) ;
  • }
/* Truc est un nom générique donc à remplacer par ce que vous voulez si c'est pour une exploitation plus ambitieuse. */

/*******************************/
/* Fichier truc.h  (à découper)*/
/*******************************/

#ifndef TRUC_H

#define TRUC_H

#include <windows.h>

#include <math.h>

#define NBCHAMPS       7

#define TRUC_POINT     0

#define TRUC_LIGNE     1

#define TRUC_RECTANGLE 2

#define TRUC_ABSOLU    0

#define TRUC_RELATIF   1

#define TRUC_AXE_X     0

#define TRUC_AXE_Y     1

#define TRUC_AXE_Z     2

typedef double TRUCNB    ; 
typedef double TRUCANGLE ; 
typedef long int TRUCINT ; 

typedef TRUCNB** TRUCMAT ; 
typedef struct{
  TRUCMAT matrice ; 
  TRUCNB posx, posy, posz ; 
  TRUCINT nbvec ; 
  HWND hwnd ; 
  HDC hdc ; 
  RECT rect ; 
}HTRUC ; 

typedef struct{
        TRUCNB x, y, z ; 
}TRUCPOINT ; 

void TrucCreerEspace  (HWND hwnd, HTRUC* t, RECT rect) ; 
void TrucGenPoint     (HTRUC *t, TRUCNB x, TRUCNB y, TRUCNB z) ; 
void TrucGenLigne     (HTRUC *t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) ; 
void TrucGenRectangle (HTRUC *t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) ; 
void TrucGenPolygone  (HTRUC* t, TRUCPOINT* lppt, TRUCINT nb) ; 
void TrucAfficher     (HTRUC t) ; 
void TrucDeplacer     (HTRUC *t, TRUCNB x, TRUCNB y, TRUCNB z, TRUCINT mode) ; 
void TrucTourner      (HTRUC* t, TRUCINT axe, TRUCANGLE angle) ; 

#endif


/******************************/
/* Fichier truc.c (à découper)*/
/******************************/
#include "truc.h"

/* Formule magique de conversion de coordonnées en trois dimensions pour qu'elles puissent s'afficher sur un écran en deux dimensions */

#define FORM(a,b) (t.matrice [a] [b + b / 2 + 2] - (b % 2 == 0 ? t.posx : t.posy)) * (1.0 + (b % 2 == 0 ? (t.rect.right - t.rect.left) : (t.rect.bottom - t.rect.top)) / (t.matrice [a] [(b / 2 + 1) * 3 + 1] - t.posz > 0 ? t.matrice [a] [(b / 2 + 1) * 3 + 1] - t.posz : 1)) + (b % 2 == 0 ? (t.rect.right - t.rect.left) : (t.rect.bottom - t.rect.top)) / 2


void TrucCreerEspace (HWND hwnd, HTRUC* t, RECT rect) /* Créer un espace du point de vue structure de données */
{
  t->matrice     = (TRUCNB**)malloc (sizeof (TRUCNB*)) ; 
  *(t->matrice)  = (TRUCNB*)malloc (sizeof (TRUCNB))   ; 
  t->nbvec       = 1 ; 
  t->posx        = 0 ; 
  t->posy        = 0 ; 
  t->posz        = 0 ; 
  t->rect.top    = rect.top     ; 
  t->rect.bottom = rect.bottom  ; 
  t->rect.left   = rect.left    ; 
  t->rect.right  = rect.right   ; 
  t->hwnd        = hwnd         ; 
  t->hdc         = GetDC (hwnd) ; 
}


void TrucGenPoint (HTRUC* t, TRUCNB x, TRUCNB y, TRUCNB z) /* Génerer un point dans la matrice */ 
{
  t->nbvec                      = t->nbvec + 1 ; 
  t->matrice                    = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ; 
  t->matrice [t->nbvec - 1]     = (TRUCNB*)malloc (NBCHAMPS * sizeof (TRUCNB)) ; 
  t->matrice [t->nbvec - 1] [0] = TRUC_POINT ; 
  t->matrice [t->nbvec - 1] [1] = x ; 
  t->matrice [t->nbvec - 1] [2] = y ; 
  t->matrice [t->nbvec - 1] [3] = z ; 
}


void TrucGenLigne (HTRUC* t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) /* Génerer une ligne dans la matrice */
{
  t->nbvec                      = t->nbvec + 1 ; 
  t->matrice                    = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ; 
  t->matrice [t->nbvec - 1]     = (TRUCNB*)malloc (NBCHAMPS * sizeof (TRUCNB)) ; 
  t->matrice [t->nbvec - 1] [0] = TRUC_LIGNE ; 
  t->matrice [t->nbvec - 1] [1] = xs ; 
  t->matrice [t->nbvec - 1] [2] = ys ; 
  t->matrice [t->nbvec - 1] [3] = zs ; 
  t->matrice [t->nbvec - 1] [4] = xf ; 
  t->matrice [t->nbvec - 1] [5] = yf ; 
  t->matrice [t->nbvec - 1] [6] = zf ; 
}

void TrucGenRectangle (HTRUC* t, TRUCNB xs, TRUCNB ys, TRUCNB zs, TRUCNB xf, TRUCNB yf, TRUCNB zf) /* Génerer un rectangle dans la matrice */
{
  t->nbvec                      = t->nbvec + 1 ; 
  t->matrice                    = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ; 
  t->matrice [t->nbvec - 1]     = (TRUCNB*)malloc (NBCHAMPS * sizeof (TRUCNB)) ;  
  t->matrice [t->nbvec - 1] [0] = TRUC_RECTANGLE ; 
  t->matrice [t->nbvec - 1] [1] = xs ; 
  t->matrice [t->nbvec - 1] [2] = ys ; 
  t->matrice [t->nbvec - 1] [3] = zs ; 
  t->matrice [t->nbvec - 1] [4] = xf ; 
  t->matrice [t->nbvec - 1] [5] = yf ; 
  t->matrice [t->nbvec - 1] [6] = zf ; 
}

void TrucGenPolygone (HTRUC* t, TRUCPOINT* lppt, TRUCINT nb) /* Génerer un polygone dans la matrice */
{
  TRUCINT i;
  t->nbvec                                = t->nbvec + 1 ; 
  t->matrice                              = (TRUCNB**)realloc (t->matrice, t->nbvec * sizeof (TRUCNB*)) ; 
  t->matrice [t->nbvec - 1]               = (TRUCNB*)malloc ((3 * nb + 4) * sizeof (TRUCNB)) ;  
  t->matrice [t->nbvec - 1] [0]           = TRUC_POLYGONE ; 
  t->matrice [t->nbvec - 1] [1]           = 1.0 * nb ; 
  for (i = 0 ; i < nb ; i++)
  {
    t->matrice [t->nbvec - 1] [3 * i + 2] = lppt [i].x ; 
    t->matrice [t->nbvec - 1] [3 * i + 3] = lppt [i].y ; 
    t->matrice [t->nbvec - 1] [3 * i + 4] = lppt [i].z ; 
  }
}

/* La il serait possible de faire une fonction TrucGenBoule. */

void TrucAfficher (HTRUC t) /* Afficher tous les objets génerés avec la formule magique (differenciation selon les objets) */
{
  TRUCINT i = 0 ; 
  for (i = 1 ; i < t.nbvec ; i++)
  {
    POINT lp [2] ; 
    if (t.matrice [i] [0] == TRUC_POINT && t.matrice [i] [3] - t.posz > 0 && t.matrice [i] [3] - t.posz < 40)
    {
      lp [0].x = FORM(i, 0) ; 
      lp [0].y = FORM(i, 1) ; 
      lp [1].x = FORM(i, 0) + 1 ; 
      lp [1].y = FORM(i, 1) + 1 ; 
      Polyline (t.hdc, lp, 2) ; 
    }
    if (t.matrice [i] [0] == TRUC_LIGNE)
    {
      lp [0].x = FORM(i, 0) ; 
      lp [0].y = FORM(i, 1) ; 
      lp [1].x = FORM(i, 2) ; 
      lp [1].y = FORM(i, 3) ; 
      if (t.matrice [i] [3] - t.posz   >=  0 || t.matrice [i] [6] - t.posz >= 0) Polyline (t.hdc, lp, 2) ; 
    }
    if (t.matrice [i] [0] == TRUC_RECTANGLE && t.matrice [i] [6] - t.posz > 0 && t.matrice [i] [3] - t.posz > 0)
    {
      POINT lp2 [4] ; 
      lp2 [0].x = FORM(i, 0) ; 
      lp2 [0].y = FORM(i, 1) ; 
      lp2 [1].x = FORM(i, 2) ; 
      lp2 [1].y = lp2 [0].y ; 
      lp2 [2].x = lp2 [1].x ;
      lp2 [2].y = FORM(i, 3) ; 
      lp2 [3].x = lp2 [0].x ; 
      lp2 [3].y = lp2 [2].y ; 
      Polygon (t.hdc, lp2, 4) ;
    } 
    if (t.matrice [i] [0] == TRUC_POLYGONE)
    {
      TRUCINT j,k;
      for (j = 0 ; j < t.matrice [i] [1] - 1; j++)
      {
	POINT lp2 [2] ;
	k = 2 * j;
	lp2 [0].x = FORM(i, k) ; k++ ; 
	lp2 [0].y = FORM(i, k) ; k++ ; 
	lp2 [1].x = FORM(i, k) ; k++ ; 
	lp2 [1].y = FORM(i, k) ; k++ ; 
	if (t.matrice [i] [4 + 3 * j] - t.posz  >=  0 || t.matrice [i] [7 + 3 * j] - t.posz >= 0) Polyline (t.hdc, lp2, 2) ; 
      }
    }
  }
}

void TrucDeplacer (HTRUC* t, TRUCNB x, TRUCNB y, TRUCNB z, TRUCINT mode) /* Fonction de déplacement de l'oeil (trivial) */
{
  if (mode == TRUC_ABSOLU)
  {
    t->posx = x ; 
    t->posy = y ; 
    t->posz = z ; 
  }else{
    t->posx += x ; 
    t->posy += y ; 
    t->posz += z ; 
  }
  InvalidateRect (t->hwnd, &(t->rect), TRUE) ; 
}


void TrucTourner (HTRUC* t, TRUCINT axe, TRUCANGLE angle) /* Fonction de rotation de l'oeil (voir matrices de rotation sur un moteur de recherche) */
{
  TRUCINT i ; 
  for (i = 1 ; i < t->nbvec ; i++)
  {
    if (t->matrice [i] [0] == TRUC_POINT)
    {
      if (axe == TRUC_AXE_X)
      {
        TRUCNB a = t->matrice [i] [2], b = t->matrice [i] [3] ;
        t->matrice [i] [2] =   sin (angle) * (b - t->posz) + cos (angle) * (a - t->posy) + t->posy ;  
        t->matrice [i] [3] = - sin (angle) * (a - t->posy) + cos (angle) * (b - t->posz) + t->posz ; 
      }
      if (axe == TRUC_AXE_Y)
      {
        TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [3] ;
        t->matrice [i] [1] = - sin (angle) * (b - t->posz) + cos (angle) * (a - t->posx) + t->posx ; 
        t->matrice [i] [3] =   sin (angle) * (a - t->posx) + cos (angle) * (b - t->posz) + t->posz ; 
      }
      if (axe == TRUC_AXE_Z)
      {
        TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [2] ;
        t->matrice [i] [1] =   sin (angle) * (b - t->posy) + cos (angle) * (a - t->posx) + t->posx ;  
        t->matrice [i] [2] = - sin (angle) * (a - t->posx) + cos (angle) * (b - t->posy) + t->posy ; 
      }

    }
    if (t->matrice [i] [0] == TRUC_LIGNE || t->matrice [i] [0] == TRUC_RECTANGLE)
    {
      if (axe == TRUC_AXE_X)
      {
        TRUCNB a = t->matrice [i] [2], b = t->matrice [i] [3], c = t->matrice [i] [5], d = t->matrice [i] [6];
        t->matrice [i] [2] =   sin (angle) * (b - t->posz) + cos (angle) * (a - t->posy) + t->posy ; 
        t->matrice [i] [3] = - sin (angle) * (a - t->posy) + cos (angle) * (b - t->posz) + t->posz ; 
        t->matrice [i] [5] =   sin (angle) * (d - t->posz) + cos (angle) * (c - t->posy) + t->posy ; 
        t->matrice [i] [6] = - sin (angle) * (c - t->posy) + cos (angle) * (d - t->posz) + t->posz ; 
      }
      if (axe == TRUC_AXE_Y)
      {
        TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [3], c = t->matrice [i] [4], d = t->matrice [i] [6];
        t->matrice [i] [1] = - sin (angle) * (b - t->posz) + cos (angle) * (a - t->posx) + t->posx ; 
        t->matrice [i] [3] =   sin (angle) * (a - t->posx) + cos (angle) * (b - t->posz) + t->posz ; 
        t->matrice [i] [4] = - sin (angle) * (d - t->posz) + cos (angle) * (c - t->posx) + t->posx ; 
        t->matrice [i] [6] =   sin (angle) * (c - t->posx) + cos (angle) * (d - t->posz) + t->posz ; 
      }
      if (axe == TRUC_AXE_Z)
      {
        TRUCNB a = t->matrice [i] [1], b = t->matrice [i] [2], c = t->matrice [i] [4], d = t->matrice [i] [5];
        t->matrice [i] [1] =   sin (angle) * (b - t->posy) + cos (angle) * (a - t->posx) + t->posx ; 
        t->matrice [i] [2] = - sin (angle) * (a - t->posx) + cos (angle) * (b - t->posy) + t->posy ; 
        t->matrice [i] [4] =   sin (angle) * (d - t->posy) + cos (angle) * (c - t->posx) + t->posx ; 
        t->matrice [i] [5] = - sin (angle) * (c - t->posx) + cos (angle) * (d - t->posy) + t->posy ; 
      }
    }
    if (t->matrice [i] [0] == TRUC_POLYGONE)
    {
      TRUCINT j;
      for (j = 0 ; j < t->matrice [i] [1] ; j++)
      {
        if (axe == TRUC_AXE_X)
        {
          TRUCNB a = t->matrice [i] [3 * j + 3], b = t->matrice [i] [3 * j + 4] ;
          t->matrice [i] [3 * j + 3] =   sin (angle) * (b - t->posz) + cos (angle) * (a - t->posy) + t->posy ;  
          t->matrice [i] [3 * j + 4] = - sin (angle) * (a - t->posy) + cos (angle) * (b - t->posz) + t->posz ; 
        }
        if (axe == TRUC_AXE_Y)
        {
          TRUCNB a = t->matrice [i] [3 * j + 2], b = t->matrice [i] [3 * j + 4] ;
          t->matrice [i] [3 * j + 2] = - sin (angle) * (b - t->posz) + cos (angle) * (a - t->posx) + t->posx ; 
          t->matrice [i] [3 * j + 4] =   sin (angle) * (a - t->posx) + cos (angle) * (b - t->posz) + t->posz ; 
        }
        if (axe == TRUC_AXE_Z)
        {
          TRUCNB a = t->matrice [i] [3 * j + 2], b = t->matrice [i] [3 * j + 3] ;
          t->matrice [i] [3 * j + 2] =   sin (angle) * (b - t->posy) + cos (angle) * (a - t->posx) + t->posx ;  
          t->matrice [i] [3 * j + 3] = - sin (angle) * (a - t->posx) + cos (angle) * (b - t->posy) + t->posy ; 
        }
      }
    }
  }
  InvalidateRect (t->hwnd, &(t->rect), TRUE) ; 
}


 Conclusion

J'ai besoin d'aide car je bloque sur plusieurs problèmes :
- Comment se programme l'affichage d'une boule ?
- Comment s'écrit l'algorithme d'élimination des faces cachées ?
- (Pourquoi ma fonction de rotation redimensionne-t-elle les objets ?) corrigé avec cette mise à jour

Je n'ai jamais programmé dans ce domaine donc à défaut, la source est classée en débutant.

J'ai inclus un "faire.bat" pour une compilation plus simple, même si la logique aurait voulu que je constitue un "makefile".

Dans l'exemple, voici les assignations des touches :
- Flèches directionnelles : se déplacer selon les axes x et z
- F1 et F2 : se déplacer selon l'axe y
Sur le pavé numérique :
- 8  et 2  : tourner par rapport à l'axe des x
- 9  et 7  : tourner par rapport à l'axe des y
- 4  et 6  : tourner par rapport à l'axe des z

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

07 août 2005 21:36:43 :
Espacement du code source.
07 août 2005 21:37:52 :
Idem.
07 août 2005 21:38:34 :
Idem.
07 août 2005 21:42:54 :
Ajout des assignations clavier dans l'exemple.
07 août 2005 21:44:04 :
Espacement du code source.
07 août 2005 22:00:05 :
Ajout de commentaires.
07 août 2005 22:08:38 :
Correction d'une faute grammaticale.
07 août 2005 22:38:58 :
Suppression de variables et d'instructions inutiles.
09 août 2005 17:22:23 :
Explication de l'erreur durant la rotation : j'ai oublié de considérer qu'il fallait utiliser les anciennes valeurs des points d'extrémités et non les modifiées (A=B C=A*X avant) (maintenant : tmp=A A=B C=tmp*X)
09 août 2005 23:52:58 :
Création de la fonction de génération d'un polygone... J'ai perdu beaucoup de temps sur une erreur bête.
09 août 2005 23:53:26 :
Mise à jour de l'archive compressée.
09 août 2005 23:56:22 :
Mise à jour du code.
10 août 2005 10:48:46 :
Espacement du code
11 août 2005 18:20:22 :
Correction d'erreur

 Sources du même auteur

Source avec Zip Source avec une capture INTERPRÈTEUR DE SCRIPT EN LANGAGE "K"
Source avec Zip Source avec une capture MY PLAYER : VOTRE LECTEUR MP3 [COMPLET]

 Sources de la même categorie

Source avec Zip Source avec une capture VIEWER COMPLET POUR LE TRAITEMENT DE L'IMAGE : IMANALYSE par Pistol_Pete
Source avec Zip DECOUPAGE DE SPRITES par Jackyzgood
Source avec Zip Source avec une capture SIMULATION DE LA DIFFUSION THERMIQUE par ncoder
Source avec Zip Source avec une capture Source .NET (Dotnet) TRANSFORMÉ DE FOURIER RAPIDE EN TRAITEMENT D'IMAGE par reyken
Source avec Zip Source avec une capture 2D GAME DIRECT X 9 par nanonavich

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture XTENGINE - MOTEUR 3D BASÉ SUR OPENGL, GLEW ET LA SDL par XT95
Source avec Zip MOTEUR A PARTICULES par leo666
Source avec Zip Source avec une capture MOTEUR 3D AVEC OPENGL par crazy lapinou
Source avec Zip Source avec une capture JEUX DIRECTX9 COMPLET (JEU D'AVIONS MINIATURES) par supergrey
Source avec Zip Source avec une capture PERSONNAGE ANIME EN 3D par EBArtSoft

Commentaires et avis

Commentaire de JCDjcd le 08/08/2005 11:28:12

si tu as resolu le probleme des faces cachees, alors tu peux faire une sphere en plein de petits triangles, mais le probleme c'est que tout cela est fait en soft, donc ca peut devenir tres long

Dans ta rotation, tu n'aurais pas oublier le "+ t->posz" ??

Commentaire de LiBe le 08/08/2005 12:51:48

Où donc ?

Commentaire de JCDjcd le 08/08/2005 13:08:42

dans TrucTourner()
si C est le centre de rotation alors Y =R.(X-C) + C
avec R la matrice de rotation de centre O, et justement tu oublies la composante en z du " + C"

Commentaire de LiBe le 08/08/2005 13:44:31

Je comprends...mais je pense suivre la forme générale :
a * (t->matrice [i] [j] - t->posA) + b * (t->matrice [i] [k] - t->posB)+ t->posC ;

comment modifier cette formule pour que ce soit ça ?

Commentaire de LiBe le 08/08/2005 13:47:00

Excuse moi en, mais est-ce que tu pourrais par exemple réecrire juste la première formule du TRUC_POINT dans Truc_Tourner() ?

Commentaire de LiBe444 le 09/08/2005 17:25:05

Jcd l'erreur était bien plus bête que ça, et elle ne résidait même pas de surcroît dans la formule.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Mon moteur graphique (2D) va à 10 FPS ? [ par ProGamer ] Je ne comprends pas... Sur mon PC (processeur AMD Athlon XP 1500+), je l'ai testé, à 60 FPS. J'ai ensuite essayé sur mon ancien PC, qui est tout de mê 3D ENGINE [ par xarier ] SALUT VOILA j'ai deux question la premiere c comment realiser un moteur 3d c vous avez des cours c simpa et surtot avec opengl 2eme question est ce qu Comment fonctionne un moteur 2D ? [ par psykocrash ] Bonjour,J'ai pour projet de d&#233;velopper un rpg &#224; la Final Fantasy (en 2D, 4 h&#233;ros, un inventaire pour les techniques et les armes, des c Moteur 3d [ par deguelatore ] Tout d'abord, &#224; quoi sert un moteur 3d. Ensuite, comment en programmer un sous OpenGL?&nbsp;&nbsp;&nbsp;Merci et @+ Moteur 3D. [ par pauledouard ] Voila, bonjour à tous, je suis tout nouveau.Ma question est simple, avez vous une marche à suivre pour construire un moteur 3D en opengl en partant de Création d'un moteur 3D en partant de rien [ par poiuytrez3 ] Bonjour, je souhaiterai cr&#233;er un petit moteur 3D le plus simple possible en partant de rien. Quelqu'un en a t il d&#233;j&#224; fait un? Je ne so Graphique 3D [ par ajusteur ] Bonjour,Je recherche de l'aide sur un Open source "OPEN CASCADE".Je ne connais pas le Visual C++, je ne programme qu'en DELPHI.Est-il possible de r&#2 moteur 3d [ par ben1002 ] Voil&#224;, j'ai blender et directx 9.0, j'ai lu un bon didactitiel sur le langage C++ (http://www.commentcamarche.net/cpp/cppintro.php3) et j'aimerai moteur graphique [ par vladisback ] bonjour je pense depuis quelque temps a essayer de réaliser un moteur graphique (bien que ce ne soit peut etre pas le bon terme) comme opengl et direc


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,967 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales