Accueil > > > CODE SOURCE D'UN MOTEUR TROIS DIMENSIONS - LES BASES
CODE SOURCE D'UN MOTEUR TROIS DIMENSIONS - LES BASES
Information sur la source
Description
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
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
Sources de la même categorie
Commentaires et avis
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évelopper un rpg à la Final Fantasy (en 2D, 4 héros, un inventaire pour les techniques et les armes, des c
Moteur 3d [ par deguelatore ]
Tout d'abord, à quoi sert un moteur 3d. Ensuite, comment en programmer un sous OpenGL? 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éer un petit moteur 3D le plus simple possible en partant de rien. Quelqu'un en a t il déjà 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
moteur 3d [ par ben1002 ]
Voilà, 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
|
Derniers Blogs
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : WIN APIRE : WIN API par racpp
Cliquez pour lire la suite par racpp
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|