All pastes #204135 Raw Edit

Mine

public text v1 · immutable
#204135 ·published 2006-10-15 18:11 UTC
rendered paste body
////////////////////////////////////////////////////////
//				CSE 581: Lab 1
//				Brandon Walters
//				walters.235@osu.edu
//
//				Based off of Dr. Shen's "simple lab1" code
//
////////////////////////////////////////////////////////
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>

#define DRAW_POINT	1

#define RED			1
#define BLUE		2
#define GREEN		3
#define CLEAR_SCREEN 4

#define DISTANCE	60

/////////////////////////////////////////////
void create_menu();
/////////////////////////////////////////////

int drawing_color = RED;
int drawing_mode = 1;

int menu_id = NULL;

int width=500, height = 500; // display window width and height

int press_x, press_y;
int release_x, release_y;
int rotate=0;
int mouse_x=0, mouse_y=0;

typedef struct
{
	int x1, x2, y1, y2;
	int color;
	int rotate;
}polyline;
polyline *polylines;
int polyline_count = 0;



int   type_list[200];       // primitive buffer
                            // 200: max number of primitives allowed
float vertex_list_x[1000];  // x buffer
float vertex_list_y[1000];  // y buffer
int color_list[200];


int   nop = 0; // number of primitives
int   nov = 0; // number of vertices

/////////////////////////////////////////////

void clear_screen()
{
	nop = 0;
	nov = 0;

	free(polylines);
	polylines = NULL;
	polyline_count = 0;

	glutPostRedisplay(); // now display everything in the buffers
}

/////////////////////////////////////////////
void display_all()
{
  float x, y;
  int p_idx, next_primitive; int v_idx = 0;

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  //glPushMatrix();

	for (int i=0; i<polyline_count;i++)
	{
		if (polylines[i].color == RED)
			glColor3f(1,0,0);
		else if (polylines[i].color == BLUE)
			glColor3f(0,0,1);
		else if (polylines[i].color == GREEN)
			glColor3f(0,1,0);

		//glMatrixMode(GL_MODELVIEW);
		//glPushMatrix();
		//glRotatef(90, 0.0, 0.0, 1.0);
		glTranslatef(polylines[i].x1+DISTANCE/2,polylines[i].y1,0);
		glRotatef(polylines[i].rotate,0,0,1);	// rotate by rotate angle
		glTranslatef(-polylines[i].x1-DISTANCE/2,-polylines[i].y1,0);
		glBegin(GL_LINES);
			glVertex2f(polylines[i].x1,height-polylines[i].y1);
			glVertex2f(polylines[i].x2,height-polylines[i].y2);
		glEnd();

		glLoadIdentity();

	}
	//glPopMatrix();
	
	glFlush();  // force OpenGL to draw all the objects 

  //if (nop!=0) {  // there primitive buffer is not empty

  //  for (p_idx = 0; p_idx <nop; p_idx++) {

  //    next_primitive = type_list[p_idx];

  //    if (next_primitive==1) //glColor3f(1,1,1);     // point: white
	 // {
		//if (color_list[p_idx] == RED)
		//	glColor3f(1,0,0);
		//else if (color_list[p_idx] == BLUE)
		//	glColor3f(0,0,1);
		//else if (color_list[p_idx] == GREEN)
		//	glColor3f(0,1,0);
	 // }

  //    if (next_primitive==1){
		//glPointSize(3);
		//glBegin(GL_POINTS);
		//  x = vertex_list_x[v_idx];
		//  y = vertex_list_y[v_idx];
		//  glVertex2f(x,y);
		//glEnd();
		//glPointSize(1);
		//v_idx++;
  //    }
  //    glFlush();  // force OpenGL to draw all the objects 
  //  }
  //}
}

/////////////////////////////////////////////

void display()
{
  // clear the screen
  glClearColor(0,0,0,1);  //  background color - black
  glClear(GL_COLOR_BUFFER_BIT);

  // set the world window
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, 499, 0, 499);  // here I just hard-wire the world window 

  // now call the real drawing function
  display_all();


}

