IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Visual C++ Discussion :

[3D] afficher polygones


Sujet :

Visual C++

  1. #1
    Membre éprouvé
    Homme Profil pro
    Développeur
    Inscrit en
    Août 2003
    Messages
    1 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Développeur

    Informations forums :
    Inscription : Août 2003
    Messages : 1 493
    Par défaut [3D] afficher polygones
    Bonjour, j'utilise MS Visual C++ 2005 et je dois afficher une scene 3D comprenant une maison (2 polygones : pavé & toit)
    Mes structures sont définies mais je ne sait pas comment & quelles sont les fonctions pour afficher un polygone ?
    Dans mon cours on me dit d'appeller aussi une fonction permettant de dessiner des triangles (possibiliter d'implémenter).
    void scanCon2DTr(HDC hdc, int x1, int y1, int x2, int y2, int x3, int y3, COLORREF c);

    DataStruct.h :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    #ifndef	_3D_DATASTRUCT_H
    #define _3D_DATASTRUCT_H
     
      #include "windows.h"
     
     
    /*	Colour */
    	typedef struct structTagRGBColour
    		{
    		int	red, green, blue;		/* red, green, blue components*/
    		}
    			RGBColour,				/* RGB colour */
    			* pRGBColour;			/* pointer to RGB colour */
     
    /*	2D vertex */
    	typedef	  struct	tagVertex2D
    				{
                   double x, y;	/* x and y coordinates of a vertex */
                   }	Vertex2D,                  		/* 2D vertex */
                   		* pVertex2D;                  	/* pointer to 2D vertex */
     
    /*	3D vertex */
    	typedef	  struct	tagVertex
    					{
                   double x, y, z;	/* x, y, and z coordinates of a vertex */
                   }	Vertex3D,                  		/* 3D vertex */
                   		* pVertex3D;                  	/* pointer to 3D vertex */
     
     
    /*	3D polygon */
    	typedef struct structTag3DPolygon
    		{
    		int			f3SidedPoly[3];      /* each integer is index of vertex in array of vertices for object */
     
    		int			visible;			/* polygon visibility flag: 1 -> visible, 0 ->invisible */
     
    		}
    			f3DPolygon,					/* 3D polygon */
    			* p3DPolygon,				/* pointer to 3D polygon */
    			f3DFacet,					/* 3D facet */
    			* p3DFacet;					/* pointer to 3D facet */
     
    /*	3D object */
    	typedef struct structTag3DObject
    		{
    		p3DFacet			pFacetArray;			/* array of facets */
    		int					numFacets;				/* number of object facets */ 
     
    		pVertex3D			pVertexArray;			/* array of vertex coordinates */
    		int					numVertices;			/* count of object vertices */
     
    													/* modelling transformation parameters */
    		Vertex3D			worldPosition;			/* object position */
    		double				xScale, yScale, zScale; /* object's x, y, and z scale factors */
    		double				rotXAngle, rotYAngle, rotZAngle;	/* object's x, y, and z angle (degrees) of rotation */
     
    		RGBColour				colour;				/* object colour */
     
    		int					visible;				/* object visibility flag: 1 -> visible, 0 ->invisible */
    		}
    			f3DObject,				/* 3D Object */
    			* p3DObject;			/* pointer to 3D object */
     
     
     
    /*	Object group */
    	typedef struct structTag3DObjectGroup
    		{
    		p3DObject	pObjectArray;					/* array of objects in group */
    		int			numObjs;						/* count of objects in group */
     
    		}
    			f3DObjectGroup,					/* group of 3D object */
    			* p3DObjectGroup;				/* pointer to group of 3D object */
     
     
    /*	Scene */
    	typedef struct structTag3DScene
    		{
    		p3DObjectGroup	pObjGroupArray;				/* array of grouped scene objects */
    		int				numObjGroups;				/* count of grouped scene objects */
     
    		}
    			f3DScene,				/* 3D Scene */
    			* p3DScene;				/* pointer to 3D scene */
     
     
    /* Function prototypes */
    void loadHouseData(p3DScene scene);
    void loadWallsData(p3DObject pObj);
    void loadRoofData(p3DObject pObj);
    p3DObjectGroup allocateObjGroupArray(int numObjGroups);
    p3DObject allocateObjectArray(int numObjects);
    p3DFacet allocateFacetArray(int numFacets);
    Vertex3D * allocateVertexArray(int numVertices);
     
     
    #endif /* _3D_DATASTRUCT_H */
    DataStruct.c :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    /*  This function loads a specific scene (house) */
    void loadHouseData(p3DScene pWorld ) {
    	pWorld->numObjGroups = 2;
    	if ( (pWorld->pObjGroupArray = allocateObjGroupArray(pWorld->numObjGroups)) == NULL ) {
    		return;		/* improve */
    	}
    	else {
    		pWorld->pObjGroupArray[0].numObjs = 1;	/* each object group has one object */
    		pWorld->pObjGroupArray[1].numObjs = 1;
     
    		if ( (pWorld->pObjGroupArray[0].pObjectArray = allocateObjectArray(pWorld->pObjGroupArray[0].numObjs)) == NULL )
    			{
    			return;		/* improve */
    			}
    		else if ( (pWorld->pObjGroupArray[1].pObjectArray = allocateObjectArray(pWorld->pObjGroupArray[1].numObjs)) == NULL )
    			{
    			return;		/* improve */
    			}
     
    		loadWallsData(&(pWorld->pObjGroupArray[0].pObjectArray[0]));	/* load walls data */
    		loadRoofData(&(pWorld->pObjGroupArray[1].pObjectArray[0]));		/* load roof data */
    		}
     
    	return;
    }
     
    /*  This function loads wall data for a house */
     
    void loadWallsData(p3DObject pWalls) {
    	pWalls->numFacets = 8;
    	if ( (pWalls->pFacetArray = allocateFacetArray(pWalls->numFacets)) == NULL ) {
    		return;		/* improve */
    	}
    	else {
    		pWalls->pFacetArray[0].f3SidedPoly[0] = 0;
    		pWalls->pFacetArray[0].f3SidedPoly[1] = 1;
    		pWalls->pFacetArray[0].f3SidedPoly[2] = 2;
    		pWalls->pFacetArray[0].visible = 1;
     
    		pWalls->pFacetArray[1].f3SidedPoly[0] = 0;
    		pWalls->pFacetArray[1].f3SidedPoly[1] = 2;
    		pWalls->pFacetArray[1].f3SidedPoly[2] = 3;
    		pWalls->pFacetArray[1].visible = 1;
     
    		pWalls->pFacetArray[2].f3SidedPoly[0] = 3;
    		pWalls->pFacetArray[2].f3SidedPoly[1] = 2;
    		pWalls->pFacetArray[2].f3SidedPoly[2] = 5;
    		pWalls->pFacetArray[2].visible = 1;
     
    		pWalls->pFacetArray[3].f3SidedPoly[0] = 3;
    		pWalls->pFacetArray[3].f3SidedPoly[1] = 5;
    		pWalls->pFacetArray[3].f3SidedPoly[2] = 4;
    		pWalls->pFacetArray[3].visible = 1;
     
    		pWalls->pFacetArray[4].f3SidedPoly[0] = 4;
    		pWalls->pFacetArray[4].f3SidedPoly[1] = 5;
    		pWalls->pFacetArray[4].f3SidedPoly[2] = 6;
    		pWalls->pFacetArray[4].visible = 1;
     
    		pWalls->pFacetArray[5].f3SidedPoly[0] = 4;
    		pWalls->pFacetArray[5].f3SidedPoly[1] = 6;
    		pWalls->pFacetArray[5].f3SidedPoly[2] = 7;
    		pWalls->pFacetArray[5].visible = 1;
     
    		pWalls->pFacetArray[6].f3SidedPoly[0] = 7;
    		pWalls->pFacetArray[6].f3SidedPoly[1] = 1;
    		pWalls->pFacetArray[6].f3SidedPoly[2] = 0;
    		pWalls->pFacetArray[6].visible = 1;
     
    		pWalls->pFacetArray[7].f3SidedPoly[0] = 7;
    		pWalls->pFacetArray[7].f3SidedPoly[1] = 6;
    		pWalls->pFacetArray[7].f3SidedPoly[2] = 1;
    		pWalls->pFacetArray[7].visible = 1;
     
    		}
    	pWalls->numVertices = 8;
     
    	if ( (pWalls->pVertexArray = allocateVertexArray(pWalls->numVertices)) == NULL ) {
    		return;		/* improve */
    	}
    	else {
    		pWalls->pVertexArray[0].x = 0; pWalls->pVertexArray[0].y = 1; pWalls->pVertexArray[0].z = 1;  
    		pWalls->pVertexArray[1].x = 0; pWalls->pVertexArray[1].y = 0; pWalls->pVertexArray[1].z = 1;  
    		pWalls->pVertexArray[2].x = 1; pWalls->pVertexArray[2].y = 0; pWalls->pVertexArray[2].z = 1;  
    		pWalls->pVertexArray[3].x = 1; pWalls->pVertexArray[3].y = 1; pWalls->pVertexArray[3].z = 1;  
    		pWalls->pVertexArray[4].x = 1; pWalls->pVertexArray[4].y = 1; pWalls->pVertexArray[4].z = 0;  
    		pWalls->pVertexArray[5].x = 1; pWalls->pVertexArray[5].y = 0; pWalls->pVertexArray[5].z = 0;  
    		pWalls->pVertexArray[6].x = 0; pWalls->pVertexArray[6].y = 0; pWalls->pVertexArray[6].z = 0;  
    		pWalls->pVertexArray[7].x = 0; pWalls->pVertexArray[7].y = 1; pWalls->pVertexArray[7].z = 0;  
    	}
    	pWalls->worldPosition.x = 10, pWalls->worldPosition.y = 0, pWalls->worldPosition.z = 0;
    	pWalls->xScale = pWalls->yScale = pWalls->zScale = 100; 
    	pWalls->rotXAngle = pWalls->rotYAngle = pWalls->rotZAngle = 0;
     
    	pWalls->colour.red = 128, pWalls->colour.green = 128, pWalls->colour.blue = 128;
     
    	pWalls->visible = 1;
     
    	return;
    }
     
    /*  This function loads roof data for a house */
     
    void loadRoofData(p3DObject pRoof) {
    	pRoof->numFacets = 6;
    	if ( (pRoof->pFacetArray = allocateFacetArray(pRoof->numFacets)) == NULL ) {
    		return;		/* improve */
    	}
    	else {
    		pRoof->pFacetArray[0].f3SidedPoly[0] = 0;
    		pRoof->pFacetArray[0].f3SidedPoly[1] = 1;
    		pRoof->pFacetArray[0].f3SidedPoly[2] = 2;
    		pRoof->pFacetArray[0].visible = 1;
     
    		pRoof->pFacetArray[1].f3SidedPoly[0] = 0;
    		pRoof->pFacetArray[1].f3SidedPoly[1] = 2;
    		pRoof->pFacetArray[1].f3SidedPoly[2] = 3;
    		pRoof->pFacetArray[1].visible = 1;
     
    		pRoof->pFacetArray[2].f3SidedPoly[0] = 3;
    		pRoof->pFacetArray[2].f3SidedPoly[1] = 2;
    		pRoof->pFacetArray[2].f3SidedPoly[2] = 5;
    		pRoof->pFacetArray[2].visible = 1;
     
    		pRoof->pFacetArray[3].f3SidedPoly[0] = 3;
    		pRoof->pFacetArray[3].f3SidedPoly[1] = 5;
    		pRoof->pFacetArray[3].f3SidedPoly[2] = 4;
    		pRoof->pFacetArray[3].visible = 1;
     
    		pRoof->pFacetArray[4].f3SidedPoly[0] = 0;
    		pRoof->pFacetArray[4].f3SidedPoly[1] = 3;
    		pRoof->pFacetArray[4].f3SidedPoly[2] = 1;
    		pRoof->pFacetArray[4].visible = 1;
     
    		pRoof->pFacetArray[5].f3SidedPoly[0] = 1;
    		pRoof->pFacetArray[5].f3SidedPoly[1] = 3;
    		pRoof->pFacetArray[5].f3SidedPoly[2] = 4;
    		pRoof->pFacetArray[5].visible = 1;
    		}
     
    	pRoof->numVertices = 6;
     
    	if ( (pRoof->pVertexArray = allocateVertexArray(pRoof->numVertices)) == NULL ) {
    		return;		/* improve */
    	}
    	else {
    		pRoof->pVertexArray[0].x = 0.5; pRoof->pVertexArray[0].y = 1; pRoof->pVertexArray[0].z = 1;  
    		pRoof->pVertexArray[1].x = 0; pRoof->pVertexArray[1].y = 0; pRoof->pVertexArray[1].z = 1;  
    		pRoof->pVertexArray[2].x = 1; pRoof->pVertexArray[2].y = 0; pRoof->pVertexArray[2].z = 1;  
    		pRoof->pVertexArray[3].x = 0.5; pRoof->pVertexArray[3].y = 1; pRoof->pVertexArray[3].z = 0;  
    		pRoof->pVertexArray[4].x = 0; pRoof->pVertexArray[4].y = 0; pRoof->pVertexArray[4].z = 0;  
    		pRoof->pVertexArray[5].x = 1; pRoof->pVertexArray[5].y = 0; pRoof->pVertexArray[5].z = 0;  
    	}
     
     
    	pRoof->worldPosition.x = 10, pRoof->worldPosition.y = 100, pRoof->worldPosition.z = 0;
    	pRoof->xScale = pRoof->zScale = 100; 
    	pRoof->yScale = 50; 
    	pRoof->rotXAngle = pRoof->rotYAngle = pRoof->rotZAngle = 0;
     
    	pRoof->colour.red = 128, pRoof->colour.green = 0, pRoof->colour.blue = 0;
     
    	pRoof->visible = 1;
     
    	return;
    }
    PS : je débute en 3D

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 66
    Par défaut
    j'ai fait un tout petit peu de 3D, et comme personne n'a donné de réponse, je pense que ce que je vais te dire est bon à prendre, surtout si tu es débutant

    Afficher un polygone en 3D consiste en fait à convertir ses coordonnées 3D en celles de ton écran en 2D. On réalise une projection pour ça. Il faut que tu retrouves la formule de projection quelque part (moi j'ai oublié, mais ça se retrouve sûrement vite en faisant un schéma et avec l'aide de son ami google).

    A l'époque ou j'ai programmé, la plupart des modèles 3D étaient constitués de triangles. Donc peut être que c'est le cas pour ta maison, et c'est pour ça qu'on te parle d'implémenter ou de trouver une routine affichant un triangle (ça c'est facile normalement). Il y a une bonne raison au fait que ce soit des triangles, mais là encore, je ne pourrai te dire.

    Désolé pour le peu d'info, mais j'espère que ça te donnera des pistes.

  3. #3
    Membre éprouvé
    Homme Profil pro
    Développeur
    Inscrit en
    Août 2003
    Messages
    1 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Développeur

    Informations forums :
    Inscription : Août 2003
    Messages : 1 493
    Par défaut
    y-a-t-il un algorithme pour convertir une figure de plus de 3 cotés en un triangle ??
    Si oui, pouvez-vous me le donner ou dme dire ou je peux le trouver.

    Merci d'avance

  4. #4
    Membre éprouvé
    Homme Profil pro
    Développeur
    Inscrit en
    Août 2003
    Messages
    1 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Développeur

    Informations forums :
    Inscription : Août 2003
    Messages : 1 493
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    void drawPoly(p3DObject o, HDC hdc) {
    	int i;
    	double x1, y1, z1, x2, y2, z2, x3, y3, z3;
    	for(i=0; i<o->numVertices && o->visible==1; i++) {
    		x1=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[0]].x;
    		y1=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[1]].y;
    		z1=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[2]].z;
     
    		x2=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[0]].x;
    		y2=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[1]].y;
    		z2=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[2]].z;
     
    		x3=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[0]].x;
    		y3=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[1]].y;
    		z3=o->pVertexArray[o->pFacetArray[i].f3SidedPoly[3]].z;
     
    		if(o->pFacetArray[i].visible==1) scanCon2DTr(hdc,
    			(int) x1*o->xScale,
    			(int) y1*o->yScale,
    			(int) x2*o->xScale,
    			(int) y2*o->yScale,
    			(int) x3*o->xScale,
    			(int) y3*o->yScale,
    			RGB(o->colour.red, o->colour.green, o->colour.blue));
    	}
    	return;
    }
    J'arrive maintenant a avoir les triangles mais pas a les afficher (ou ils sont trop petit pour etre vu.
    Quelle opération dois-je mettre pour obtenir le tout a l'échelle voulue ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    scanCon2DTr(hdc,
    			(int) x1*o->xScale,
    			(int) y1*o->yScale,
    			(int) x2*o->xScale,
    			(int) y2*o->yScale,
    			(int) x3*o->xScale,
    			(int) y3*o->yScale,
    			RGB(o->colour.red, o->colour.green, o->colour.blue));

Discussions similaires

  1. Réponses: 2
    Dernier message: 03/12/2013, 09h39
  2. [Google Maps] Afficher marqueurs présents dans un polygone
    Par DevLancelot dans le forum APIs Google
    Réponses: 4
    Dernier message: 07/08/2011, 10h56
  3. Afficher une image dans un polygone
    Par Elwe31 dans le forum VB.NET
    Réponses: 3
    Dernier message: 09/10/2008, 09h24
  4. Impossible d'afficher un gros polygone texturé au CPU (c++)
    Par Dams333 dans le forum API graphiques
    Réponses: 16
    Dernier message: 19/09/2008, 22h19
  5. Quelqu'un a une source pour afficher un polygone?
    Par supergrey dans le forum Flash
    Réponses: 3
    Dernier message: 16/07/2007, 08h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo