void initPebbleRigidity(int n) { int i, j; //Assigning the number of agents NUM = n; /*Init the graph structure*/ graph = calloc(NUM, sizeof (int*)); if (!graph) { printf("Error in memory allocation for graph\n"); exit(1); } for (i = 0; i < NUM; i++) { graph[i] = calloc(NUM, sizeof (int)); if (!graph[i]) { printf("Error in memory allocation for graph\n"); exit(1); } } //for (i = 0; i < NUM; i++) { // for (j = 0; j < NUM; j++) { // graph[i][j] = Gnew[i][j]; // } //} init_vertices(); init_rigidity_check(); }
unsigned vertex(double* x, double* y) { if (tolerance_ == 0.0) return geom_.vertex(x, y); if (status_ == initial) init_vertices(); return output_vertex(x, y); }
void sph_model::init_arrays(int n) { glGenBuffers(1, &vertices); glGenBuffers(16, elements); glBindBuffer(GL_ARRAY_BUFFER, vertices); init_vertices(n); for (int b = 0; b < 16; ++b) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements[b]); init_elements(n, b); } count = 4 * n * n; }
unsigned vertex(double * x, double * y) { if (offset_ == 0.0) { return geom_.vertex(x, y); } if (status_ == initial) { init_vertices(); } if (pos_ >= vertices_.size()) { return SEG_END; } pre_ = (pos_ ? cur_ : pre_first_); cur_ = vertices_.at(pos_++); if (pos_ == vertices_.size()) { return output_vertex(x, y); } double const check_dist = offset_ * threshold_; double const check_dist2 = check_dist * check_dist; double t = 1.0; double vt, ut; for (size_t i = pos_; i+1 < vertices_.size(); ++i) { //break; // uncomment this to see all the curls vertex2d const& u0 = vertices_[i]; vertex2d const& u1 = vertices_[i+1]; double const dx = u0.x - cur_.x; double const dy = u0.y - cur_.y; if (dx*dx + dy*dy > check_dist2) { break; } if (!intersection(pre_, cur_, &vt, u0, u1, &ut)) { continue; } if (vt < 0.0 || vt > t || ut < 0.0 || ut > 1.0) { continue; } t = vt; pos_ = i+1; } cur_.x = pre_.x + t * (cur_.x - pre_.x); cur_.y = pre_.y + t * (cur_.y - pre_.y); return output_vertex(x, y); }
int main(int argc, char* argv[]) { GLFWwindow window; double t, dt_total, t_old; if (!glfwInit()) { fprintf(stderr, "GLFW initialization failed\n"); exit(EXIT_FAILURE); } window = glfwOpenWindow(640, 480, GLFW_WINDOWED, "Wave Simulation", NULL); if (!window) { fprintf(stderr, "Could not open window\n"); exit(EXIT_FAILURE); } glfwSwapInterval(1); // Keyboard handler glfwSetKeyCallback(key_callback); glfwSetInputMode(window, GLFW_KEY_REPEAT, GL_TRUE); // Window resize handler glfwSetWindowSizeCallback(window_resize_callback); glfwSetMouseButtonCallback(mouse_button_callback); glfwSetMousePosCallback(mouse_position_callback); glfwSetScrollCallback(scroll_callback); // Initialize OpenGL init_opengl(); // Initialize simulation init_vertices(); init_grid(); adjust_grid(); // Initialize timer t_old = glfwGetTime() - 0.01; while (running) { t = glfwGetTime(); dt_total = t - t_old; t_old = t; // Safety - iterate if dt_total is too large while (dt_total > 0.f) { // Select iteration time step dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total; dt_total -= dt; // Calculate wave propagation calc_grid(); } // Compute height of each vertex adjust_grid(); // Draw wave grid to OpenGL display draw_scene(); glfwPollEvents(); // Still running? running = running && glfwIsWindow(window); } exit(EXIT_SUCCESS); }
void ram(void) { int i; frame = 0; struct vertex2d projected[NUM_VERTICES]; float matrix_projection[16]; init_matrix_projection_fov(matrix_projection, DEG2RAD(2), 0.1, 50, 1); while (true) { lcdFill(COLOR_BACKGROUND); float mat_a[16]; float mat_b[16]; float mat_c[16]; float matrix_worldviewprojection[16]; init_vertices(vertices); // Calculate worldviewprojection matrix by concatenating the // following operations: // - rotate y axis // - rotate x axis // - translate // - project float rad = (float)frame * 0.015f; init_matrix_rotation_y(mat_a, rad); init_matrix_rotation_x(mat_b, -rad*0.8); matrix_multiplication(mat_a, mat_b, mat_c); init_matrix_translation(mat_a, 0, -2, 11.5f); matrix_multiplication(mat_c, mat_a, mat_b); matrix_multiplication(mat_b, matrix_projection, matrix_worldviewprojection); // Transform vertices for (i=0; i<NUM_VERTICES; i++) { struct vertex transformed; transform_vertex(&vertices[i], &transformed, matrix_worldviewprojection); if (transformed.w > -0.0001) projected[i].x = VERTEX_BEHIND_CAMERA; else { projected[i].x = (int)(transformed.x / transformed.w) + SCREEN_WIDTH / 2; projected[i].y = (int)(transformed.y / transformed.w) + SCREEN_HEIGHT / 2; } } // Draw line grid struct vertex2d from, to; for (int x=0; x<TESSELATION; x++) for (int y=0; y<TESSELATION; y++) { if (x < (TESSELATION - 1)) { from = projected[x + y * TESSELATION]; to = projected[x + y * TESSELATION + 1]; line(from.x, from.y, to.x, to.y, COLOR_LINE); } if (y < (TESSELATION - 1)) { from = projected[x + y * TESSELATION]; to = projected[x + y * TESSELATION + TESSELATION]; line(from.x, from.y, to.x, to.y, COLOR_LINE); } } lcdDisplay(); rad += 0.015; frame++; int key = getInputRaw(); if (key == BTN_ENTER) break; } }
int main(int argc, char* argv[]) { GLFWwindow* window; double t, dt_total, t_old; int width, height; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(640, 480, "Wave Simulation", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwSetWindowSizeCallback(window, window_size_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetScrollCallback(window, scroll_callback); glfwMakeContextCurrent(window); glfwSwapInterval(1); glfwGetWindowSize(window, &width, &height); window_size_callback(window, width, height); // Initialize OpenGL init_opengl(); // Initialize simulation init_vertices(); init_grid(); adjust_grid(); // Initialize timer t_old = glfwGetTime() - 0.01; while (!glfwWindowShouldClose(window)) { t = glfwGetTime(); dt_total = t - t_old; t_old = t; // Safety - iterate if dt_total is too large while (dt_total > 0.f) { // Select iteration time step dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total; dt_total -= dt; // Calculate wave propagation calc_grid(); } // Compute height of each vertex adjust_grid(); // Draw wave grid to OpenGL display draw_scene(window); glfwPollEvents(); } exit(EXIT_SUCCESS); }