void render_scene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { glPushMatrix(); glTranslatef(70.0, 0, 70.0); glTranslatef(i*10.0, 0, j * 10.0); glCallList(snowman_display_list); glPopMatrix(); } render_height_map(); render_wall(); render_sky(); render_tree(); glutSwapBuffers(); }
static void render_tree(qnode *root) { if (root == NULL) { return; } SDL_SetRenderDrawColor(gCore.renderer, 0x0, 0xff, 0x0, 0xff); SDL_Rect rect = { .x = root->x, .y = root->y, .w = root->w, .h = root->h }; SDL_RenderDrawRect(gCore.renderer, &rect); for (int i = 0; i < 4; i++) { render_tree(root->children[i]); } } bool core_initialize(int width, int height, int maxDepth, int maxObjectsPerNode) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("[Error] Could not initialize SDL: %s\n", SDL_GetError()); return false; } gCore.window = SDL_CreateWindow("MC202 - Particle Collider", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); if (gCore.window == NULL) { printf("[Error] Could not create window: %s\n", SDL_GetError()); return false; } gCore.renderer = SDL_CreateRenderer(gCore.window, -1, SDL_RENDERER_SOFTWARE); if (gCore.renderer == NULL) { printf("[Error] Could not create a renderer for window: %s\n", SDL_GetError()); return false; } if (!IMG_Init(IMG_INIT_PNG)) { printf("[Error] Could not initialize SDL Image: %s\n", IMG_GetError()); } gCore.entities = list_create(); gCore.width = width; gCore.height = height; gCore.maxDepth = maxDepth; gCore.maxObjectsPerNode = maxObjectsPerNode; gCore.maxIterations = -1; return true; } void core_insertObjects(list *initialObjects) { list_concatenate(gCore.entities, initialObjects); }
void core_render() { SDL_Surface *windowSurface = SDL_GetWindowSurface(gCore.window); SDL_Rect stretchRect; render_tree(gCore.tree->root); node *cur = gCore.entities->head; while (cur != NULL) { stretchRect.x = cur->b->x; stretchRect.y = cur->b->y; stretchRect.w = 2 * cur->b->radius; stretchRect.h = 2 * cur->b->radius; SDL_BlitScaled(cur->b->texture, NULL, windowSurface, &stretchRect); cur = cur->next; } }
void render_tree(FILE *f, struct SkipQuadtree<Point>::Node *tree, size_t depth) { double x1 = tree->mid[0] - tree->radius; double x2 = tree->mid[0] + tree->radius; double y1 = tree->mid[1] - tree->radius; double y2 = tree->mid[1] + tree->radius; //square if (depth < 1) fprintf(f, "4 setlinewidth\n"); else if (depth < 2) fprintf(f, "3 setlinewidth\n"); else if (depth < 4) fprintf(f, "2 setlinewidth\n"); else fprintf(f, "1 setlinewidth\n"); fprintf(f, "%.0f %.0f %.0f %.0f node-bounds\n", x1, x2, y1, y2); if (!tree->nodes) { fprintf(f, "%.0f %.0f draw-point\n", (*tree->pt)[0], (*tree->pt)[1]); } else { for (int i = 0; i < 4; ++i) { if (tree->nodes[i]) render_tree(f, tree->nodes[i], depth+1); } } }
int main(int argc, char **argv) { if (argc != 3) { printf("usage: render_tree <input> <output>\n"); exit(1); } int pt_count; FILE *f = fopen(argv[1], "r"); if (!f) { printf("error: could not open points file: %s\n", argv[1]); exit(1); } fscanf(f, "%d", &pt_count); if (pt_count < 0) { printf("error: invalid point count %d\n", pt_count); exit(1); } Point *pts = new Point[pt_count]; double x, y; for (int i = 0; i < pt_count; ++i) { fscanf(f, "%lf, %lf", &x, &y); pts[i][0] = x; pts[i][1] = y; } fclose(f); SkipQuadtree<Point> sqt(2, pts, pt_count); printf("completed building tree...\n "); f = fopen(argv[2], "w"); if (!f) { printf("error: could not open output file: %s\n", argv[2]); exit(1); } fprintf(f, "%%%%!PS-Adobe-2.0\n"); fprintf(f, "%%%%Pages: %d\n", sqt.levels.size()); fprintf(f, "/page-begin {\n"); fprintf(f, "gsave\n"); fprintf(f, "} def\n"); fprintf(f, "/page-end {\n"); fprintf(f, " grestore\n"); fprintf(f, " showpage\n"); fprintf(f, "} def\n"); //define point function for later fprintf(f, "/draw-point {\n"); fprintf(f, " /y exch def\n"); fprintf(f, " /x exch def\n"); fprintf(f, " gsave\n"); fprintf(f, " newpath\n"); fprintf(f, " 0.5 0.5 0.7 setrgbcolor\n"); fprintf(f, " x y 2 0 360 arc\n"); fprintf(f, " closepath\n"); fprintf(f, " fill\n"); fprintf(f, " newpath\n"); fprintf(f, " 0.4 setgray\n"); fprintf(f, " x y 2 0 360 arc\n"); fprintf(f, " closepath\n"); fprintf(f, " stroke\n"); fprintf(f, " grestore\n"); fprintf(f, "} def\n"); //node bounding box fprintf(f, "/node-bounds {\n"); fprintf(f, " /y2 exch def\n"); fprintf(f, " /y1 exch def\n"); fprintf(f, " /x2 exch def\n"); fprintf(f, " /x1 exch def\n"); fprintf(f, " gsave\n"); fprintf(f, " 0.7 setgray\n"); fprintf(f, " newpath\n"); fprintf(f, " x2 y2 moveto\n"); fprintf(f, " x1 y2 lineto\n"); fprintf(f, " x1 y1 lineto\n"); fprintf(f, " x2 y1 lineto\n"); fprintf(f, " closepath\n"); fprintf(f, " stroke \n"); fprintf(f, " grestore\n"); fprintf(f, "} def\n"); for (size_t i = 0; i < sqt.levels.size(); ++i) { fprintf(f, "%%%%Page: %d\n", i + 1); fprintf(f, "page-begin\n"); render_tree(f, sqt.levels[i], 0); fprintf(f, "page-end\n"); } fclose(f); }