//////////////////////////////////////////////////
//
//  The callback functions
//
//////////////////////////////////////////////////

void mymouse(int button, int state, int x, int y)
{
	mouse_x = x;
	mouse_y = y;

  //if (drawing_mode == DRAW_POINT){
  //  if (state == GLUT_UP) {
  //    // insert to both the primitive and vertex buffers
  //    type_list[nop++] = 1; // 1: point
  //    vertex_list_x[nov] = x;
  //    vertex_list_y[nov++] = height - y;  // GLUT and GL define the
  //                                        // origin at different positions
	 // color_list[nop-1] = drawing_color;
  //  }
  //}
  //glutPostRedisplay(); // now display everything in the buffers
}
/////////////////////////////////////////////

void create_line(int r)
{
	polyline_count++;
	polylines = (polyline*) realloc(polylines, sizeof(polyline) * polyline_count);

	polylines[polyline_count-1].x1 = mouse_x;
	polylines[polyline_count-1].y1 = mouse_y;
	polylines[polyline_count-1].x2 = mouse_x + DISTANCE;
	polylines[polyline_count-1].y2 = mouse_y;

	polylines[polyline_count-1].rotate = r;
	polylines[polyline_count-1].color = drawing_color;

	mouse_x = polylines[polyline_count-1].x2;
	mouse_y = polylines[polyline_count-1].y2 + sin(3.14*180*r)*DISTANCE;

	glutPostRedisplay(); // now display everything in the buffers
}

/////////////////////////////////////////////

void mymenu(int value)
{
  //if (value == 1) drawing_mode = DRAW_POINT;
	if (value == RED)
	{
		drawing_color = RED;
	}
	else if (value == BLUE)
	{
		drawing_color = BLUE;
	}
	else if (value == GREEN)
	{
		drawing_color = GREEN;
	}
	else if (value == CLEAR_SCREEN)
	{
		clear_screen();
	}

	create_menu();

}

void create_menu()
{
	
	if (menu_id != NULL) glutDestroyMenu(menu_id);

	menu_id = glutCreateMenu(mymenu);
	if (drawing_color == RED) glutAddMenuEntry("->Red", RED); else glutAddMenuEntry("Red", RED);
	if (drawing_color == GREEN) glutAddMenuEntry("->Green", RED); else glutAddMenuEntry("Green", GREEN);
	if (drawing_color == BLUE) glutAddMenuEntry("->Blue", RED); else glutAddMenuEntry("Blue", BLUE);
	glutAddMenuEntry("-----------------", CLEAR_SCREEN);
	glutAddMenuEntry("Clear Screen", CLEAR_SCREEN);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

/////////////////////////////////////////////

void mykey(unsigned char key, int x, int y)
{
	if (key =='q')
		exit(0);
	else if (key == 'f')
	{
		rotate = 0;
		create_line(0);
	}
	else if (key == 't')
	{
		rotate += 45;
		if (rotate > 360) rotate=0;
		create_line(rotate);
	}
}

////////////////////////////////////////////////////

void resize(int w, int h)
{
  // this is really just the default GLUT behavior. you should change it for the lab1
  //glViewport(0,0,w,h); 
  //glutPostRedisplay(); 
	width = w;
   height = h;

   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(65.0, (float)width / height, 1, 1000);
   glMatrixMode(GL_MODELVIEW);
}

////////////////////////////////////////////////////
void myInit() {
  // did not really do much here 
  // set the viewport
  glViewport(0,0,width, height);
}


//////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
  // init glut drawing window
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
  glutInitWindowSize(width,height);
  glutCreateWindow("Brandon Walters' Lab 1");

  myInit();   // my own OpenGL initialization

  // register callbacks
  glutDisplayFunc(display);
  glutReshapeFunc(resize);
  glutMouseFunc(mymouse);
  glutKeyboardFunc(mykey);

  // create menu
  create_menu();

  // enter the event loop
  glutMainLoop();
}