#include #include #include #include #include #include "SDL/SDL_gfxPrimitives.h" #include "tools/lib/hsmw_gfx.h" using namespace std; Edge* p_edges = NULL; VectorR3* p_points = NULL; long NO_POINTS = 0, NO_LINES = 0; SDL_Surface *screen; // our screen to draw on void ScaleModel(double factor, bool flip_x, bool flip_y, bool flip_z) { // find out extends VectorR3 min, max; min = VectorR3(DBL_MAX, DBL_MAX, DBL_MAX); max = VectorR3(DBL_MIN, DBL_MIN, DBL_MIN); for(long i = 0; i < NO_POINTS; i++) { for(int comp = 0; comp < 3; comp++) { if(min[comp] > p_points[i][comp]) min[comp] = p_points[i][comp]; if(max[comp] < p_points[i][comp]) max[comp] = p_points[i][comp]; } // for } // for double extend = -1; for(int comp = 0; comp < 3; comp++) { if(extend < (max[comp] - min[comp])) extend = max[comp] - min[comp]; } // for for(long i = 0; i < NO_POINTS; i++) { p_points[i] *= factor/extend; if(flip_x == true) p_points[i][0] *= -1.0; if(flip_y == true) p_points[i][1] *= -1.0; if(flip_z == true) p_points[i][2] *= -1.0; } // for } // ScaleModel void DrawLine(int start_x, int start_y, int end_x, int end_y) { aalineColor(screen, start_x+screen->w/2, start_y+screen->h/2, end_x+screen->w/2, end_y+screen->h/2, 0xFFFFFFFF); } // DrawLine void ClearScreen() { bzero(screen->pixels, screen->pitch*screen->h); // clear screen buffer } // ClearScreen void UpdateScreen() { SDL_UnlockSurface(screen); SDL_UpdateRect(screen, 0, 0, screen->w, screen->h); SDL_LockSurface(screen); } // UpdateScreen // horizontale vertikale // Mausbewegung Mausbewegung VectorR3 Rotate(VectorR3 point, double alfa, double beta) { VectorR3 result; VectorR3 X(cos(alfa), 0.0, -sin(alfa)), // gedrehte Koordinatenachsen bzw. der Drehung um y Y( 0.0, 1.0, 0.0), Z(sin(alfa), 0.0, cos(alfa)); result = point[0]*X + point[1]*Y + point[2]*Z; /* result[0] = cos(alfa)*point[0] + 0.0*point[1] + sin(alfa)*point[2]; result[1] = 0.0*point[0] + 1.0*point[1] + 0.0*point[2]; result[2] = -sin(alfa)*point[0] + 0*point[1] + cos(alfa)*point[2]; */ VectorR3 rot_final; // nicht in den originalen Punkt schreiben X = VectorR3(1, 0, 0); // gedrehte Koordinatenachsen bzw. der Drehung um x Y = VectorR3(0.0, cos(beta), sin(beta)); Z = VectorR3(0, -sin(beta), cos(beta)); rot_final = result[0]*X + result[1]*Y + result[2]*Z; return rot_final; } // Rotate VectorR3 Perspective(VectorR3 v, float focal_length) { VectorR3 result; const double epsilon = 0.00001; if(fabs(v[2]) < epsilon) return v; result[0] = (v[0]*focal_length)/v[2]; result[1] = (v[1]*focal_length)/v[2]; return result; } // Perspective bool DepthTest(VectorR3& start_point, VectorR3& end_point) { return true; } // DepthTest // horizontale vertikale // Mausbewegung Mausbewegung void DisplayModel(float angle_1, float angle_2, float depth) { VectorR3 rot_points[NO_POINTS]; VectorR3 start_point, end_point; int start_index, end_index; ClearScreen(); for(int point = 0; point < NO_POINTS; point++) { // rotate points here... using Rotate() rot_points[point] = Rotate(p_points[point], angle_1, angle_2); } // for double focal_length = 300; // (dt.: Brennweite) for(int line = 0; line < NO_LINES; line++) { // first line is called p_edges[0], second p_edges[1], ... // the start-index of the first line ist called p_edges[0].line_start // the end-index of the first line ist called p_edges[0].line_end // what is an index? It points into the array of 3D-points called rot_points // rot_points is of type VectorR3. Use rot_points[0][0] for x-component of // first point in array, p_points[0][1] for y-component... // draw the 3D modell using only x- and y-components int index_start = p_edges[line].line_start; int index_end = p_edges[line].line_end; VectorR3 start_vert = Perspective(rot_points[index_start], focal_length); VectorR3 end_vert = Perspective(rot_points[index_end], focal_length); DrawLine(start_vert[0], start_vert[1], end_vert[0], end_vert[1]); } // for UpdateScreen(); } // DisplayModel int main(int argc, char** argv) { SDL_Event event; bool l_button_down = false, r_button_down = false; float cursor_x, cursor_y, depth; bool redraw = true; ReadObjFile(argv[1], &p_points, &p_edges, NULL, &NO_POINTS, &NO_LINES, NULL); ScaleModel(350.0, false, true, false); SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(512, 512, 24, 0); cursor_x = screen->w/2; cursor_y = screen->h/2; depth = 300; for(;;) { if(redraw == true) { DisplayModel((cursor_x/screen->w - 0.5)*2*M_PI, (cursor_y/screen->h - 0.5)*2*M_PI, depth); redraw = false; } // if SDL_WaitEvent(&event); switch(event.type) { case SDL_MOUSEMOTION: if(l_button_down) { cursor_x = event.motion.x; cursor_y = event.motion.y; redraw = true; } // if if(r_button_down) { depth = event.motion.y; redraw = true; } // if break; case SDL_MOUSEBUTTONDOWN: redraw = true; if(event.button.button == SDL_BUTTON_LEFT) { l_button_down = true; cursor_x = event.motion.x; cursor_y = event.motion.y; } // if if(event.button.button == SDL_BUTTON_RIGHT) { depth = event.motion.y; r_button_down = true; } // if switch(event.button.button) { case SDL_BUTTON_WHEELUP: depth -= 30; redraw = true; break; case SDL_BUTTON_WHEELDOWN: depth += 30; redraw = true; break; } // switch break; case SDL_MOUSEBUTTONUP: if(event.button.button == SDL_BUTTON_LEFT) l_button_down = false; if(event.button.button == SDL_BUTTON_RIGHT) r_button_down = false; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case 27: case 'q': case 'Q': goto exit_app; default: break; } // switch } // switch } // for exit_app: delete[] p_edges; delete[] p_points; SDL_UnlockSurface(screen); SDL_FreeSurface(screen); exit(0); } // main