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
WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui l'ont sagement suivi. Je profite de ce poste pour fai...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVéTECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVé par ROMELARD Fabrice
Speaker : Guillaume Rochette Cette session est dédiée à fournir le retour sur la mise en place d'un cloud privé (IaaS) par Osiatis pour son compte ou celui de ses clients. Ce projet s'est déroulé sur 4 mois et a permis de faire évoluer...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|