Exemplo n.º 1
0
    void rasterizer::draw_triangles()
    {
        const auto& desc = state_->get_desc();
        if (desc.fm == fill_wireframe) {
            draw_lines();
        } else if (desc.fm == fill_solid) {
			if (prim_vert_cnt_ < 3) {
				draw_lines();
				return;
			}
			//draw_lines();
			assert(prim_vert_cnt_ == 3);
			bool have_color = !colors_.empty();
			for (size_t index = 0; index < indices_.size(); ) {
				const auto& v0 = wvp_[indices_[index]];
				const auto& v1 = wvp_[indices_[index + 1]];
				const auto& v2 = wvp_[indices_[index + 2]];
				const color_rgba8* c0 = &color_rgba8::black;
				const color_rgba8* c1 = &color_rgba8::black;
				const color_rgba8* c2 = &color_rgba8::black;
				if (have_color) {
					c0 = &colors_[indices_[index]];
					c1 = &colors_[indices_[index + 1]];
					c2 = &colors_[indices_[index + 2]];
				}
				draw_triangle(v0.x, v0.y, v1.x, v1.y, v2.x, v2.y, *c0, *c1, *c2);
				index += prim_vert_cnt_;
			}
        }
    }
Exemplo n.º 2
0
Arquivo: track.c Projeto: empeg/gpsapp
void track_draw(void)
{
    int head = tracklog_idx;

    if (tracklog_size == MAX_TRACK)
	draw_lines(&tracklog[head], MAX_TRACK - head, VFDSHADE_DIM);

    draw_lines(tracklog, head, VFDSHADE_DIM);
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: young-k/Notes
int main() {

  screen s;
  color c;

  /* Setup color and screen */

  clear_screen(s);
  
  c.red = 255;
  c.green = 150;
  c.blue = 100;

  /* Setup matrices */
  
  struct matrix *edges;
  struct matrix *transform;

  edges = new_matrix(4, 1);

  /* Finally Testing */
  
  add_edge(edges, 80, 80, 0, 80, 120, 0);
  add_edge(edges, 80, 120, 0, 120, 120, 0);
  add_edge(edges, 120, 120, 0, 120, 80, 0);
  add_edge(edges, 120, 80, 0, 80, 80, 0);

  add_edge(edges, 60, 80, 0, 80, 120, 0);
  add_edge(edges, 80, 120, 0, 100, 100, 0);
  add_edge(edges, 100, 100, 0, 80, 60, 0);
  add_edge(edges, 80, 60, 0, 60, 80, 0);

  add_edge(edges, 90, 90, 0, 110, 90, 0);
  add_edge(edges, 110, 90, 0, 110, 110, 0);
  add_edge(edges, 110, 110, 0, 90, 110, 0);
  add_edge(edges, 90, 110, 0, 90, 90, 0);
  
  draw_lines(edges, s, c);

  int i;
  for (i = 0; i < 40; i+=2) {
    transform = make_translate(i, i, i);
    matrix_mult(transform, edges);
    draw_lines(edges, s, c);
  }  
  
  display(s);
  save_extension(s, "matrix.png");

  /* Free Matrices */
  
  free_matrix( transform );
  free_matrix( edges );

  return 0;
}  
Exemplo n.º 4
0
int		main()
{
  t_infos	s;
  int		color;
  t_vect	a;
  t_vect	b;
  
  s.mlx_ptr = mlx_init();
  s.win_ptr = mlx_new_window(s.mlx_ptr, 500, 500, "lolilol");
  a.x = 0;
  a.y = 0;
  b.x = 250;
  b.y = 250;
  while (1)
    {
      color = rand_val(0, 0x00FFFFFF);
      while (a.x != 500)
	{
      color = rand_val(0, 0x00FFFFFF);
	  draw_lines(s, a, b, color);
	  usleep(TIME);
	  a.x++;
	}
      while (a.y != 500)
	{
      color = rand_val(0, 0x00FFFFFF);
	  draw_lines(s, a, b, color);
	  a.y++;
	  usleep(TIME);
	}
      while (a.x != 0)
	{
      color = rand_val(0, 0x00FFFFFF);
	  draw_lines(s, a, b, color);      
	  a.x--;
	  usleep(TIME);
	}
      while (a.y != 0)
	{
      color = rand_val(0, 0x00FFFFFF);
	  draw_lines(s, a, b, color);
	  a.y--;
	  usleep(TIME);
	}
    }

  mlx_loop(s.win_ptr);
}
Exemplo n.º 5
0
int main( int argc, char** argv ) {

  screen s;
  struct matrix *edges;
  struct matrix *transform;
  color c;
  c.red = 200;
  c.blue = 150;
  c.green = 150;
  
  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);

  clear_screen( s );
  
  add_sphere( edges, 250, 250, 50, .01 );
  add_torus( edges, 200, 200, 25, 50, .01 );
  add_box( edges, 250, 250, 0, 100, 100, 100 );
  
  draw_lines( edges, s, c );
  
  /*  if ( argc == 2 )
    parse_file( argv[1], transform, edges, s );
  else
    parse_file( "stdin", transform, edges, s );
  */
  display(s);

  printf("hi\n");
  save_ppm( s, "curves.ppm" );
  
  free_matrix( transform );
  free_matrix( edges );
}  
Exemplo n.º 6
0
void draw_lines(std::shared_ptr<image_t> img, const std::vector<vec2f_t>& lines, float color, int thickness, const mat3f_t& image_transform, std::function<bool (uint32_t)> accept_lines) {
    std::vector<vec2i_t> discrete_lines(lines.size());
    for (uint32_t i = 0; i < lines.size(); ++i) {
        discrete_lines[i] = (image_transform * lines[i].homogeneous()).head(2).template cast<int>();
    }
    draw_lines(img, discrete_lines, color, thickness, accept_lines);
}
Exemplo n.º 7
0
void MGCommandDrawer::draw_points_lines(
		const MGColor& pcolor,	//color of points(boundary)
		const MGColor& lcolor,	//color of line.
		const std::vector<MGPosition>& ipos
)const{
	draw_lines(lcolor,ipos);
	draw_points(pcolor,ipos);
}
Exemplo n.º 8
0
void Shape::draw() const
{
    Fl_Color oldc = fl_color();
    // there is no good portable way of retrieving the current style
    fl_color(lcolor.as_int());            // set color
    fl_line_style(ls.style(),ls.width()); // set style
    draw_lines();
    fl_color(oldc);      // reset color (to previous)
    fl_line_style(0);    // reset line style to default
}
Exemplo n.º 9
0
static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    cairo_save (cr);
    cairo_set_line_width (cr, FIXED_POINT_MIN*10.0);
    draw_lines (cr);
    cairo_restore (cr);

    cairo_translate (cr, CELL_WIDTH + PAD, 0);
    cairo_save (cr);
    cairo_set_line_width (cr, FIXED_POINT_MIN);
    draw_lines (cr);
    cairo_restore (cr);

    cairo_translate (cr, CELL_WIDTH + PAD, 0);
    cairo_save (cr);
    cairo_set_line_width (cr, FIXED_POINT_MIN/10.0);
    draw_lines (cr);
    cairo_restore (cr);

    return CAIRO_TEST_SUCCESS;
}
Exemplo n.º 10
0
void
draw_grid(cairo_t *cr, gint width, gint height)
{
  if (grid_snap && grid_type != GRID_HIDDEN)
    {
      cairo_save (cr);
      if (grid_type == GRID_LINES)
        {
          draw_lines(cr, width, height);
        }
      else
        {
          draw_crosses(cr, width, height);
        }
      cairo_restore (cr);
    }
}
Exemplo n.º 11
0
Arquivo: main.c Projeto: stuydw/matrix
void drawPicture(screen s, color c){

  struct matrix* picture = new_matrix(3, 5); 

  int startx = 45; 
  int starty = 400; 


  int counter = 1; 
  int sizec = 0.5; 
  int locationc = 50; 

  while(counter != 9){
    free_matrix(picture); 
    picture = new_matrix(3,5); 
    startx+= locationc; 
    //starty = starty + 25*(7-counter); 
    add_edge(picture, startx, starty, 1, startx+50, starty, 1); 
    add_edge(picture, startx+50, starty, 1, startx+90, starty-40, 1); 
    add_edge(picture, startx+90, starty-40, 1, startx+90, starty-90, 1); 
    add_edge(picture, startx+90, starty-90, 1, startx+50, starty-130, 1); 
    add_edge(picture, startx+50, starty-130, 1, startx, starty-130, 1); 
    add_edge(picture, startx, starty-130, 1, startx-40, starty-90,1); 
    add_edge(picture, startx-40, starty-90,1, startx-40,starty-40,1); 
    add_edge(picture, startx-40, starty-40,1, startx, starty, 1); 
    scalar_mult(sizec +(counter *0.10), picture); 
    locationc+= 5; 
    draw_lines(picture, s,c); 

    counter++; 
  }
 
  //print_matrix(picture); 
 
  //scalar_mult(1.5, picture); 
 
   /*
    scalar_mult(0.5, picture); 
    matrix_mult(transformers, picture); 
    draw_lines(picture, s, c); 
  */

  


}
Exemplo n.º 12
0
void draw_lines(std::shared_ptr<image_t> img, const std::vector<vec2f_t>& lines, float color, int thickness, uint32_t width, uint32_t height, uint32_t border, std::function<bool (uint32_t)> accept_lines) {
    std::vector<vec2f_t> extruded_lines(lines.size());
    std::vector<vec2i_t> discrete_lines(lines.size());
    vec2f_t dim(width - 2*border, height - 2*border);
    for (uint32_t i = 0; i < lines.size() / 2; ++i) {
        vec2f_t p0 = lines[i*2], p1 = lines[i*2+1];
        vec2f_t dir = (p1 - p0).normalized();
        p0 -= 2.f * dir;
        p1 += 2.f * dir;
        extruded_lines[i*2+0] = p0;
        extruded_lines[i*2+1] = p1;
    }
    for (uint32_t i = 0; i < lines.size(); ++i) {
        vec2f_t rescaled = (extruded_lines[i].array() * dim.array()).matrix();// + vec2f_t::Constant(border);
        discrete_lines[i] = rescaled.template cast<int>();
    }
    draw_lines(img, discrete_lines, color, thickness, accept_lines);
}
Exemplo n.º 13
0
int main( int argc, char** argv ) {

  screen s;
  struct matrix *edges;
  struct matrix *transform;
  color c;
  c.red = 0;
  c.green = 255;
  c.blue = 255;

  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);

  if ( argc == 2 )
    parse_file( argv[1], transform, edges, s );
  else
    parse_file( "stdin", transform, edges, s );

  add_edge( edges, 250,0,0, 250,25,0 );//M                       
  add_edge( edges, 250,25,0, 263,0,0 );
  add_edge( edges, 263,0,0, 275,25,0 );
  add_edge( edges, 275,25,0, 275,0,0 );
  add_edge( edges, 280,0,0, 293,25,0 );//A                      
  add_edge( edges, 293,25,0, 305,0,0 );
  add_edge( edges, 287,13,0, 299,13,0 );
  add_edge( edges, 310,0,0, 325,25,0 );//Y                               
  add_edge( edges, 318,13,0, 305,25,0 );
  add_edge( edges, 330,0,0, 343,25,0 );//A                              
  add_edge( edges, 343,25,0, 355,0,0 );
  add_edge( edges, 337,13,0, 349,13,0 );
  add_edge( edges, 360,0,0, 360,25,0 );//N                         
  add_edge( edges, 360,25,0, 385,0,0 );
  add_edge( edges, 385,0,0, 385,25,0 );
  add_edge( edges, 390,0,0, 390,25,0 );//K                           
  add_edge( edges, 390,13,0, 408,25,0 );
  add_edge( edges, 395,14,0, 408,0,0 );
  draw_lines(edges, s, c);

  save_extension(s, "dimensional.png");
  display(s);

  free_matrix( transform );
  free_matrix( edges );
}  
Exemplo n.º 14
0
void make_crater_list() {
	int j;
        float theta;

	listnum = glGenLists(1);
	glNewList(listnum, GL_COMPILE);

	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glColor3f(0.27, 0.3, 0.27);
	for(j = 0; j < NUM_CRATERS; j++) {
		glPushMatrix();
		glTranslatef(x[j], 0.0, y[j]);

		glBegin(GL_TRIANGLE_STRIP);
		if (BINC * sqrt(x[j]*x[j] + y[j]*y[j]) > 2 * M_PI / 5) {
			draw_outside(radius[j], height[j], 2 * M_PI / 5 );
			draw_inside(radius[j], height[j], 2 * M_PI / 5);
 		}
		draw_outside(radius[j], height[j], BINC * sqrt(x[j]*x[j] + y[j]*y[j]));
		draw_inside(radius[j], height[j], BINC * sqrt(x[j]*x[j] + y[j]*y[j]));
		glEnd();
		glPopMatrix();
	}

        do_terrain();

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glColor3f(0.05, 0.07, 0.05);
	for(j = 0; j < NUM_CRATERS; j++) {
		glPushMatrix();
		glTranslatef(x[j], 0.0, y[j]);

		glBegin(GL_QUADS);
		draw_lines(radius[j], height[j], WINC);
		glEnd();

		glPopMatrix();
	}

	glEndList();
}
Exemplo n.º 15
0
	void rasterizer::draw()
	{
        if (indices_.empty() || wvp_.empty()) {
            return;
        }
        const auto& desc = state_->get_desc();
        switch (desc.prim_) {
            case pt_point:
                break;
            case pt_line:
                draw_lines();
                break;
            case pt_triangle:
                draw_triangles();
                break;
            case pt_none:
            default:
                break;
        }
	}
