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
32 glRotatef(15, 1.0, 0.0, 0.0);
33 glRotatef(-25, 0.0, 1.0, 0.0);
38 //While the user hasn't quit
41 //If there's events to handle
42 if( SDL_PollEvent( &event
) )
48 if( event
.type
== SDL_MOUSEMOTION
)
50 //Get the mouse offsets
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
;
68 //If the user has Xed out the window
69 if( event
.type
== SDL_QUIT
)
85 glClearColor( 0, 0, 0, 0 );
88 glViewport(0, 0, SCREEN_WIDTH
, SCREEN_HEIGHT
);
91 glMatrixMode( GL_PROJECTION
);
93 gluPerspective(45.0f
,(GLfloat
) SCREEN_WIDTH
/(GLfloat
)SCREEN_HEIGHT
, 0.1f
, 1000.0f
);
94 //Initialize modelview matrix
95 glMatrixMode( GL_MODELVIEW
);
98 glTranslatef( 0.0f
, 0.0f
, -500.0f
);
99 //If there was any errors
100 if( glGetError() != GL_NO_ERROR
)
105 //If everything initialized
112 if( SDL_Init( SDL_INIT_EVERYTHING
) < 0 )
118 if( SDL_SetVideoMode( SCREEN_WIDTH
, SCREEN_HEIGHT
, SCREEN_BPP
, SDL_OPENGL
) == NULL
)
130 SDL_WM_SetCaption( "OpenGL Test", NULL
);
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
);
144 draw_vectors(cantidad
, vectores
);
147 glVertex3f( 0, 0, 0 );
148 glVertex3f( x
, 0, -1*y
);
154 void draw_vectors(int cantidad
, int vectores
[][2])
157 while(i
< cantidad
&& (cantidad
!= 0))
159 glVertex3f( 0, 0, 0 );
160 glVertex3f( vectores
[i
][0], vectores
[i
][1], 0 );
168 glVertex3f( SCREEN_WIDTH
/2 - 100, 0, 0 );
169 glVertex3f( -1*SCREEN_WIDTH
/2 + 50, 0, 0 );
172 glVertex3f( 0, SCREEN_HEIGHT
/2 - 60 , 0 );
173 glVertex3f( 0, -1*SCREEN_HEIGHT
/2 + 30,0 );
176 glVertex3f( 0, 0, SCREEN_HEIGHT
/2 - 50);
177 glVertex3f( 0, 0, -2*SCREEN_HEIGHT
/2 );
181 void dibuja_flecha(int x1
, int y1
){
184 float theta
= atan2(y
, x
); //angulo del vector
185 int magnitud
= 20;//magnitud del vector
186 float a1
=0, b1
= 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
);