void SwglLine(GLdouble x1, GLdouble y1, GLdouble z1, GLdouble x2, GLdouble y2, GLdouble z2) { //copy to homogenous coordinate GLdouble h1[4]={x1, y1, z1, 1.0}; GLdouble h2[4]={x2, y2, z2, 1.0}; GLdouble w1[4]={x1, y1, 0, 1.0}; //window coordinate GLdouble w2[4]={x2, y2, 0, 1.0}; //implement the opengl pipeline here swTransformation(h1, w1); swTransformation(h2, w2); ////draw the 2D line //glBegin(GL_LINES); // //glColor3fv(colors[index1]); // glVertex2f(w1[0], w1[1]); // //glColor3fv(colors[index2]); // glVertex2f(w2[0], w2[1]); //glEnd(); //implement switch(DRAWTYPE) { case 0: { writepixel(w1[0], w1[1], 0, 1, 0, 0); //writepixel(w1[0], w1[1], 1, 0, 0); writepixel(w2[0], w2[1], 0, 0, 1, 0); //writepixel(w1[0], w1[1], 1, 0, 0); } break; case 1: case 2: case 3: { GLdouble col[4]; glGetDoublev(GL_CURRENT_COLOR, col); BresenhamLine(w1[0], w1[1], w1[2], w2[0], w2[1], w2[2], col[0], col[1], col[2]); } break; } }
inline void SwglTri(GLdouble x1, GLdouble y1, GLdouble z1, GLdouble x2, GLdouble y2, GLdouble z2, GLdouble x3, GLdouble y3, GLdouble z3, GLdouble nx1=1, GLdouble ny1=0, GLdouble nz1=0, GLdouble nx2=1, GLdouble ny2=0, GLdouble nz2=0, GLdouble nx3=1, GLdouble ny3=0, GLdouble nz3=0, GLdouble r1=1, GLdouble g1=1, GLdouble b1=1, GLdouble r2=1, GLdouble g2=1, GLdouble b2=1, GLdouble r3=1, GLdouble g3=1, GLdouble b3=1) { //copy to homogenous coordinate GLdouble h1[4]={x1, y1, z1, 1.0}; GLdouble h2[4]={x2, y2, z2, 1.0}; GLdouble h3[4]={x3, y3, z3, 1.0}; //window coordinate GLdouble w1[4]={x1, y1, 0, 1.0}; GLdouble w2[4]={x2, y2, 0, 1.0}; GLdouble w3[4]={x3, y3, 0, 1.0}; //implement the opengl pipeline here swTransformation(h1, w1); swTransformation(h2, w2); swTransformation(h3, w3); switch(DRAWTYPE) { case 0: { //copy to homogenous coordinate GLdouble h1[4]={x1, y1, z1, 1.0}; GLdouble h2[4]={x2, y2, z2, 1.0}; GLdouble h3[4]={x3, y3, z3, 1.0}; //window coordinate GLdouble w1[4]={x1, y1, 0, 1.0}; GLdouble w2[4]={x2, y2, 0, 1.0}; GLdouble w3[4]={x3, y3, 0, 1.0}; //implement the opengl pipeline here swTransformation(h1, w1); swTransformation(h2, w2); swTransformation(h3, w3); writepixel(w1[0], w1[1], 0, r1, g1, b1);//writepixel(w1[0], w1[1], r1, g1, b1) writepixel(w2[0], w2[1], 0, r2, g2, b2); writepixel(w3[0], w3[1], 0, r3, g3, b3); } break; case 1: { //copy to homogenous coordinate GLdouble h1[4]={x1, y1, z1, 1.0}; GLdouble h2[4]={x2, y2, z2, 1.0}; GLdouble h3[4]={x3, y3, z3, 1.0}; //window coordinate GLdouble w1[4]={x1, y1, 0, 1.0}; GLdouble w2[4]={x2, y2, 0, 1.0}; GLdouble w3[4]={x3, y3, 0, 1.0}; //implement the opengl pipeline here swTransformation(h1, w1); swTransformation(h2, w2); swTransformation(h3, w3); swTriangle(w1[0], w1[1], w1[2], w2[0], w2[1], w2[2], w3[0], w3[1], w3[2], r1, g1, b1); } break; case 2: { swTriangleG(x1, y1, z1, x2, y2, z2, x3, y3, z3, nx1, ny1, nz1, nx2, ny2, nz2, nx3, ny3, nz3, r1, g1, b1, r2, g2, b2, r3, g3, b3); } break; case 3: { swTriangleP(x1, y1, z1, x2, y2, z2, x3, y3, z3, nx1, ny1, nz1, nx2, ny2, nz2, nx3, ny3, nz3, r1, g1, b1, r2, g2, b2, r3, g3, b3); } break; } }
int main(int argc, char **argv) { FILE *in, *out; bmp_header_t bmp_header; dib_header_t dib_header; rgbquad_t *palette; int i, j, k, add, bufsize, offset; char buf[3], palbuf[1024]; char *outbuf; if(argc < 3) { fprintf(stderr, "USAGE: %s <INPUT> <OUTPUT>\n", argv[0]); return -1; } printf("Opening input file... "); if((in = fopen(argv[1], "rb")) == NULL) { printf("ERROR: Could not open input file for reading.\n"); return -1; } printf("OK\n"); bmp_header = readbmpheader(in); dib_header = readdibheader(in); printf("Checking magic number... "); if(strncmp(bmp_header.magic, "BM", 2)) { printf("ERROR: Not a bitmap file.\n"); fclose(in); return -1; } printf("OK\n"); printf("Checking header size... "); printf("%d. ", dib_header.size); if(dib_header.size != 40) { printf("ERROR: Not Windows V3\n"); fclose(in); return -1; } printf("OK\n"); printf("Checking dimensions... "); printf("%dx%dx%dbbp. ", dib_header.width, dib_header.height, dib_header.bpp[0]); if((dib_header.width != REQ_WIDTH) || (dib_header.height != REQ_HEIGHT) || (dib_header.bpp[0] != REQ_BPP)) { printf("ERROR. %dx%dx%d required.\n", REQ_WIDTH, REQ_HEIGHT, REQ_BPP); fclose(in); return -1; } printf("OK\n\n"); if(REQ_BPP == 8) { printf("Reading palette... "); fseek(in, sizeof(bmp_header_t) + dib_header.size, SEEK_SET); fread(palbuf, 1024, 1, in); palette = readpalette(palbuf); printf("OK\n"); } else palette = NULL; printf("Opening output file... "); if((out = fopen(argv[2], "wb")) == NULL) { printf("ERROR: Could not open output file for writing.\n"); fclose(in); return -1; } printf("OK\n"); fseek(in, bmp_header.offset, SEEK_SET); printf("Filling output buffer... "); bufsize = REQ_HEIGHT * REQ_WIDTH * (REQ_BPP / 8); add = (REQ_BPP == 8)?1024:0; if((outbuf = (char*)malloc(bufsize + add)) == NULL) { fprintf(stderr, "ERROR: malloc(%d) failed.\n", bufsize); fclose(out); fclose(in); return -1; } for(i = 0; i < add; i++) outbuf[i] = palbuf[i]; for(i = 0; i < REQ_HEIGHT; i++) { offset = (REQ_BPP / 8) * REQ_WIDTH * (REQ_HEIGHT - 1 - i) + add; for(j = 0; j < REQ_WIDTH; j++) { fread(buf, (REQ_BPP / 8), 1, in); writepixel(buf, palette, offset + (j * REQ_BPP / 8), outbuf); } for(k = 0; k < ((REQ_WIDTH * REQ_BPP / 8) % 4); k++) fgetc(in); } printf("Done.\nWriting file... "); fwrite(outbuf, bufsize + add, 1, out); printf("Done.\n"); if(palette) free(palette); free(outbuf); fclose(out); fclose(in); return 0; }