Dev - C++ - Como hacer 3D en C++

 
Vista:

Como hacer 3D en C++

Publicado por Héctor Guedea (2 intervenciones) el 14/02/2006 02:23:42
Que tal, primeamente un gran saludo a todos!. Soy Héctor Guedea estoy estudiando Ingenieria en Telemática ( 2 Semestre), Universidad de Colima.

El motivo por el cual les escribo es algo que para nosotros fue dificil de entender puesto que tenemos poca experiencia programando, ya que estamos iniciando la carrera, nuestro maestro de Geometria nos dejo que hagamos por medio de vectores una imagen en 3D en C++, y que esta la podamos rotar a nuestro gusto , asi como lo hace 3D MAX, pero la verdad no sabemos por donde empezar, seria de gran ayuda que me orienten un poco o que me digan, alguna web, etc.

Muchas gracias!.

Atte: Héctor Guedea
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Como hacer 3D en C++

Publicado por Marcos Ortiz (6 intervenciones) el 24/02/2006 16:55:01
Si trabajas con el Borland Builder , le incluyes una libreria que necesita paar trabajar con 3D, no me acuerdo como se llama, pero en el sitio de la Borland está.Buscala ahi:me parace que es ·3Dlib.h, no estoy seguro
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como hacer 3D en C++

Publicado por ricardo (1 intervención) el 18/09/2006 20:21:44
quiero ejemplos de programas de arreglos en tres dimensiones resueltos varios ejemplos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como hacer 3D en C++

Publicado por Jorge Arce (1 intervención) el 07/08/2009 00:51:20
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
//este es un ejeplo de 3d  de un cubo que rota traslada y escala
 
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
typedef struct tmat
 { float v[4][4];
  };
  tmat identidad=
  {{ {1,0,0,0},
     {0,1,0,0},
     {0,0,1,0},
     {0,0,0,1}}
  } ;
  tmat mat=identidad;
 
typedef struct tpunto
{
 float x,y,z;
 int dib;
};
const maxp=12;
tpunto lista[maxp]={	{0,0,0,0},
			{100,0,0,1},
			{100,0,100,1},
			{0,0,100,1},
			{0,0,0,1},
			{0,100,0,1},
			{100,100,0,1},
			{100,0,0,1},
			{100,100,0,0},
			{100,0,100,1},
			{0,100,0,0},
			{0,0,100,1},
			};
void aplicar_mat(tpunto a,tpunto *b)
{
b->x=mat.v[0][0]*a.x+mat.v[0][1]*a.y+mat.v[0][2]*a.z+mat.v[0][3]*1;
b->y=mat.v[1][0]*a.x+mat.v[1][1]*a.y+mat.v[1][2]*a.z+mat.v[1][3]*1;
b->z=mat.v[2][0]*a.x+mat.v[2][1]*a.y+mat.v[2][2]*a.z+mat.v[2][3]*1;
}
void dibujar(void)
{
int i;
float x,y;
tpunto dato;
for(i=0;i<maxp;i++)
	{
	//aplica la matriz de transformacion a los puntos
	aplicar_mat(lista[i],&dato);
	//aplicar proyeccion
	x=dato.x*1000/(1000+dato.z);
	y=dato.y*1000/(1000+dato.z);
	if(lista[i].dib==0)
		moveto(getmaxx()/2+x,getmaxx()/2-y);
	else
		lineto(getmaxx()/2+x,getmaxx()/2-y);
	}
}
void multiplica(tmat a,tmat b,tmat *c)
{
int i,j,k;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
	{
	c->v[i][j]=0;
	for(k=0;k<4;k++)
	    c->v[i][j]+=a.v[i][k]*b.v[k][j];
	}
}
void rotarx(float ang)
{
tmat rot,aux;
float dcos=cos(ang*3.1416/180);
float dsin=sin(ang*3.1416/180);
rot=identidad;
rot.v[1][1]=dcos;
rot.v[1][2]=-dsin;
rot.v[2][1]=dsin;
rot.v[2][2]=dcos;
aux=mat;
multiplica(rot,aux,&mat);
}
void rotary(float ang)
{
tmat rot,aux;
float dcos=cos(ang*3.1416/180);
float dsin=sin(ang*3.1416/180);
rot=identidad;
rot.v[0][0]=dcos;
rot.v[0][2]=dsin;
rot.v[2][0]=-dsin;
rot.v[2][2]=dcos;
aux=mat;
multiplica(rot,aux,&mat);
}
void rotarz(float ang)
{
tmat rot,aux;
float dcos=cos(ang*3.1416/180);
float dsin=sin(ang*3.1416/180);
rot=identidad;
rot.v[0][0]=dcos;
rot.v[0][1]=-dsin;
rot.v[1][0]=dsin;
rot.v[1][1]=dcos;
aux=mat;
multiplica(rot,aux,&mat);
}
void trasladar(float tx, float ty,float tz)
{
   tmat a,b;
   a=identidad;
   a.v[0][3]=tx;
   a.v[1][3]=ty;
   a.v[2][3]=tz;
   b=mat;
   multiplica(a,b,&mat);
}
void escalar(float sx, float sy,float sz)
{
   tmat a,b;
   a=identidad;
   a.v[0][0]=sx;
   a.v[1][1]=sy;
   a.v[2][2]=sz;
   b=mat;
   multiplica(a,b,&mat);
}
 