Exemplo n.º 16
0
void draw_letter (letter_t* let, color_t c, color_t oc, bool is_fill) {
  // printf("LET %c: ", let->c);
  if (is_fill) {
    for (int i = 0; i < let->lines.size(); i++) {
      letter_lines_t* lines = let->lines[i];
      glColor4f(oc[0], oc[1], oc[2], oc[3]);
      draw_fat_lines(&lines->points, 0.2);
    }
    for (int i = 0; i < let->lines.size(); i++) {
      letter_lines_t* lines = let->lines[i];
      glColor4f(c[0], c[1], c[2], c[3]);
      draw_fat_lines(&lines->points, 0.1);
    }
  } else {
    for (int i = 0; i < let->lines.size(); i++) {
      letter_lines_t* lines = let->lines[i];
      glColor4f(c[0], c[1], c[2], c[3]);
      draw_lines(&lines->points, GL_LINE_STRIP);
    }
  }
}
Exemplo n.º 17
0
void
gtk_vi_screen_refresh(GtkViScreen *vi)
{
    if (vi->lastx != vi->curx || vi->lasty != vi-> cury) {
	mark_lines(vi, vi->lasty, 
		vi->lastx ? *ColAt(vi,vi->lasty,vi->lastx-1) + 1 : 0, 
		vi->lasty+1, *ColAt(vi,vi->lasty,vi->lastx)+1);
	mark_lines(vi, vi->cury, 
		vi->curx ? *ColAt(vi,vi->cury,vi->curx-1) + 1 : 0, 
		vi->cury+1, *ColAt(vi,vi->cury,vi->curx)+1);
    }
    if (vi->marked_maxy == 0)
	return;
    draw_lines(vi, vi->marked_y, vi->marked_x, vi->marked_maxy, vi->marked_maxx);
    vi->marked_x = vi->cols;
    vi->marked_y = vi->rows;
    vi->marked_maxx = 0;
    vi->marked_maxy = 0;
    vi->lastx = vi->curx;
    vi->lasty = vi->cury;
}
Exemplo n.º 18
0
Arquivo: main.c Projeto: stuydw/matrix
int main() {

  screen s;
  color c;

  c.red = 255;
  c.green = 0;
  c.blue = 0;

  int i, j;

  for( i=0; i<XRES; i++) {
    for ( j=0; j<YRES; j++) {
      plot( s, c, i, j);
    }
  }



  c.red = 0;
  c.green = 0;
  c.blue = 0;

  int d = 0;
  struct matrix *picture = new_matrix(3,3);
  while(d < XRES){
   
    add_edge(picture, d, XRES-d, 1, XRES, d+d, 1);
    d++;  
  }


  draw_lines(picture, s, c);

  // display( s );    
  save_ppm(s,  "picture.ppm" );
  // save_extension(s, "image.jpg");
  
}  
Exemplo n.º 19
0
Arquivo: main.c Projeto: stuydw/matrix
int main() {

    screen s;
    color c;

    c.red = 0;
    c.green = 255;
    c.blue = 255;

    int i, j;

    for( i=0; i<XRES; i++)
        for ( j=0; j<YRES; j++) {

            c.red = random() % (MAX_COLOR + 1);
            c.green = random() % (MAX_COLOR + 1);
            c.blue = random() % (MAX_COLOR + 1);

            plot( s, c, i, j);
        }

    struct matrix * points = new_matrix(4, 4);
    add_point(points, 50, 250, 0);
    add_point(points, 70, 250, 0);
    add_edge(points, 30, 100, 0, 300, 400, 0);
    draw_lines(points, s, c);
    print_matrix(points);
    printf("\n");
    scalar_mult(2.0, points);
    printf("\n");
    ident(points);
    printf("\n");
    display( s );
    save_ppm(s,  "picture" );
    save_extension(s, "picture.jpg");

}
Exemplo n.º 20
0
int main(int argc, char **argv) {
    srand(time(NULL));
    rand();
    if (argc == 2 || argc == 3) {
        char *hostname = argv[1];
        int port = DEFAULT_PORT;
        if (argc == 3) {
            port = atoi(argv[2]);
        }
        db_disable();
        client_enable();
        client_connect(hostname, port);
        client_start();
    }
    if (!glfwInit()) {
        return -1;
    }
    create_window();
    if (!window) {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(VSYNC);
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    glfwSetKeyCallback(window, on_key);
    glfwSetMouseButtonCallback(window, on_mouse_button);
    glfwSetScrollCallback(window, on_scroll);

    #ifndef __APPLE__
        if (glewInit() != GLEW_OK) {
            return -1;
        }
    #endif

    if (db_init()) {
        return -1;
    }

    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LINE_SMOOTH);
    glLogicOp(GL_INVERT);
    glClearColor(0.53, 0.81, 0.92, 1.00);

    GLuint texture;
    glGenTextures(1, &texture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    load_png_texture("texture.png");

    GLuint block_program = load_program(
        "shaders/block_vertex.glsl", "shaders/block_fragment.glsl");
    GLuint matrix_loc = glGetUniformLocation(block_program, "matrix");
    GLuint camera_loc = glGetUniformLocation(block_program, "camera");
    GLuint sampler_loc = glGetUniformLocation(block_program, "sampler");
    GLuint timer_loc = glGetUniformLocation(block_program, "timer");
    GLuint position_loc = glGetAttribLocation(block_program, "position");
    GLuint normal_loc = glGetAttribLocation(block_program, "normal");
    GLuint uv_loc = glGetAttribLocation(block_program, "uv");

    GLuint line_program = load_program(
        "shaders/line_vertex.glsl", "shaders/line_fragment.glsl");
    GLuint line_matrix_loc = glGetUniformLocation(line_program, "matrix");
    GLuint line_position_loc = glGetAttribLocation(line_program, "position");

    GLuint item_position_buffer = 0;
    GLuint item_normal_buffer = 0;
    GLuint item_uv_buffer = 0;
    int previous_block_type = 0;

    Chunk chunks[MAX_CHUNKS];
    int chunk_count = 0;

    Player players[MAX_PLAYERS];
    int player_count = 0;

    FPS fps = {0, 0};
    float matrix[16];
    float x = (rand_double() - 0.5) * 10000;
    float z = (rand_double() - 0.5) * 10000;
    float y = 0;
    float dy = 0;
    float rx = 0;
    float ry = 0;
    double px = 0;
    double py = 0;

    int loaded = db_load_state(&x, &y, &z, &rx, &ry);
    ensure_chunks(chunks, &chunk_count,
        floorf(roundf(x) / CHUNK_SIZE),
        floorf(roundf(z) / CHUNK_SIZE), 1);
    if (!loaded) {
        y = highest_block(chunks, chunk_count, x, z) + 2;
    }

    glfwGetCursorPos(window, &px, &py);
    double previous = glfwGetTime();
    while (!glfwWindowShouldClose(window)) {
        update_fps(&fps, SHOW_FPS);
        double now = glfwGetTime();
        double dt = MIN(now - previous, 0.2);
        previous = now;

        if (exclusive && (px || py)) {
            double mx, my;
            glfwGetCursorPos(window, &mx, &my);
            float m = 0.0025;
            rx += (mx - px) * m;
            ry -= (my - py) * m;
            if (rx < 0) {
                rx += RADIANS(360);
            }
            if (rx >= RADIANS(360)){
                rx -= RADIANS(360);
            }
            ry = MAX(ry, -RADIANS(90));
            ry = MIN(ry, RADIANS(90));
            px = mx;
            py = my;
        }
        else {
            glfwGetCursorPos(window, &px, &py);
        }

        int sz = 0;
        int sx = 0;
        ortho = glfwGetKey(window, 'F');
        fov = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) ? 15.0 : 65.0;
        if (glfwGetKey(window, 'Q')) break;
        if (glfwGetKey(window, 'W')) sz--;
        if (glfwGetKey(window, 'S')) sz++;
        if (glfwGetKey(window, 'A')) sx--;
        if (glfwGetKey(window, 'D')) sx++;
        float m = dt * 1.0;
        if (glfwGetKey(window, GLFW_KEY_LEFT)) rx -= m;
        if (glfwGetKey(window, GLFW_KEY_RIGHT)) rx += m;
        if (glfwGetKey(window, GLFW_KEY_UP)) ry += m;
        if (glfwGetKey(window, GLFW_KEY_DOWN)) ry -= m;
        float vx, vy, vz;
        get_motion_vector(flying, sz, sx, rx, ry, &vx, &vy, &vz);
        if (glfwGetKey(window, GLFW_KEY_SPACE)) {
            if (flying) {
                vy = 1;
            }
            else if (dy == 0) {
                dy = 8;
            }
        }
        if (glfwGetKey(window, 'Z')) {
            vx = -1; vy = 0; vz = 0;
        }
        if (glfwGetKey(window, 'X')) {
            vx = 1; vy = 0; vz = 0;
        }
        if (glfwGetKey(window, 'C')) {
            vx = 0; vy = -1; vz = 0;
        }
        if (glfwGetKey(window, 'V')) {
            vx = 0; vy = 1; vz = 0;
        }
        if (glfwGetKey(window, 'B')) {
            vx = 0; vy = 0; vz = -1;
        }
        if (glfwGetKey(window, 'N')) {
            vx = 0; vy = 0; vz = 1;
        }
        float speed = flying ? 20 : 5;
        int step = 8;
        float ut = dt / step;
        vx = vx * ut * speed;
        vy = vy * ut * speed;
        vz = vz * ut * speed;
        for (int i = 0; i < step; i++) {
            if (flying) {
                dy = 0;
            }
            else {
                dy -= ut * 25;
                dy = MAX(dy, -250);
            }
            x += vx;
            y += vy + dy * ut;
            z += vz;
            if (collide(chunks, chunk_count, 2, &x, &y, &z)) {
                dy = 0;
            }
        }
        if (y < 0) {
            y = highest_block(chunks, chunk_count, x, z) + 2;
        }

        for (int i = 0; i < chunk_count; i++) {
            Chunk *chunk = chunks + i;
            chunk->dirty = 0;
        }

        if (left_click) {
            left_click = 0;
            int hx, hy, hz;
            int hw = hit_test(chunks, chunk_count, 0, x, y, z, rx, ry,
                &hx, &hy, &hz);
            if (hy > 0 && is_destructable(hw)) {
                set_block(chunks, chunk_count, hx, hy, hz, 0, 1);
            }
        }

        if (right_click) {
            right_click = 0;
            int hx, hy, hz;
            int hw = hit_test(chunks, chunk_count, 1, x, y, z, rx, ry,
                &hx, &hy, &hz);
            if (is_obstacle(hw)) {
                if (!player_intersects_block(2, x, y, z, hx, hy, hz)) {
                    set_block(chunks, chunk_count, hx, hy, hz, block_type, 1);
                }
            }
        }

        if (middle_click) {
            middle_click = 0;
            int hx, hy, hz;
            int hw = hit_test(chunks, chunk_count, 0, x, y, z, rx, ry,
                &hx, &hy, &hz);
            if (is_selectable(hw)) {
                block_type = hw;
            }
        }

        if (teleport) {
            teleport = 0;
            if (player_count) {
                int index = rand_int(player_count);
                Player *player = players + index;
                x = player->x;
                y = player->y;
                z = player->z;
                rx = player->rx;
                ry = player->ry;
                ensure_chunks(chunks, &chunk_count,
                    floorf(roundf(x) / CHUNK_SIZE),
                    floorf(roundf(z) / CHUNK_SIZE), 1);
            }
        }

        client_position(x, y, z, rx, ry);
        char buffer[RECV_BUFFER_SIZE];
        while (client_recv(buffer, RECV_BUFFER_SIZE)) {
            float ux, uy, uz, urx, ury;
            if (sscanf(buffer, "U,%*d,%f,%f,%f,%f,%f",
                &ux, &uy, &uz, &urx, &ury) == 5)
            {
                x = ux; y = uy; z = uz; rx = urx; ry = ury;
                ensure_chunks(chunks, &chunk_count,
                    floorf(roundf(x) / CHUNK_SIZE),
                    floorf(roundf(z) / CHUNK_SIZE), 1);
                y = highest_block(chunks, chunk_count, x, z) + 2;
            }
            int bx, by, bz, bw;
            if (sscanf(buffer, "B,%*d,%*d,%d,%d,%d,%d",
                &bx, &by, &bz, &bw) == 4)
            {
                set_block(chunks, chunk_count, bx, by, bz, bw, 0);
                if ((int)roundf(x) == bx && (int)roundf(z) == bz) {
                    y = highest_block(chunks, chunk_count, x, z) + 2;
                }
            }
            int pid;
            float px, py, pz, prx, pry;
            if (sscanf(buffer, "P,%d,%f,%f,%f,%f,%f",
                &pid, &px, &py, &pz, &prx, &pry) == 6)
            {
                Player *player = find_player(players, player_count, pid);
                if (!player && player_count < MAX_PLAYERS) {
                    player = players + player_count;
                    player_count++;
                    player->id = pid;
                    player->position_buffer = 0;
                    player->normal_buffer = 0;
                    player->uv_buffer = 0;
                    printf("%d other players are online\n", player_count);
                }
                if (player) {
                    update_player(player, px, py, pz, prx, pry);
                }
            }
            if (sscanf(buffer, "D,%d", &pid) == 1) {
                delete_player(players, &player_count, pid);
                printf("%d other players are online\n", player_count);
            }
        }

        for (int i = 0; i < chunk_count; i++) {
            Chunk *chunk = chunks + i;
            if (chunk->dirty) {
                update_chunk(chunk);
            }
        }

        int p = floorf(roundf(x) / CHUNK_SIZE);
        int q = floorf(roundf(z) / CHUNK_SIZE);
        ensure_chunks(chunks, &chunk_count, p, q, 0);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        update_matrix_3d(matrix, x, y, z, rx, ry);

        // render chunks
        glUseProgram(block_program);
        glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix);
        glUniform3f(camera_loc, x, y, z);
        glUniform1i(sampler_loc, 0);
        glUniform1f(timer_loc, glfwGetTime());
        for (int i = 0; i < chunk_count; i++) {
            Chunk *chunk = chunks + i;
            if (chunk_distance(chunk, p, q) > RENDER_CHUNK_RADIUS) {
                continue;
            }
            if (!chunk_visible(chunk, matrix)) {
                continue;
            }
            draw_chunk(chunk, position_loc, normal_loc, uv_loc);
        }

        // render players
        for (int i = 0; i < player_count; i++) {
            Player *player = players + i;
            draw_player(player, position_loc, normal_loc, uv_loc);
        }

        // render focused block wireframe
        int hx, hy, hz;
        int hw = hit_test(
            chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz);
        if (is_obstacle(hw)) {
            glUseProgram(line_program);
            glLineWidth(1);
            glEnable(GL_COLOR_LOGIC_OP);
            glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix);
            GLuint cube_buffer = make_cube_buffer(hx, hy, hz, 0.51);
            draw_lines(cube_buffer, line_position_loc, 3, 48);
            glDeleteBuffers(1, &cube_buffer);
            glDisable(GL_COLOR_LOGIC_OP);
        }

        update_matrix_2d(matrix);

        // render crosshairs
        glUseProgram(line_program);
        glLineWidth(4);
        glEnable(GL_COLOR_LOGIC_OP);
        glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix);
        GLuint line_buffer = make_line_buffer();
        draw_lines(line_buffer, line_position_loc, 2, 4);
        glDeleteBuffers(1, &line_buffer);
        glDisable(GL_COLOR_LOGIC_OP);

        // render selected item
        update_matrix_item(matrix);
        if (block_type != previous_block_type) {
            previous_block_type = block_type;
            make_single_cube(
                &item_position_buffer, &item_normal_buffer, &item_uv_buffer,
                0, 0, 0, 0.5, block_type);
        }
        glUseProgram(block_program);
        glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix);
        glUniform3f(camera_loc, 0, 0, 5);
        glUniform1i(sampler_loc, 0);
        glUniform1f(timer_loc, glfwGetTime());
        glDisable(GL_DEPTH_TEST);
        draw_single_cube(
            item_position_buffer, item_normal_buffer, item_uv_buffer,
            position_loc, normal_loc, uv_loc);
        glEnable(GL_DEPTH_TEST);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    client_stop();
    db_save_state(x, y, z, rx, ry);
    db_close();
    glfwTerminate();
    return 0;
}
Exemplo n.º 21
0
Arquivo: main.c Projeto: stuydw/matrix
int main() {

  //Basic Matrix Math
  struct matrix *a;
  struct matrix *b;

  a=new_matrix(4,4);
  b=new_matrix(4,2);
 
  
  printf("Identity matrix:\n");
  ident(a);
  print_matrix(a);
  
  b->m[0][0]=1;
  b->m[0][1]=2;
  b->m[1][0]=3;
  b->m[1][1]=4;
  b->m[2][0]=5;
  b->m[2][1]=6;
  b->m[3][0]=7;
  b->m[3][1]=8;
  
  printf("Matrix #2:\n");
  print_matrix(b);
  
  printf("Scalar Multiplication by 2:\n");
  scalar_mult(2, b);
  print_matrix(b);
  
  printf("New Matrix #1:\n");
  a->m[2][1]=3;
  a->m[0][3]=2;
  print_matrix(a);
  
  printf("Matrix Multiplication:\n");
  matrix_mult(a, b);
  print_matrix(b);

  printf("Adding points/edges:\n");
  struct matrix *d;
  d = new_matrix(3, 3);
  add_point(d, 200,400,70);
  add_point(d, 200,0,7);
  print_matrix(d);
  printf("\n");
  add_edge(d, 300,500,100,300,100,134);
  add_edge(d, 100,500,100,100,100,134);
  add_edge(d, 400,00,100,400,400,134);
  print_matrix(d);
  printf("\n");


  screen s;
  color c;

  c.red = 200;
  c.green = 100;
  c.blue = 250;

  int i, j;



  for( i=0; i<XRES; i++) 
    for ( j=0; j<YRES; j++) {
      plot( s, c, i, j);
    }

  c.red=0;
  c.green=200;
  c.blue=200;

  draw_lines(d, s, c);
 
  display( s );    
  save_ppm(s,  "image" );
  save_extension(s, "image.jpg");
  
}  
Exemplo n.º 22
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 255;
  g.green = 0;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
    double width, height, depth, radius, radius1, radius2;
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      //printf("BOX\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf",
	     &x, &y, &z, &width, &height, &depth);
      add_box(pm, x, y, z, width, height, depth);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "sphere", strlen(line)) == 0 ) {
      //printf("SPHERE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &radius);
      add_sphere(pm, x, y, radius, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "torus", strlen(line)) == 0 ) {
      //printf("TORUS\n");ds
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &radius1, &radius2 );
      add_torus(pm, x, y, radius1, radius2, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_lines(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
    }
    
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if (strncmp(line, "#", strlen(1)) == 0){
    }
    else {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
Exemplo n.º 23
0
Arquivo: main.c Projeto: stuydw/matrix
int main() {

  screen s;
  color c;

  // TEST: add_point
  struct matrix *a;
  a = (struct matrix *)new_matrix(3,3);
  add_point(a,1,0,2);

  // TEST: print_matrix
  printf("\nAdding one point to matrix A:\n");
  print_matrix(a);
  
  //  TEST: add_edge
  add_edge(a,4,5,3,2,2,2);
  printf("Adding an edge:\n");
  print_matrix(a);

  // TEST: scalar multiplication
  scalar_mult(5,a);
  printf("Scalar multiplication by 5:\n");
  print_matrix(a);
  scalar_mult(0.2,a);
  printf("Scalar multiplication by 0.2, returning to original matrix:\n");
  print_matrix(a);

  // TEST: matrix matrix multiplication
  struct matrix *b;
  b = (struct matrix *)new_matrix(3,1);
  add_point(b,1,2,3);
  printf("Matrix B:\n");
  print_matrix(b);
  matrix_mult(a,b);
  printf("Matrix multiplication of A with B:\n");
  print_matrix(a);

  // TEST: turning a matrix into the identity matrix
  struct matrix *d;
  d = (struct matrix *)new_matrix(3,3);
  add_edge(d,3,4,5,8.2,9.1,2);
  add_point(d,5,2,1);
  printf("Matrix C:\n");
  print_matrix(d);
  ident(d);
  printf("Matrix C after being made into the 3 x 3 identity matrix:\n");
  print_matrix(d);

  /*
  struct matrix *m;
  m = (struct matrix *)new_matrix(3,3);
  m->m[0][0] = 1;
  m->m[0][1] = 2;
  m->m[0][2] = 5;
  m->m[1][0] = 3;
  m->m[1][1] = 4;
  m->m[1][2] = 6;
  m->m[2][0] = 7;
  m->m[2][1] = 8;
  m->m[2][2] = 9;
  */

  /*
  struct matrix *n;
  n = (struct matrix *)new_matrix(3,4);
  n->m[0][0] = 1;
  n->m[1][0] = 2;
  n->m[2][0] = 1;
  n->m[0][1] = 1;
  n->m[1][1] = 0;
  n->m[2][1] = 1;
  n->m[0][2] = 1;
  n->m[1][2] = 2;
  n->m[2][2] = 1;
  n->m[0][3] = 1;
  n->m[1][3] = 1;
  n->m[2][3] = 0;
  */
 
  /*
  struct matrix *p;
  p = (struct matrix *)new_matrix(3,5);
  p->m[0][0] = 1;
  //print_matrix(p);
  //print_matrix(matrix_mult(m,n));
  */

  /*
  struct matrix *q;
  q = (struct matrix *)new_matrix(3,2);
  add_point(q,0,0,0);
  add_point(q,100,0,0);

  struct matrix *t;
  t = (struct matrix *)new_matrix(3,3);
  t->m[0][0] = cos(M_PI / 6);
  t->m[1][0] = sin(M_PI / 6);
  t->m[0][1] = -1 * sin(M_PI / 6);
  t->m[1][1] = cos(M_PI / 6);
  t->lastcol = 2;
  */
  /*add_edge(t,cos( M_PI / 6),sin( M_PI / 6), 0,
	   -1 * sin(M_PI / 6), cos(M_PI / 6), 0);
  */

  /*
  struct matrix *u;
  u = (struct matrix *)new_matrix(3,2);
  add_edge(u,0,0,0,100,0,0);
  
  int i;
  for(i = 0; i < 11; i++){
    matrix_mult(t,q);
    add_edge(u,q->m[0][0],q->m[1][0],q->m[2][0],
	     q->m[0][1],q->m[1][1],q->m[2][1]);}
  int j,k;
  float f;
  for(j = 0; j < u->rows; j++){
    for(k = 0; k < u->cols; k++){
      f = u->m[j][k];
      u->m[j][k] = (int)f + 250;}
  }

  print_matrix(t);
  printf("%f\n", cos(M_PI / 6));
  */

  /*
  struct matrix *r;
  r = (struct matrix *)new_matrix(3,2);
  r->m[0][0] = 0;
  r->m[0][1] = 0;
  r->m[1][0] = 50;
  r->m[1][1] = 50;

  print_matrix(m);
  matrix_mult(m,q);
  print_matrix(m);
  */

  /*
  int i,j;
  c.red = 0;
  c.green = 0;
  c.blue = 255;
  for(i = 0; i < XRES; i++){
    for(j = 0; j < YRES; j++){
      plot(s,c,i,j);
    }
  }
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  draw_lines(q,s,c);
  */

  /*
  int i, j;

  for( i=0; i<XRES; i++) 
    for ( j=0; j<YRES; j++) {

      c.red = random() % (MAX_COLOR + 1);
      c.green = random() % (MAX_COLOR + 1);
      c.blue = random() % (MAX_COLOR + 1);

      plot( s, c, i, j);
    }
  */

  c.red = 170;
  c.green = 240;
  c.blue = 30;

  int i,j;
  
  for(i = 0; i < XRES; i++){
    for(j = 0; j < YRES; j++){
      plot(s,c,i,j);
    }
  }

  // TEST: draw lines in edge matrix
  c.red = 0;
  c.green = 0;
  c.blue = 0;

  struct matrix *e;
  e = (struct matrix *)new_matrix(3,2);
  //add_point(e,0,0,0);
  //add_point(e,250,250,0);
  //draw_lines(e,s,c);
  for(i = 0; i < 25; i++){
    add_edge(e,250+5*i,250+5*i,0,500-5*i,250+5*i,0);
    add_edge(e,500-5*i,250+5*i,0,500-5*i,500-5*i,0);
    //add_edge(e,500-5*i,500-5*i,0,250+5*i,250+5*i,0);
  }
  for(i = 0; i < 25; i++){
    add_edge(e,5*i,5*i,0,250-5*i,5*i,0);
    add_edge(e,250-5*i,5*i,0,250-5*i,250-5*i,0);
    //add_edge(e,250-5*i,250-5*i,0,5*i,5*i,0);
  }
  for(i = 0; i < 25; i++){
    add_edge(e,125+5*i,125+5*i,0,125+5*i,375-5*i,0);
    add_edge(e,125+5*i,375-5*i,0,375-5*i,375-5*i,0);
    //add_edge(e,375-5*i,375-5*i,0,125+5*i,125+5*i,0);
  }
  add_edge(e,0,0,0,125,125,0);
  add_edge(e,375,375,0,500,500,0);
  draw_lines(e,s,c);

  display( s );    
  save_ppm(s,  "image" );
  save_extension(s, "image.jpg");  
  
}  
Exemplo n.º 24
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void
my_main (int polygons)
{

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;

  num_frames = 1;
  step = 0.05;

  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack ();
  tmp = new_matrix (4, 1000);
  clear_screen (t);

  for (i = 0; i < lastop; i++)
    {

      switch (op[i].opcode)
	{

	case SPHERE:
	  add_sphere (tmp, op[i].op.sphere.d[0],	//cx
		      op[i].op.sphere.d[1],	//cy
		      op[i].op.sphere.d[2],	//cz
		      op[i].op.sphere.r, step);
	  //apply the current top origin
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case TORUS:
	  add_torus (tmp, op[i].op.torus.d[0],	//cx
		     op[i].op.torus.d[1],	//cy
		     op[i].op.torus.d[2],	//cz
		     op[i].op.torus.r0, op[i].op.torus.r1, step);
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case BOX:
	  add_box (tmp, op[i].op.box.d0[0],
		   op[i].op.box.d0[1],
		   op[i].op.box.d0[2],
		   op[i].op.box.d1[0],
		   op[i].op.box.d1[1], op[i].op.box.d1[2]);
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case LINE:
	  add_edge (tmp, op[i].op.line.p0[0],
		    op[i].op.line.p0[1],
		    op[i].op.line.p0[1],
		    op[i].op.line.p1[0],
		    op[i].op.line.p1[1], op[i].op.line.p1[1]);
	  draw_lines (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case MOVE:
	  //get the factors
	  xval = op[i].op.move.d[0];
	  yval = op[i].op.move.d[1];
	  zval = op[i].op.move.d[2];

	  transform = make_translate (xval, yval, zval);
	  //multiply by the existing origin
	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case SCALE:
	  xval = op[i].op.scale.d[0];
	  yval = op[i].op.scale.d[1];
	  zval = op[i].op.scale.d[2];

	  transform = make_scale (xval, yval, zval);
	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case ROTATE:
	  xval = op[i].op.rotate.degrees * (M_PI / 180);

	  //get the axis
	  if (op[i].op.rotate.axis == 0)
	    transform = make_rotX (xval);
	  else if (op[i].op.rotate.axis == 1)
	    transform = make_rotY (xval);
	  else if (op[i].op.rotate.axis == 2)
	    transform = make_rotZ (xval);

	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case PUSH:
	  push (s);
	  break;
	case POP:
	  pop (s);
	  break;
	case SAVE:
	  save_extension (t, op[i].op.save.p->name);
	  break;
	case DISPLAY:
	  display (t);
	  break;
	}
    }

  free_stack (s);
  free_matrix (tmp);
  //free_matrix( transform );    
}
Exemplo n.º 25
0
int main(){
	vector<City> tsp; 
	city_parser("input-test2.txt", tsp); 	
	FILE * o_f;
	o_f = fopen("output-test2.txt", "w");



	City  data_set[6] = City();
 	data_set[0] = City(1,15,3); 
	data_set[1] = City(2,9,0);
	data_set[2] = City(3,2,8);
	data_set[3] = City(4,11,6);  
	data_set[4] = City(5,2,2);
	data_set[5] = City(6,8,9);

//	for(int i = 0; i< tsp.size(); ++i)
//		tsp[i].Print("before");

	vector<City> hull_points = quickHull(tsp); 

	int remove_index = 0;  

	for(int i =0; i <hull_points.size();i++){
		remove_index =  find(tsp.begin(), tsp.end(), hull_points[i]) -  tsp.begin();
		tsp.erase(tsp.begin() + remove_index);
	}	
//	for(int i = 0; i< tsp.size(); ++i)
//		tsp[i].Print("after"); 

//	for(int i =0; i <hull_points.size();++i)
//		hull_points[i].Print("tsp"); 

	hull_sort(hull_points, 0);
	vector<Line> hull_lines = draw_lines(hull_points, tsp);	


//	for(int i = 0; i <tsp.size();i++)
//		tsp[i].Print();

/*	for(int i =0; i<hull_points.size();i++)
		hull_points[i].Print();
*/

	vector< pair<Line, vector<City> > > closest_lines = determine_closest_lines(hull_lines, tsp);

//	for(int i = 0; i < tsp.size(); i++)	
//		closest_lines[i].second.push_back(tsp[i]);

	for(int i =0; i < closest_lines.size(); i++){
		int *lengths = new int[closest_lines[i].second.size()];
		 for(int j=0; j<closest_lines[i].second.size();j++){
                        Vec2 temp = closest_lines[i].first.points().Unit(); 
			lengths[j] = closest_lines[i].second[j].point().Dot(temp);
		 }
		quickCitySort(closest_lines[i].second, lengths, 0, closest_lines[i].second.size()-1); 
	}


//	for(int i =0; i<closest_lines.size();i++){
//		hull_points[i].Print(); 
//		for(int j=0; j <closest_lines[i].second.size(); j++){
//			closest_lines[i].second[j].Print(); 
//		}
//	}		
 
// compute our walk. 
	float tot_len = 0; 
	vector<City> walk; 
	for(int i = 0; i < closest_lines.size();++i){
		walk.push_back(hull_points[i]); 
		for(int j = 0; j < closest_lines[i].second.size(); ++j){
			walk.push_back(closest_lines[i].second[j]); 
		}
	}

//compute total length
	Vec2 temp;
	float tempy; 
	for(int i =0; i < walk.size(); i++){
		float a0 = walk[i].point().x();
		float b0 = walk[i-1].point().x();
		float a1 = walk[i].point().y();
		float b1 = walk[i-1].point().y();
		float dx = a0 - b0;
		float dy = a1 - b1;
		tempy = rint(sqrt(dx*dx + dy*dy));
		tot_len += tempy;
	}

//print solutionte total length
	fprintf(o_f, " %i\n",int(tot_len)); 
        for(int i =0; i < walk.size(); i++){
                        walk[i].Print("",o_f);
	}
//
	cout << "total path length is: " << tot_len <<" units of space" <<endl; 
 

return 0;
} 
Exemplo n.º 26
0
/*======== void parse_file () ==========
Inputs: char * filename
struct matrix * transform,
struct matrix * pm,
screen s
Returns:

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
Every command is a single character that takes up a line
Any command that requires arguments must have those arguments in the second line.
The commands are as follows:
l: add a line to the edge matrix -
takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
b: add a hermite cutve to the edge matrix -
takes 8 arguemnts (x0, y0, x1, y1, x2, y2, x3, y3)
h: add a bezier to the edge matrix -
takes 8 arguemnts (x0, y0, x1, y1, x2, y2, x3, y3)
c: add a circle to the edge matrix -
takes 3 arguemnts (cx, cy, r)
m: add a sphere to the edge matrix -
takes 3 arguemnts (cx, cy, r)
d: add a torus to the edge matrix -
takes 4 arguemnts (cx, cy, r1, r2)
p: add a rectangular prism to the edge matrix -
takes 6 arguemnts (x, y, z, width, height, depth)
w: clear the currnt edge matrix -
takes 0 arguments
i: set the transform matrix to the identity matrix -
s: create a scale matrix,
then multiply the transform matrix by the scale matrix -
takes 3 arguments (sx, sy, sz)
t: create a translation matrix,
then multiply the transform matrix by the translation matrix -
takes 3 arguments (tx, ty, tz)
x: create an x-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
y: create an y-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
z: create an z-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
a: apply the current transformation matrix to the
edge matrix
v: draw the lines of the edge matrix to the screen
display the screen
g: draw the lines of the edge matrix to the screen
save the screen to a file -
takes 1 argument (file name)
q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

03/08/12 16:22:10
jdyrlandweaver
====================*/
void parse_file ( char * filename,
                  struct matrix * transform,
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 0;
  g.green = 255;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 )
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    char c;
    double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4;
   
    c = line[0];

    switch (c) {
    case 'l':
      // printf("LINE!\n");
      fgets(line, 255, f);
      // printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      break;
    case 'p':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      break;
    case 'm':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 0.05);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'd':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 0.05);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'c':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'b':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'h':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 's':
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
      break;
    case 't':
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      // line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
      break;
    case 'x':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
      break;
    case 'y':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
      break;
    case 'z':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
      break;
    case 'i':
      ident(transform);
      break;
    case 'a':
      //printf("APPLY!\n");
      //print_matrix( transform );
      // print_matrix(pm);
      matrix_mult(transform, pm);
      break;
    case 'v':
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
      break;
    case 'w':
      pm->lastcol = 0;
      break;
    case 'g':
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_polygons(pm, s, g);
      save_extension(s, line);
      break;
    case 'q':
      return;
    case '#':
      break;
    default:
      printf("Invalid command\n");
      break;
    }
  }

  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
