Useless update
[hammerdown.git] / dethphysics / vector3D.c
blob0cf010f7912fe631a5b5c83b9f2a2f2e846a4a36
1 //headers
2 #include <SDL/SDL.h>
3 #include <SDL/SDL_opengl.h>
4 #include <stdio.h>
5 #include <math.h>
7 //Screen attributes
8 const int SCREEN_WIDTH = 640;
9 const int SCREEN_HEIGHT = 480;
10 const int SCREEN_BPP = 32;
12 //function prototypes
13 void draw_line(int, int, int, int vectores[][2]);
14 void draw_axis();
15 void draw_vectors(int, int vectores[][2]);
16 void dibuja_flecha(int, int);
18 int main(int argc, char *argv[])
20 int quit = 0;
22 //Initialize
23 if( init() == 0 )
25 return 1;
28 SDL_Surface *screen;
29 SDL_Event event; //The event structure
30 int cantidad = 0; // cantidad de vectores
31 int vectores[1000][2]; //arreglo de vectores
32 glRotatef(15, 1.0, 0.0, 0.0);
33 glRotatef(-25, 0.0, 1.0, 0.0);
34 glBegin(GL_LINES);
35 draw_axis();
36 glEnd();
37 //escucha de eventos
38 //While the user hasn't quit
39 while( quit == 0 )
41 //If there's events to handle
42 if( SDL_PollEvent( &event ) )
44 //The mouse offsets
45 int x = 0, y = 0;
47 //If the mouse moved
48 if( event.type == SDL_MOUSEMOTION )
50 //Get the mouse offsets
51 x = event.motion.x;
52 y = event.motion.y;
53 x = x - SCREEN_WIDTH/2;
54 y = SCREEN_HEIGHT/2 - y;
55 draw_line(x, y, cantidad, vectores);
58 if (event.type == SDL_MOUSEBUTTONDOWN)
60 if( event.button.button == SDL_BUTTON_LEFT )
62 vectores[cantidad][0] = event.button.x;
63 vectores[cantidad][1] = event.button.y;
64 cantidad++;
67 SDL_GL_SwapBuffers();
68 //If the user has Xed out the window
69 if( event.type == SDL_QUIT )
71 //Quit the program
72 quit = 1;
77 SDL_Quit();
79 return 0;
82 int init_GL()
84 //Set clear color
85 glClearColor( 0, 0, 0, 0 );
87 //reset the viewport
88 glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
90 //Set projection
91 glMatrixMode( GL_PROJECTION );
92 glLoadIdentity();
93 gluPerspective(45.0f,(GLfloat) SCREEN_WIDTH/(GLfloat)SCREEN_HEIGHT, 0.1f, 1000.0f);
94 //Initialize modelview matrix
95 glMatrixMode( GL_MODELVIEW );
96 glLoadIdentity();
98 glTranslatef( 0.0f, 0.0f, -500.0f );
99 //If there was any errors
100 if( glGetError() != GL_NO_ERROR )
102 return 0;
105 //If everything initialized
106 return 1;
109 int init()
111 //Initialize SDL
112 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
114 return 0;
117 //Create Window
118 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
120 return 0;
123 //Initialize OpenGL
124 if( init_GL() == 0 )
126 return 0;
129 //Set caption
130 SDL_WM_SetCaption( "OpenGL Test", NULL );
132 return 1;
135 void draw_line(int x, int y, int cantidad, int vectores [][2])
137 // Clear the screen before drawing:
138 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
140 // draw the line
141 glBegin(GL_LINES);
143 draw_axis();
144 draw_vectors(cantidad, vectores);
146 //dibuja lineas
147 glVertex3f( 0, 0, 0 );
148 glVertex3f( x, 0, -1*y );
149 dibuja_flecha(x,y);
151 glEnd();
154 void draw_vectors(int cantidad, int vectores[][2])
156 int i=0;
157 while(i < cantidad && (cantidad != 0))
159 glVertex3f( 0, 0, 0 );
160 glVertex3f( vectores[i][0], vectores[i][1], 0 );
161 i++;
165 void draw_axis()
167 //eje x
168 glVertex3f( SCREEN_WIDTH/2 - 100, 0, 0 );
169 glVertex3f( -1*SCREEN_WIDTH/2 + 50, 0, 0 );
171 //eje y
172 glVertex3f( 0, SCREEN_HEIGHT/2 - 60 , 0 );
173 glVertex3f( 0, -1*SCREEN_HEIGHT/2 + 30,0 );
175 //eje z
176 glVertex3f( 0, 0, SCREEN_HEIGHT/2 - 50);
177 glVertex3f( 0, 0, -2*SCREEN_HEIGHT/2 );
181 void dibuja_flecha(int x1, int y1){
182 int x = x1;
183 int y = y1;
184 float theta = atan2(y, x); //angulo del vector
185 int magnitud = 20;//magnitud del vector
186 float a1=0, b1 = 0; //
187 float a2=0, b2 = 0;
189 //valores del vector flecha
190 //obtengo los angulos
191 float phi1 = theta + 210;
192 float phi2 = theta - 210;
194 //obtengo los puntos y le sumo el valor del punto P(x1,y1)
195 //para obtener una representacion particular
196 a1 = magnitud*cos( phi1 ) + x1; b1 = magnitud*sin( phi1 ) + y1;
197 a2 = magnitud*cos( phi2 ) + x1; b2 = magnitud*sin( phi2 ) + y1;
199 //dibuja la lineas de la flecha
200 glVertex3f ( x1, 0, -y1);
201 glVertex3f ( a1, 0, -b1);
202 glVertex3f ( x1, 0, -y1);
203 glVertex3f ( a2, 0, -b2);