int main(void)
{
   int gd,gm;
   detectgraph(&gd,&gm);
   initgraph(&gd, &gm, "c:\borlandc\bgi");
   rotarx(30);
   rotary(45);
   rotary(10);
   trasladar(-100,10,-100);
   escalar(2,2,2);
   dibujar();
   getch();
   closegraph();
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como hacer 3D en C

Publicado por sakura (1 intervención) el 19/10/2011 07:35:34
una pregunta al tratar de correr el programa me sale lo sig.
typedef "maxp" is initialized (use_typeof_instead)
me podrias ayudar a solucionar eso x fa
gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como hacer 3D en C

Publicado por gonzalo (1 intervención) el 10/05/2014 07:46:49
me sale lo mismo, pudiste resolverlo?? ayuda porfavor
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Como hacer 3D en C++

Publicado por Blue (1 intervención) el 22/03/2013 07:19:10
En verda funciona, simple y practico para entender, excelente, lo modifike un poko para ejecutarlo en windows :)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Como hacer 3D en C++

Publicado por Adolfo (1 intervención) el 01/10/2018 21:44:10
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//     CUBO 3d en Dev C++ para Windows.
//
//     El ejemplo de Jorge Arce lo he modificado para poder manejar mejor los vértices en 2D.
 
#include <windows.h>
 
#include <math.h>
 
#define IDT_1 1000
 
RECT re;
bool borrar = true;  //Indica si el cubo debe dibujarse en blanco para borrarlo
const int margenx = 280; //Variables para posicionar el cubo.
const int margeny = 200;
 
typedef struct tmat
{
  float v[4][4];
};
 
tmat identidad = {
	{{1,0,0,0},
	{0,1,0,0},
	{0,0,1,0},
	{0,0,0,1}}
} ;
tmat mat = identidad;
 
typedef struct tpunto
{
	float x,y,z;
};
 
const int maxp = 8;
 
tpunto lista[maxp] = {
 
 {-50, 50,-50},
 { 50, 50,-50},
 { 50,-50,-50},
 {-50,-50,-50},
 
 {-50, 50, 50},
 { 50, 50, 50},
 { 50,-50, 50},
 {-50,-50, 50},
 
};
 
typedef struct tvertice2D //Almacenará los puntos de los vértices para un plano en dos dimensiones.
{
	int x, y;
};
tvertice2D vert2D[maxp];
 
void aplicar_mat(tpunto a,tpunto *b)
{
	b->x=mat.v[0][0]*a.x+mat.v[0][1]*a.y+mat.v[0][2]*a.z+mat.v[0][3]*1;
	b->y=mat.v[1][0]*a.x+mat.v[1][1]*a.y+mat.v[1][2]*a.z+mat.v[1][3]*1;
	b->z=mat.v[2][0]*a.x+mat.v[2][1]*a.y+mat.v[2][2]*a.z+mat.v[2][3]*1;
}
 
