3 #include <SDL/SDL_opengl.h>
8 const int SCREEN_WIDTH
= 640;
9 const int SCREEN_HEIGHT
= 480;
10 const int SCREEN_BPP
= 32;
13 void draw_line(int, int, int, int vectores
[][2]);
15 void draw_vectors(int, int vectores
[][2]);
16 void dibuja_flecha(int, int);
18 int main(int argc
, char *argv
[])
29 SDL_Event event
; //The event structure
30 int cantidad
= 0; // cantidad de vectores
31 int vectores
[1000][2]; //arreglo de vectores
38 //While the user hasn't quit
42 //If there's events to handle
43 if( SDL_PollEvent( &event
) )
46 // glViewport(0, 0, 600, 800);
52 if( event
.type
== SDL_MOUSEMOTION
)
54 //Get the mouse offsets
57 draw_line(x
, y
, cantidad
, vectores
);
60 if (event
.type
== SDL_MOUSEBUTTONDOWN
)
62 if( event
.button
.button
== SDL_BUTTON_LEFT
)
64 vectores
[cantidad
][0] = event
.button
.x
;
65 vectores
[cantidad
][1] = event
.button
.y
;
71 //If the user has Xed out the window
72 if( event
.type
== SDL_QUIT
)
88 glClearColor( 0, 0, 0, 0 );
91 glViewport(0, 0, SCREEN_WIDTH
, SCREEN_HEIGHT
);
94 glMatrixMode( GL_PROJECTION
);
96 glOrtho( 0, SCREEN_WIDTH
, SCREEN_HEIGHT
, 0, -1, 1 );
98 //Initialize modelview matrix
99 glMatrixMode( GL_MODELVIEW
);
102 //If there was any errors
103 if( glGetError() != GL_NO_ERROR
)
108 //If everything initialized
115 if( SDL_Init( SDL_INIT_EVERYTHING
) < 0 )
121 if( SDL_SetVideoMode( SCREEN_WIDTH
, SCREEN_HEIGHT
, SCREEN_BPP
, SDL_OPENGL
) == NULL
)
133 SDL_WM_SetCaption( "OpenGL Test", NULL
);
138 void draw_line(int x
, int y
, int cantidad
, int vectores
[][2])
140 // Clear the screen before drawing:
141 glClear( GL_COLOR_BUFFER_BIT
);
147 draw_vectors(cantidad
, vectores
);
150 glVertex3f( SCREEN_WIDTH
/2, SCREEN_HEIGHT
/2, 0 );
151 glVertex3f( x
, y
, 0 );
157 void draw_vectors(int cantidad
, int vectores
[][2])
160 while(i
< cantidad
&& (cantidad
!= 0))
162 glVertex3f( SCREEN_WIDTH
/2, SCREEN_HEIGHT
/2, 0 );
163 glVertex3f( vectores
[i
][0], vectores
[i
][1], 0 );
171 glVertex3f( SCREEN_WIDTH
/2, 20, 0 );
172 glVertex3f( SCREEN_WIDTH
/2,SCREEN_HEIGHT
- 20, 0 );
174 glVertex3f( SCREEN_WIDTH
- 20, SCREEN_HEIGHT
/2, 0 );
175 glVertex3f( 20, SCREEN_HEIGHT
/2,0 );
178 void dibuja_flecha(int x1
, int y1
){
179 int x
= x1
- SCREEN_WIDTH
/2;
180 int y
= y1
- SCREEN_HEIGHT
/2;
181 float theta
= atan2(y
, x
); //angulo del vector
182 int magnitud
= 20;//magnitud del vector
183 float a1
=0, b1
= 0; //
186 //valores del vector flecha
187 //obtengo los angulos
188 float phi1
= theta
+ 210;
189 float phi2
= theta
- 210;
191 //obtengo los puntos y le sumo el valor del punto P(x1,y1)
192 //para obtener una representacion particular
193 a1
= magnitud
*cos( phi1
) + x1
; b1
= magnitud
*sin( phi1
) + y1
;
194 a2
= magnitud
*cos( phi2
) + x1
; b2
= magnitud
*sin( phi2
) + y1
;
196 //dibuja la lineas de la flecha
197 glVertex3f ( x1
, y1
, 0);
198 glVertex3f ( a1
, b1
, 0);
199 glVertex3f ( x1
, y1
, 0);
200 glVertex3f ( a2
, b2
, 0);