Exemplo n.º 27
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         sphere: add a sphere to the edge matrix - 
	    takes 3 arguemnts (cx, cy, r)
         torus: add a torus to the edge matrix - 
	    takes 4 arguemnts (cx, cy, r1, r2)
         box: add a rectangular prism to the edge matrix - 
	    takes 6 arguemnts (x, y, z, width, height, depth)
	 clear: clear the currnt edge matrix -
	    takes 0 arguments
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  struct stack * STACK = new_stack();
  
  g.red = 0;
  g.green = 255;
  g.blue = 0;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
   
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;

    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }
    
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    
    else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 10);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 10);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f); 
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "print", strlen(line)) == 0) {
      print_matrix( STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      //clear_screen(s);
      //draw_polygons(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if ( strncmp(line, "push", strlen(line)) == 0 ) {
      push( STACK ); //seg fault
      printf("Pushed\n");
    }
    else if ( strncmp(line, "pop", strlen(line)) == 0 ) {
      pop( STACK );
      printf("Popped\n");
    } 
    else if ( line[0] != '#' ) {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
Exemplo n.º 28
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

  int i;
  double step;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
      //simple cases
      //i realized that you already defined these constants  in another 
      //file after looking at this for 30 min -_-
    case POP:
      pop(s);
      break;
    case PUSH:
      push(s);
      break;

      //move,scale,rotate
    case MOVE:
      transform = make_translate(op[i].op.move.d[0],op[i].op.move.d[1],
				 op[i].op.move.d[2]);
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    case ROTATE:
      //there are 3 rotations possible, SO SWITCH-CEPTION!
      switch((int)op[i].op.rotate.axis)
	{
	case ROT_X:
	  transform = make_rotX(op[i].op.rotate.degrees);
	  break;
	case ROT_Y:
	  transform = make_rotY(op[i].op.rotate.degrees);
	  break;
	case ROT_Z:
	  transform = make_rotZ(op[i].op.rotate.degrees);
	  break;
	}
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    case SCALE:
      transform = make_scale(op[i].op.scale.d[0],op[i].op.scale.d[1],
			     op[i].op.scale.d[2]);
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    
      //box,sphere,torus
    case BOX:
      add_box(tmp,op[i].op.box.d0[0],op[i].op.box.d0[1],op[i].op.box.d0[2],
	      op[i].op.box.d1[0],op[i].op.box.d1[1],op[i].op.box.d1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
    case SPHERE:
      add_sphere(tmp,op[i].op.sphere.d[0],op[i].op.sphere.d[1],
		 op[i].op.sphere.d[2],op[i].op.sphere.r,0.01);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
    case TORUS:
      add_torus(tmp,op[i].op.torus.d[0],op[i].op.torus.d[1],op[i].op.torus.d[2],
		op[i].op.torus.r0,op[i].op.torus.r1,0.01);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
      
      //line
    case LINE:
      add_edge(tmp,op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2],
	       op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_lines(tmp,t,g);
      free_matrix(tmp);
      tmp=new_matrix(4,1000);//RESET
      break;
     
      //EVERYTIN else
    case SAVE:
      save_extension(t,op[i].op.save.p->name);
      break;
    case DISPLAY:
      display(t);
      break;
    default:
      break;
    }
  }
}
Exemplo n.º 29
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
      takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,bv    
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {


  double args [6];
  char command [25];

  color c;
  c.red = 255;
  c.blue = 0;
  c.green = 0;

  FILE *f = fopen(filename, "r");


  if(f == NULL) {
    printf("cannot find file\n");
    exit(1);
  }

  while(fgets(command, 25, f)){
    if(command[0] == 'l') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf %lf %lf %lf", &args[0], &args[1], &args[2], &args[3], &args[4], &args[5]);
      add_edge(pm, args[0], args[1], args[2], args[3], args[4], args[5]);
    }

    else if (command[0] == 'i') {
      ident(transform);
    }

    else if (command[0] == 's') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]);
      matrix_mult(make_scale(args[0], args[1], args[2]), transform);
    }

    else if (command[0] == 't') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]);
      struct matrix *m;
      m = make_translate(args[0], args[1], args[2]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'x') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotX(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'y') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotY(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'z') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotZ(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'a') {
      matrix_mult(transform, pm);
    }

    else if (command[0] == 'v') {
      draw_lines(pm, s, c);
    }

    else if (command[0] == 'g') {
      draw_lines(pm, s, c);
      fgets(command, 25, f);
      int i = strlen(command);
      command[i] = '\0';
      save_extension(s, command);
    }

    else if (command[0] == 'q') {
      exit(1);
    }
  }
  
}
Exemplo n.º 30
0
/*======== void my_main() ==========
Inputs:
Returns:

This is the main engine of the interpreter, it should
handle most of the commadns in mdl.

If frames is not present in the source (and therefore
num_frames is 1, then process_knobs should be called.

If frames is present, the enitre op array must be
applied frames time. At the end of each frame iteration
save the current screen to a file named the
provided basename plus a numeric string such that the
files will be listed in order, then clear the screen and
reset any other data structures that need it.

Important note: you cannot just name your files in
regular sequence, like pic0, pic1, pic2, pic3... if that
is done, then pic1, pic10, pic11... will come before pic2
and so on. In order to keep things clear, add leading 0s
to the numeric portion of the name. If you use sprintf,
you can use "%0xd" for this purpose. It will add at most
x 0s in front of a number, if needed, so if used correctly,
and x = 4, you would get numbers like 0001, 0002, 0011,
0487

05/17/12 09:41:35
jdyrlandweaver
====================*/
void my_main( int polygons ) {

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;

  struct vary_node **knobs;
  struct vary_node *vn;
  char frame_name[128];

  num_frames = 1;
  step = 5;
  f=0;

  g.red = 0;
  g.green = 255;
  g.blue = 255;

  first_pass();
  knobs = second_pass();
  for(f = 0;f<num_frames; f++){
    struct vary_node *curr = (struct vary_node *)calloc(1, sizeof(struct vary_node));
    curr = knobs[f];
    while(curr){
      printf("%s : %f\n", curr->name, curr->value);
      curr = curr->next;
    }
  }
  for(f=0;f<num_frames;f++){
    s = new_stack();
    tmp = new_matrix(4, 4);
    transform = new_matrix(4, 4);
    if(num_frames>1){
      vn = knobs[f];
      while(vn){
        SYMTAB *symb = lookup_symbol(vn->name);
        set_value(symb, vn->value);
        vn = vn->next;
      }
    }
    //print_knobs();
    for (i=0;i<lastop;i++) {
      SYMTAB *v;
      switch (op[i].opcode) {
        case SPHERE:
          //printf("Add sphere\n");
          add_sphere( tmp,op[i].op.sphere.d[0], //cx
            op[i].op.sphere.d[1],  //cy
            op[i].op.sphere.d[2],  //cz
            op[i].op.sphere.r,
            step);
          //apply the current top origin
          matrix_mult( s->data[ s->top ], tmp );
          draw_polygons( tmp, t, g );
          tmp->lastcol = 0;
          break;

        case TORUS:
          //printf("Add Torus\n");
          add_torus( tmp, op[i].op.torus.d[0], //cx
            op[i].op.torus.d[1],     //cy
            op[i].op.torus.d[2],    //cz
            op[i].op.torus.r0,
            op[i].op.torus.r1,
            step);
          matrix_mult( s->data[ s->top ], tmp );
          draw_polygons( tmp, t, g );
            tmp->lastcol = 0;
          break;

        case BOX:
          //printf("Add box\n");
          add_box( tmp, op[i].op.box.d0[0],
            op[i].op.box.d0[1],
            op[i].op.box.d0[2],
            op[i].op.box.d1[0],
            op[i].op.box.d1[1],
            op[i].op.box.d1[2]);
          matrix_mult( s->data[ s->top ], tmp );
          draw_polygons( tmp, t, g );
            tmp->lastcol = 0;
          break;

        case LINE:
          //printf("Line\n");
          add_edge( tmp, op[i].op.line.p0[0],
            op[i].op.line.p0[1],
            op[i].op.line.p0[1],
            op[i].op.line.p1[0],
            op[i].op.line.p1[1],
            op[i].op.line.p1[1]);
          draw_lines( tmp, t, g );
            tmp->lastcol = 0;
          break;

        case MOVE:
          //printf("Move\n");
          //get the factors
          xval = op[i].op.move.d[0];
          yval =  op[i].op.move.d[1];
          zval = op[i].op.move.d[2];
          v = op[i].op.move.p;
          if(v){
            xval = xval * v->s.value;
            yval = yval * v->s.value;
            zval = zval * v->s.value;
          }
          //printf("x: %f y: %f z: %f\n", xval, yval, zval);

          transform = make_translate( xval, yval, zval );
          //multiply by the existing origin
          matrix_mult( s->data[ s->top ], transform );
          //put the new matrix on the top
          copy_matrix( transform, s->data[ s->top ] );
          free_matrix( transform );
          break;

        case SCALE:
          //printf("Scale\n");
          xval = op[i].op.scale.d[0];
          yval = op[i].op.scale.d[1];
          zval = op[i].op.scale.d[2];

          v = op[i].op.scale.p;
          if(v){
            //printf("I'm not null Scale\n");
            xval *= v->s.value;
            yval *= v->s.value;
            zval *= v->s.value;
          }

          transform = make_scale( xval, yval, zval );
          matrix_mult( s->data[ s->top ], transform );
          //put the new matrix on the top
          copy_matrix( transform, s->data[ s->top ] );
          free_matrix( transform );
          break;

        case ROTATE:
          //printf("Rotate\n");
          xval = op[i].op.rotate.degrees * ( M_PI / 180 );

          v = op[i].op.rotate.p;
          if(v){
            xval *= v->s.value;
          }
          //get the axis
          if ( op[i].op.rotate.axis == 0 )
            transform = make_rotX( xval );
          else if ( op[i].op.rotate.axis == 1 )
            transform = make_rotY( xval );
          else if ( op[i].op.rotate.axis == 2 )
            transform = make_rotZ( xval );

          matrix_mult( s->data[ s->top ], transform );
          //put the new matrix on the top
          copy_matrix( transform, s->data[ s->top ] );
          free_matrix( transform );
          break;

        case PUSH:
          //printf("Push\n");
          push( s );
          break;
        case POP:
          //printf("Pop\n");
          pop( s );
          break;
        case SAVE:
          //printf("Save\n");
          save_extension( t, op[i].op.save.p->name );
          break;
        case DISPLAY:
          //printf("Display\n");
          display( t );
          break;
      }
    }
    if(num_frames>1){
      sprintf (frame_name, "%s%03d.png", name, f);
      save_extension(t, frame_name);
    }
    clear_screen(t);
    free_stack(s);
    free_matrix(tmp);
  }

  //free_stack( s );
  //free_matrix( tmp );
  //free_matrix( transform );
}