void dibujar(HDC hdc)
{
	int i;
	float x,y;
	tpunto dato;
	int getmaxx=150;
	int getmaxy=150;
	HPEN hPen;
	if(borrar == true) hPen = CreatePen(PS_SOLID,1,RGB(255,255,255));
	else  hPen = CreatePen(PS_SOLID,1,RGB(0,0,0));
	SelectObject(hdc, hPen);
 
	for(i = 0; i < maxp; i++)
	{
 
		aplicar_mat(lista[i], &dato); //aplica la matriz de transformacion a los puntos 
 
		x = dato.x * 1000 / (1000 + dato.z); //aplicar proyeccion 
		y = dato.y * 1000 / (1000 + dato.z);
 
		vert2D[i].x = margenx + x; //Almacenar los puntos para un plano 2D.
		vert2D[i].y = margeny + y;
	}
 
	//Unir primer vértice (0) con el segundo (1)	
	MoveToEx(hdc, vert2D[0].x, vert2D[0].y, NULL);
	LineTo(hdc, vert2D[1].x, vert2D[1].y);
	//Unir segundo vértice (1) con el tercero (2)	
	LineTo(hdc, vert2D[2].x, vert2D[2].y);
	//Unir tercer vértice (2) con el cuarto (3)	
	LineTo(hdc, vert2D[3].x, vert2D[3].y);
	//Unir cuarto vértice (3) con el primero (1) [Se completa un cuadrado]
	LineTo(hdc, vert2D[0].x, vert2D[0].y);
 
	//Unir quinto vértice (4) con el sexto (5)	
	MoveToEx(hdc, vert2D[4].x, vert2D[4].y, NULL);
	LineTo(hdc, vert2D[5].x, vert2D[5].y);
	//Unir sexto vértice (5) con el séptimo (6)	
	LineTo(hdc, vert2D[6].x, vert2D[6].y);
	//Unir séptimo vértice (6) con el octavo (7)	
	LineTo(hdc, vert2D[7].x, vert2D[7].y);
	//Unir octavo vértice (7) con el quinto (4) [Completo el uadrado de atrás]
	LineTo(hdc, vert2D[4].x, vert2D[4].y);
 
	//Unir primer vértice (0) con el quinto (4) 
	MoveToEx(hdc, vert2D[0].x, vert2D[0].y, NULL);
	LineTo(hdc, vert2D[4].x, vert2D[4].y);
	//Unir segundo vértice (1) con el sexto (5)
	MoveToEx(hdc, vert2D[1].x, vert2D[1].y, NULL);
	LineTo(hdc, vert2D[5].x, vert2D[5].y);
	//Unir tercer vértice (2) con el séptimo (6)
	MoveToEx(hdc, vert2D[2].x, vert2D[2].y, NULL);
	LineTo(hdc, vert2D[6].x, vert2D[6].y);
	//Unir cuarto vértice (3) con el octavo (7)
	MoveToEx(hdc, vert2D[3].x, vert2D[3].y, NULL);
	LineTo(hdc, vert2D[7].x, vert2D[7].y);     // [Líneas que unieron a los dos cuadrados]
 
	DeleteObject(hPen);
	if(borrar==true) borrar=false;
	else borrar = true;
}
 
void multiplica(tmat a, tmat b, tmat *c)
{
	int i,j,k;
	for(i=0;i<4;i++)
	for(j=0;j<4;j++)
	{
		c->v[i][j]=0;
		for(k=0;k<4;k++) c->v[i][j]+=a.v[i][k]*b.v[k][j];
 
	}
}
 
void rotarx(float ang)
{
	tmat rot,aux;
	float dcos=cos(ang*3.1416/180);
	float dsin=sin(ang*3.1416/180);
	rot=identidad;
	rot.v[1][1]=dcos;
	rot.v[1][2]=-dsin;
	rot.v[2][1]=dsin;
	rot.v[2][2]=dcos;
	aux=mat;
	multiplica(rot,aux,&mat);
}
 
void rotary(float ang)
{
	tmat rot,aux;
	float dcos=cos(ang*3.1416/180);
	float dsin=sin(ang*3.1416/180);
	rot=identidad;
	rot.v[0][0]=dcos;
	rot.v[0][2]=dsin;
	rot.v[2][0]=-dsin;
	rot.v[2][2]=dcos;
	aux=mat;
	multiplica(rot,aux,&mat);
}
 
void rotarz(float ang)
{
	tmat rot,aux;
	float dcos=cos(ang*3.1416/180);
	float dsin=sin(ang*3.1416/180);
	rot=identidad;
	rot.v[0][0]=dcos;
	rot.v[0][1]=-dsin;
	rot.v[1][0]=dsin;
	rot.v[1][1]=dcos;
	aux=mat;
	multiplica(rot,aux,&mat);
}
 
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	switch(Message)
	{
		case WM_CREATE:
		{
			SetTimer( hwnd, IDT_1, 25, NULL );
			break;
		}
		case WM_SIZE:{
			GetClientRect(hwnd, &re);
			break;
		}
		case WM_TIMER:{
			switch(wParam){
      			case IDT_1:{
 
      				hdc = GetDC(hwnd);
 
      				dibujar(hdc);  //Dibujar con blanco (borrar el anterior)
 
					rotary(3);
      				rotarx(3);
      				rotarz(3);
 
					dibujar(hdc); //Dibujar con negro 
 
					ReleaseDC(hwnd, hdc);
 
      				break;
				}
      		}
      		break;
		}
		case WM_DESTROY: {
			PostQuitMessage(0);
			KillTimer(hwnd, IDT_1);
			break;
		}
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG msg; /* A temporary location for all messages */
 
	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
 
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
 
	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
 
	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Cubo giratorio 3D",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		600, /* ancho */
		500, /* alto */
		NULL,NULL,hInstance,NULL);
 
	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
 
	/*
		Parte principal de la aplicación, que recibe mensajes del sistema operativo
		para filtrarlos y usarlos en proceso de la ventana actual:
	*/
	while(GetMessage(&msg, NULL, 0, 0) > 0) /* Si no hay error recibido */
	{
		TranslateMessage(&msg); /* Traducir códigos de tecla a caracteres */
		DispatchMessage(&msg); /* Enviar los mensajes a la procedimiento o función principal de la ventana */
	}
	return msg.wParam;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar