| |
---|
| | return new Vec(c[0], c[1], c[2]); |
---|
| | } |
---|
| | }; |
---|
| | |
---|
| | |
---|
| | |
---|
| | class Poly { |
---|
| | private: |
---|
| | Vec** initpoints; |
---|
| | //Vec* initrotation; |
---|
| |
---|
| | Vec s (0,0,0); |
---|
| | for (int i = 0; i < size; i++) { |
---|
| | s = s+*initpoints[i]; |
---|
| | } |
---|
| | Vec* r = (s / size).clone(); |
---|
| | Vec* r = (s / ((float)size)).clone(); |
---|
| | return r; |
---|
| | } |
---|
| | |
---|
| | /* |
---|
| |
---|
| | glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, d); |
---|
| | } |
---|
| | }; |
---|
| | |
---|
| | |
---|
| | class Model { |
---|
| | private: |
---|
| | Poly** faces; |
---|
| | int size; |
---|
| | |
---|
| | Vec* position; |
---|
| | Vec* rotation; |
---|
| | |
---|
| | public: |
---|
| | Model(Poly** _faces, int _size) { |
---|
| | size = _size; |
---|
| | faces = _faces; |
---|
| | this->setCenter(this->calcCenter()); |
---|
| | rotation = new Vec(0,0,0); |
---|
| | } |
---|
| | |
---|
| | ~Model() { |
---|
| | delete position; |
---|
| | delete rotation; |
---|
| | } |
---|
| | |
---|
| | void setCenter(Vec* c) { |
---|
| | delete position; |
---|
| | position = c->clone(); |
---|
| | } |
---|
| | |
---|
| | Vec* calcCenter() { |
---|
| | Vec s(0,0,0); |
---|
| | |
---|
| | for ( int i = 0; i < size; i++ ) { |
---|
| | s = s+*faces[i]->calcCenter(); |
---|
| | } |
---|
| | |
---|
| | Vec* r = (s/((float)size)).clone(); |
---|
| | return r; |
---|
| | } |
---|
| | |
---|
| | void rotate(Vec rot) { |
---|
| | this->rotation->add(rot); |
---|
| | for ( int i = 0; i < size; i++ ) { |
---|
| | faces[i]->rotate(rot); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | void draw() { |
---|
| | for ( int i = 0; i <size; i++ ) { |
---|
| | faces[i]->draw(); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | void addFace(Poly* face) { |
---|
| | Poly** t = faces; |
---|
| | size++; |
---|
| | for ( int i = 0; i < size-1; i++ ) { |
---|
| | faces[i] = t[i]; |
---|
| | } |
---|
| | faces[size-1] = face; |
---|
| | } |
---|
| | }; |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| |
---|
| | int MouseX, MouseY, OldMouseX, OldMouseY; |
---|
| | bool MouseButtonRight, MouseButtonLeft; |
---|
| | bool done = false; // Quit application when true |
---|
| | |
---|
| | /* |
---|
| | Tri** importModel(char* fname) { |
---|
| | |
---|
| | Model* importModel(char* fname) { |
---|
| | |
---|
| | FILE *fp; |
---|
| | char lbuf[200]; |
---|
| | char* buf2; |
---|
| | int numpoints = 0; |
---|
| | int numfaces = 0; |
---|
| | float[3] tpf = {0.0,0.0,0.0}; |
---|
| | float tpf[3] = {0.0f, 0.0f, 0.0f}; |
---|
| | int* tp = NULL; |
---|
| | int tps = 0; |
---|
| | Vec** points = NULL; |
---|
| | Tri** faces = NULL; |
---|
| | int cp = 0; |
---|
| | Vec** points; |
---|
| | Vec** tpoints; |
---|
| | Model* mm = new Model(NULL, 0); |
---|
| | Poly* tpp; |
---|
| | |
---|
| | |
---|
| | fp = fopen(fname, "r"); |
---|
| | |
---|
| | if ( fp == NULL ) { |
---|
| | printf("Error reading file %s\n", fname); |
---|
| | return; |
---|
| | return NULL; |
---|
| | } |
---|
| | |
---|
| | while( fgets(lbuf, sizeof(lbuf), fp) != NULL ) { |
---|
| | buf2 = strtok(lbuf, " "); |
---|
| | if ( buf2 == "v" ) { |
---|
| | numpoints++; |
---|
| | points = realloc(points, numpoints*sizeof(Vec*)); |
---|
| | tpf[0] = atof(strtok(str, null)); |
---|
| | tpf[1] = atof(strtok(str, null)); |
---|
| | tpf[2] = atof(strtok(str, null)); |
---|
| | tpf[0] = (float)atof(strtok(lbuf, NULL)); |
---|
| | tpf[1] = (float)atof(strtok(lbuf, NULL)); |
---|
| | tpf[2] = (float)atof(strtok(lbuf, NULL)); |
---|
| | points[numpoints-1] = new Vec(tpf[0], tpf[1], tpf[2]); |
---|
| | } |
---|
| | if ( buf2 == "f" ) { |
---|
| | numfaces++; |
---|
| | faces = realloc(faces, numfaces*sizeof(Tri*)); |
---|
| | tps = 0; |
---|
| | while ( (buf2 = strtok(str, null)) != NULL ) { |
---|
| | while ( (buf2 = strtok(lbuf, NULL)) != NULL ) { |
---|
| | cp = atoi(buf2); |
---|
| | tpoints[tps] = points[cp]->clone(); |
---|
| | tps++; |
---|
| | tp = realloc(tp, tps*(sizeof(int)); |
---|
| | tp[tps-1] = atoi(buf2); |
---|
| | } |
---|
| | faces[numfaces-1] = new Tri(); |
---|
| | tpp = new Poly(tpoints, tps); |
---|
| | mm->addFace(tpp); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |
---|
| | fclose(fp); |
---|
| | return faces; |
---|
| | return mm; |
---|
| | } |
---|
| | */ |
---|
| | |
---|
| | |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| | /////////////////////////////////////////////////////////////////////////// |
---|
| |
---|
| | new Poly(pp3, 4), |
---|
| | new Poly(pp4, 4), |
---|
| | new Poly(pp5, 4), |
---|
| | }; |
---|
| | |
---|
| | Model* myModel = importModel("teapot.obj"); |
---|
| | |
---|
| | //Vec* pp [4] = {&p0, &p3, &p2, &p1}; |
---|
| | //Poly poly0 (pp,4); |
---|
| | |
---|
| |
---|
| | // Draw a rotating triangle: |
---|
| | glRotatef(deltay, 0.0f, 1.0f, 0.0f); // Rotation around the Y axis |
---|
| | glRotatef(deltaz, 1.0f, 0.0f, 0.0f); |
---|
| | |
---|
| | for (int i = 0; i < SURFACE_COUNT; i++) { |
---|
| | /* for (int i = 0; i < SURFACE_COUNT; i++) { |
---|
| | polys[i]->draw(); |
---|
| | polys[i]->rotate(Vec(0.0, 0.01, 0.01)); |
---|
| | } |
---|
| | polys[i]->rotate(Vec(0.0f, 0.01f, 0.01f)); |
---|
| | }*/ |
---|
| | |
---|
| | //poly0.draw(); |
---|
| | //poly0.rotate(Vec(0.0, 0.01, 0.01)); |
---|
| | |
---|
| | light0.draw(); |
---|
| | light0.trace(); |
---|
| | |
---|
| | myModel->draw(); |
---|
| | //////////////// |
---|
| | // Check events: |
---|
| | SDL_Event event; |
---|
| | while (SDL_PollEvent(&event)) |
---|
| |
---|
| | |