NLineas.c



// DIBUJO DE LINEAS A COLORES CON VENTANA Y MENU

//	___________________________________________________________________________________________________________
//	OBJETIVO:	Se crea una ventana vacIa. Se accesa a un menU pulsando la tecla derecha del ratOn
//			El menU es contextual. Para crear menUs pull - down hay que utilizar QT (se instala aparte)
//	COMPILAR:	
//			gcc -o Proyecto_1 Proyecto_1.c -lGL -lGLU -lglut -pthread -lm
//	EJECUTAR:
//			./Proyecto_1
//	___________________________________________________________________________________________________________

#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

#define R_LINEAS 1
#define R_ABANICO 2
#define R_BORRAR 3
#define R_SALIR 4
#define R_AZUL 5
#define R_AMARILLO 6

void Evento_Menu (int);
void Demo_Lineas(void);
void Demo_Abanico(void);

int R_ACTUAL = 3, R_COLOR = R_AZUL;


void init(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
	glShadeModel (GL_FLAT);		// GL_FLAT   = rellena con el color activo del Ultimo parAmetro
					// GL_SMOOTH = interpolaciOn de colores
	int Sub_Demo, Sub_Color;

	Sub_Demo=glutCreateMenu(Evento_Menu);
	glutAddMenuEntry("Lineas", R_LINEAS);
	glutAddMenuEntry("Abanico", R_ABANICO);

	Sub_Color=glutCreateMenu(Evento_Menu);
	glutAddMenuEntry("Azul", R_AZUL);
	glutAddMenuEntry("Amarillo", R_AMARILLO);

	glutCreateMenu(Evento_Menu);
	glutAddSubMenu("Color", Sub_Color);
	glutAddSubMenu("Demos", Sub_Demo);
	glutAddMenuEntry("Borrar Pantalla", R_BORRAR);
	glutAddMenuEntry("Salir", R_SALIR);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void Evento_Menu(int Opcion)
{
	switch ( Opcion )
	{
		case R_LINEAS:
			R_ACTUAL = R_LINEAS;
			break;
		case R_ABANICO:
			R_ACTUAL = R_ABANICO;
			break;
		case R_BORRAR:
			R_ACTUAL = R_BORRAR;
			break;
		case R_AZUL:
			R_COLOR = R_AZUL;
			break;
		case R_AMARILLO:
			R_COLOR = R_AMARILLO;			
			break;
		case R_SALIR:
			exit(0);
	}
}

void Crea_Ambiente()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	if (R_COLOR == R_AZUL) glColor3f (0.0, 0.0, 1.0);
	if (R_COLOR == R_AMARILLO) glColor3f (0.0, 1.0, 1.0);

	switch ( R_ACTUAL )
	{
		case R_LINEAS:
			Demo_Lineas();
			break;
		case R_ABANICO:
			Demo_Abanico();
			break;
		case R_BORRAR:
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			break;
		case R_SALIR:
			exit(0);
	}
}

void drawOneLine(float x1, float y1, float x2, float y2)
{
	glBegin(GL_LINES);
		glVertex2f (x1, y1);
		glVertex2f (x2, y2);
	glEnd();
}

void Demo_Abanico( void )
{
	GLfloat angulo;
	int i;

	glBegin(GL_LINES);
	for (i=0; i<360; i+=3)
	{
		angulo = (GLfloat)i*3.14159f/180.0f;	// grados a radianes
		glVertex3f(0.0f, 0.0f, 0.0f);
		glVertex3f(500*cos(angulo), 500*sin(angulo), 0.0f);
	}
	glEnd();
	glFlush ();
}



void Demo_Lineas( void )
{
	int i;

	// select white for all lines 
	// in 1st row, 3 lines, each with a different stipple                       
	glEnable (GL_LINE_STIPPLE);
	glLineStipple (1, 0x0101); // dotted 
	drawOneLine (50.0, 125.0, 150.0, 125.0);
	glLineStipple (1, 0x00FF); // dashed 
	drawOneLine (150.0, 125.0, 250.0, 125.0);
	glLineStipple (1, 0x1C47); // dash/dot/dash
	drawOneLine (250.0, 125.0, 350.0, 125.0);
	// in 2nd row, 3 wide lines, each with different stipple 
	glLineWidth (5.0);
	glLineStipple (1, 0x0101); // dotted 
	drawOneLine (50.0, 100.0, 150.0, 100.0);
	glLineStipple (1, 0x00FF); // dashed 
	drawOneLine (150.0, 100.0, 250.0, 100.0);
	glLineStipple (1, 0x1C47); // dash/dot/dash 
	drawOneLine (250.0, 100.0, 350.0, 100.0);
	glLineWidth (1.0);
	// in 3rd row, 6 lines, with dash/dot/dash stipple 
	// as part of a single connected line strip                             
	glLineStipple (1, 0x1C47); // dash/dot/dash
	glBegin (GL_LINE_STRIP);
		for (i = 0; i < 7; i++) glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0);
	glEnd ();
	// in 4th row, 6 independent lines with same stipple
	for (i = 0; i < 6; i++) drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0, 50.0 + ((GLfloat)(i+1) * 50.0), 50.0);
	// in 5th row, 1 line, with dash/dot/dash stipple                        
	// and a stipple repeat factor of 5                                      
	glLineStipple (5, 0x1C47); // dash/dot/dash 
	drawOneLine (50.0, 25.0, 350.0, 25.0);
	glDisable (GL_LINE_STIPPLE);
	glFlush ();
}

// ________________________________________________
// ParAmetros por si cambia de dimensiOn la ventana
// ------------------------------------------------
void reshape (int w, int h)
{
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluOrtho2D (-(GLdouble) w, (GLdouble) w, -(GLdouble) h, (GLdouble) h);

}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition (100, 100);
	glutInitWindowSize (600, 600);
	glutCreateWindow (argv[0]);
	init ();
	glutDisplayFunc(Crea_Ambiente);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}