//----------------------------------------------------------------------------- void MyGlDraw(void) { clearScreen(); //************************************************************************* // Chame aqui as funções do mygl.h //************************************************************************* objData = new objLoader(); // cria o objeto que carrega o modelo objData->load("models/monkey_head2.obj"); // a carga do modelo é indicada atraves do nome do arquivo. // Neste caso, deve ser sempre do tipo OBJ. /**************creating the list of all vertexs**********************/ std::vector<glm::vec4> vertex_list; std::vector<Vertex> my_list; obj_vector* vector; obj_face* faces; //std::cout<<"object vertex list"<<std::endl; for (int i = 0; i < objData->vertexCount; ++i) { vector = objData->vertexList[i]; glm::vec4 aux(vector->e[0], vector->e[1], vector->e[2], 1.0); //PrintVec4(aux); vertex_list.push_back(aux); } /******OBJECT SPACE TO UNIVERSE SPACE******/ glm::mat4 Scale = glm::mat4(2.0); Scale[3].w = 1.0; glm::mat4 Trans = glm::mat4(1.0); glm::vec4 v(1.0,1.0, 1.0, 1.0); Trans[3] = v; glm::mat4 Rotate = glm::mat4(1.0); Rotate[0].x = cos(angle +=1 * PI/180.0); Rotate[2].x = sin(angle +=1 * PI/180.0); Rotate[0].z = - sin(angle +=1 * PI/180.0); Rotate[2].z = cos(angle +=1 * PI/180.0); Rotate[3].w = 1.0; glm::mat4 M_Model = Rotate * Scale; /******OBJECT SPACE TO UNIVERSE SPACE******/ /******UNIVERSE SPACE TO CAMERA SPACE******/ glm::vec3 camera_pos(0.0,0.0,5.0); glm::vec3 look_at(0.0,0.0,0.0); glm::vec3 camera_up(0.0,1.0,0.0); glm::vec3 camera_dir = look_at - camera_pos; glm::vec3 z_camera = -normalize(camera_dir); glm::vec3 x_camera = normalize(cross(camera_up, z_camera)); glm::vec3 y_camera = normalize(cross(z_camera, x_camera)); glm::vec4 homog(0.0,0.0,0.0,1.0); glm::mat4 B = glm::mat4(1.0); B[0]= glm::vec4 (x_camera,0.0); B[1]= glm::vec4 (y_camera,0.0); B[2]= glm::vec4 (z_camera,0.0); B[3]=homog; glm::mat4 trans = glm::mat4(1.0); trans[3] = glm::vec4 (-camera_pos, 1.0); glm::mat4 M_View = transpose(B) * trans; glm::mat4 Model_View = M_View * M_Model; /******UNIVERSE SPACE TO CAMERA SPACE******/ /******CAMERA SPACE TO PROJECTION SPACE (CLIPPING)******/ double d=1.0; glm::mat4 M_Projection = glm::mat4(1.0); M_Projection[2] = glm::vec4(0.0, 0.0, 1.0, -1.0/d); M_Projection[3] = glm::vec4(0.0, 0.0, d, 0.0); glm::mat4 M_ModelViewProjection = M_Projection * Model_View; /******CAMERA SPACE TO PROJECTION SPACE (CLIPPING)******/ /******PROJECTION SPACE (CLIPPING) TO CANONICAL SPACE******/ //std::cout<<"PRINTING"<<std::endl; for (int i = 0; i < objData->vertexCount; ++i) { vertex_list[i]=M_ModelViewProjection*vertex_list[i]; vertex_list[i].x=vertex_list[i].x/vertex_list[i].w; vertex_list[i].y=vertex_list[i].y/vertex_list[i].w; vertex_list[i].z=vertex_list[i].z/vertex_list[i].w; vertex_list[i].w=vertex_list[i].w/vertex_list[i].w; } /******PROJECTION SPACE (CLIPPING) TO CANONICAL SPACE******/ /******CANONICAL SPACE TO SCREEN SPACE******/ glm::mat4 Translation_Screen = glm::mat4(1.0); Translation_Screen[3] = glm::vec4((IMAGE_WIDTH -1)/2, (IMAGE_HEIGHT -1)/2, 0.0, 1.0); glm::mat4 Scale_Screen = glm::mat4(1.0); Scale_Screen[0].x = IMAGE_WIDTH/2; Scale_Screen[1].y = IMAGE_HEIGHT/2; glm::mat4 InvertY_Screen = glm::mat4(1.0); InvertY_Screen[1].y = -1.0; glm::mat4 Final_Matrix = glm::mat4(1.0); //Final_Matrix = InvertY_Screen * Scale_Screen * Translation_Screen; Final_Matrix = Translation_Screen * Scale_Screen * InvertY_Screen; //PrintMatrix(Final_Matrix); for (int i = 0; i < objData->vertexCount; ++i) { vertex_list[i] = Final_Matrix * vertex_list[i]; //PrintVec4(vertex_list[i]); } /******CANONICAL SPACE TO SCREEN SPACE******/ //std::cout<<"object vertex list"<<std::endl; for (int i = 0; i < objData->vertexCount; ++i) { Vertex tmp; tmp.setX(round(vertex_list[i].x)); tmp.setY(round(vertex_list[i].y)); tmp.setZ(round(vertex_list[i].z)); tmp.setW(round(vertex_list[i].w)); my_list.push_back(tmp); } for (int i = 0; i < objData->faceCount; ++i) { faces = objData->faceList[i]; my_list[faces->vertex_index[0]].DrawTriangle(my_list[faces->vertex_index[1]], my_list[faces->vertex_index[2]]); } }
// Runs the interactive loop and all file transfers. // Returns: exit status int main(void) { // Used to control DNS resolution requests: struct addrinfo hints; memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; // Bind to an ephemeral network port: struct addrinfo *server = NULL; int sfd = openudp(0); if(sfd < 0) handle_error("bind()"); // Allocate (small) space to store user input: char *buf = malloc(1); size_t cap = 1; char *cmd; // First word of buf size_t len; // Length of cmd // Main input loop, which normally only breaks upon a GFO: do { // Keep prompting until the user brings us back something good: do { printf("%s", SHL_PS1); readin(&buf, &cap); } while(homog(buf, ' ')); // Cleave off the command (first word): cmd = strtok(buf, " "); len = strlen(cmd); if(strncmp(cmd, CMD_CON, len) == 0) { // Read the arguments: const char *hostname = strtok(NULL, " "); const char *tmp = strtok(NULL, " "); in_port_t port = PORT_UNPRIVILEGED; if(tmp) port = atoi(tmp); // Ensure a hostname or IP has been provided: if(!hostname) { usage(CMD_CON, "hostname", "port"); continue; } // Avoid leaking any existing address: if(server) { freeaddrinfo(server); server = NULL; } // Try to resolve the requested hostname: if(getaddrinfo(hostname, NULL, &hints, &server)) { fprintf(stderr, "Unable to resolve hostname\n"); freeaddrinfo(server); server = NULL; continue; } ((struct sockaddr_in *)server->ai_addr)->sin_port = htons(port); } else if(strncmp(cmd, CMD_GET, len) == 0 || strncmp(cmd, CMD_PUT, len) == 0) { bool putting = strncmp(cmd, CMD_PUT, 1) == 0; // Ensure we're already connected to a server: if(!server) { noconn(cmd); continue; } // Make sure we were given a path argument: char *pathname = strtok(NULL, ""); if(!pathname) { usage(putting ? CMD_PUT : CMD_GET, "pathname", NULL); continue; } // Since basename() might modify pathname, copy it: char filename[strlen(pathname)+1]; memcpy(filename, pathname, sizeof filename); int fd; if(putting) { // Try opening the file for reading: if((fd = open(pathname, O_RDONLY)) < 0) { fprintf(stderr, "local: Unable to read specified file\n"); continue; } // Send a request and record the port used to acknowledge: struct sockaddr_in dest_addr; sendreq(sfd, basename(filename), OPC_WRQ, server->ai_addr); uint8_t *rmtack = recvpkta(sfd, &dest_addr); if(iserr(rmtack)) { fprintf(stderr, "remote: %s\n", strerr(rmtack)); free(rmtack); continue; } free(rmtack); // Transmit the file: sendfile(sfd, fd, &dest_addr); } else // getting { // Try opening a file of that name for writing: if((fd = open(basename(filename), O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { fprintf(stderr, "local: Unable to create the new file\n"); continue; } // Send a request and await the incoming file: sendreq(sfd, pathname, OPC_RRQ, server->ai_addr); const char *res = recvfile(sfd, fd); if(res) { fprintf(stderr, "remote: %s\n", res); close(fd); fd = -1; unlink(basename(filename)); } } if(fd >= 0) close(fd); } else if(strncmp(cmd, CMD_HLP, len) == 0) { printf("Commands may be abbreviated. Commands are:\n\n"); printf("%s\t\tconnect to remote tftp\n", CMD_CON); printf("%s\t\tsend file\n", CMD_PUT); printf("%s\t\treceive file\n", CMD_GET); printf("%s\t\texit tftp\n", CMD_GFO); printf("%s\t\tprint help information\n", CMD_HLP); } else if(strncmp(cmd, CMD_GFO, len) != 0) { fprintf(stderr, "%s: unknown directive\n", cmd); fprintf(stderr, "Try ? for help.\n"); } } while(strncmp(cmd, CMD_GFO, len) != 0); free(buf); if(server) freeaddrinfo(server); return 0; }