3 #include <SDL/SDL_opengl.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;
20 glClearColor( 0, 0, 0, 0 );
23 glViewport(0, 0, SCREEN_W
, SCREEN_H
);
26 glMatrixMode( GL_PROJECTION
);
28 glOrtho( 0, SCREEN_W
, SCREEN_H
, 0, -1, 1 );
30 //Initialize modelview matrix
31 glMatrixMode( GL_MODELVIEW
);
34 //translada hacia el fondo
35 glTranslatef( DRAW_W
/8, DRAW_H
, 0.0f
);
36 //rota en x 180 grados
38 //if there was any errors
39 if( glGetError() != GL_NO_ERROR
)
44 //if everything initialized
51 if( SDL_Init( SDL_INIT_EVERYTHING
) < 0 )
57 if( SDL_SetVideoMode( SCREEN_W
, SCREEN_H
, SCREEN_BPP
, SDL_OPENGL
) == NULL
)
69 SDL_WM_SetCaption( "OpenGL Test", NULL
);
74 void dibuja_plano(int x
, int y
)
79 // clear the screen before drawing:
80 glClear( GL_COLOR_BUFFER_BIT
);
83 glBegin(GL_TRIANGLES
);
86 glVertex3f( 0, 1, 0 );
87 glVertex3f( x
, y
, 0 );
88 glVertex3f( x
, 1, 0 );
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 );
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
;
115 a1
[0] = magnitud
*cos( theta
);
116 b1
[0] = magnitud
*sin( theta
);
118 //obtengo los angulos
122 a1
[1] = magnitud2
*cos( theta
);
123 b1
[1] = magnitud2
*sin( theta
);
125 //obtengo los angulos
128 a1
[2] = magnitud
*cos( theta
);
129 b1
[2] = magnitud
*sin( theta
);
139 glColor3f( 1.0f
, 0.0f
, 0.0f
); /* Red */
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 */
150 void dibuja_flecha(int x1
, int y1
){
151 float theta
= atan2(y1
, x1
); //angulo del vector
152 int magnitud
= 20;//magnitud del vector
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
)
177 glVertex3f( x
, y
+30, 0 );
178 glVertex3f( x
, y
, 0 );
183 int main(int argc
, char *argv
[])
187 double posX
=x
*0.85 , posY
=y
*0.85;
196 SDL_Event event
; //the event structure
197 //while the user hasn't quit
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
)
214 if( posY
> 1 && posX
> 1)
224 dibuja_cuadrado( posX
, posY
, square_size
);
225 SDL_GL_SwapBuffers();