Useless update
[hammerdown.git] / dethphysics / planoInclinado.c
blob8d35a9d2d147a1e8aa454884d324c50600512512
1 //headers
2 #include <SDL/SDL.h>
3 #include <SDL/SDL_opengl.h>
4 #include <stdio.h>
5 #include <math.h>
7 //screen main attributes
8 const int SCREEN_W = 800;
9 const int SCREEN_H = 600;
10 const int SCREEN_BPP = 32;
12 const float PI = 3.1416;
14 int DRAW_W = 800*3/4;
15 int DRAW_H = 600*3/4;
17 int init_GL()
19 //set clear color
20 glClearColor( 0, 0, 0, 0 );
22 //set the viewport
23 glViewport(0, 0, SCREEN_W, SCREEN_H);
25 //set projection
26 glMatrixMode( GL_PROJECTION );
27 glLoadIdentity();
28 glOrtho( 0, SCREEN_W, SCREEN_H, 0, -1, 1 );
30 //Initialize modelview matrix
31 glMatrixMode( GL_MODELVIEW );
32 glLoadIdentity();
34 //translada hacia el fondo
35 glTranslatef( DRAW_W/8, DRAW_H, 0.0f );
36 //rota en x 180 grados
37 glRotatef(180,1,0,0);
38 //if there was any errors
39 if( glGetError() != GL_NO_ERROR )
41 return 0;
44 //if everything initialized
45 return 1;
48 int init()
50 //initialize SDL
51 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
53 return 0;
56 //create Window
57 if( SDL_SetVideoMode( SCREEN_W, SCREEN_H, SCREEN_BPP, SDL_OPENGL ) == NULL )
59 return 0;
62 //initialize OpenGL
63 if( init_GL() == 0 )
65 return 0;
68 //set caption
69 SDL_WM_SetCaption( "OpenGL Test", NULL );
71 return 1;
74 void dibuja_plano(int x, int y)
76 int baseX = 150;
77 int baseY = 150;
79 // clear the screen before drawing:
80 glClear( GL_COLOR_BUFFER_BIT );
82 // draw the line
83 glBegin(GL_TRIANGLES);
85 //dibuja plano
86 glVertex3f( 0, 1, 0 );
87 glVertex3f( x, y, 0 );
88 glVertex3f( x, 1, 0 );
90 glEnd();
93 void dibuja_limites()
95 // draw the line
96 glBegin(GL_LINES);
98 //dibuja plano
99 glVertex3f( -DRAW_W/8, 0, 0 );
100 glVertex3f( DRAW_W, 0, 0 );
101 glVertex3f( DRAW_W, 0, 0 );
102 glVertex3f( DRAW_W, DRAW_H, 0 );
104 glEnd();
108 void dibuja_cuadrado(double x, double y, int sq){
109 float theta = atan2(y, x); //angulo del vector
110 int magnitud = sq; //magnitud del vector
111 int magnitud2 = sqrt(2)*magnitud;
112 double a1[4], b1[4];
114 //obtengo los puntos
115 a1[0] = magnitud*cos( theta );
116 b1[0] = magnitud*sin( theta );
118 //obtengo los angulos
119 theta += PI/4;
121 //obtengo los puntos
122 a1[1] = magnitud2*cos( theta );
123 b1[1] = magnitud2*sin( theta );
125 //obtengo los angulos
126 theta += PI/4;
127 //obtengo los puntos
128 a1[2] = magnitud*cos( theta );
129 b1[2] = magnitud*sin( theta );
131 //obtengo los puntos
132 a1[3] = 0;
133 b1[3] = 0;
136 //dibuja cuadrado
137 glBegin(GL_QUADS);
139 glColor3f( 1.0f, 0.0f, 0.0f ); /* Red */
140 int i;
141 for(i = 0; i < 4; i++)
143 glVertex3f( a1[i] + x , b1[i] + y, 0 );
145 glColor3f( 1.0f, 1.0f, 1.0f ); /* White */
147 glEnd();
150 void dibuja_flecha(int x1, int y1){
151 float theta = atan2(y1, x1); //angulo del vector
152 int magnitud = 20;//magnitud del vector
153 float a1=0, b1 = 0;
154 float a2=0, b2 = 0;
156 //valores del vector flecha
157 //obtengo los angulos en radianes
158 float phi1 = theta + 210;
159 float phi2 = theta - 210;
161 //obtengo los puntos y le sumo el valor del punto P(x1,y1)
162 //para obtener una representacion particular
163 a1 = magnitud*cos( phi1 ) + x1; b1 = magnitud*sin( phi1 ) + y1;
164 a2 = magnitud*cos( phi2 ) + x1; b2 = magnitud*sin( phi2 ) + y1;
166 //dibuja la lineas de la flecha
167 glVertex3f ( x1, y1, 0);
168 glVertex3f ( a1, b1, 0);
169 glVertex3f ( x1, y1, 0);
170 glVertex3f ( a2, b2, 0);
173 void draw_line(int x, int y)
175 glBegin(GL_LINES);
176 //dibuja lineas
177 glVertex3f( x, y+30, 0 );
178 glVertex3f( x, y, 0 );
179 dibuja_flecha(x,y);
180 glEnd();
183 int main(int argc, char *argv[])
185 int quit = 0;
186 int x = 415, y= 245;
187 double posX=x*0.85 , posY=y*0.85;
188 int square_size=50;
189 //initialize
190 if( init() == 0 )
192 return 1;
195 SDL_Surface *screen;
196 SDL_Event event; //the event structure
197 //while the user hasn't quit
198 while( quit == 0 )
201 //if there's events to handle
202 if( SDL_PollEvent( &event ) )
204 //if the user has Xed out the window
205 if( event.type == SDL_QUIT )
207 //quit the program
208 quit = 1;
212 dibuja_plano(x, y);
213 dibuja_limites();
214 if( posY > 1 && posX > 1)
216 if(y > x){
217 posX-= 0.1*y/x;
218 posY-= 0.1;
219 }else{
220 posX-= 0.1;
221 posY-= 0.1*y/x;
224 dibuja_cuadrado( posX, posY, square_size );
225 SDL_GL_SwapBuffers();
230 SDL_Quit();
232 return 0;