void binarization(SDL_Surface *s) { int x, y,currentpix; Uint8 r, g, b; int myArray[s->w][s->h]; for(x = 0; x < s->w; x++) { for(y = 0; y < s->h; y++) { SDL_GetRGB(getpix(s, x, y), s->format, &r, &g, &b); currentpix=(r+g+b)/3; if(currentpix<180) myArray[x][y] =1; else myArray[x][y]=0; } } for(x = 0; x < s->w; x++) { for(y = 0; y < s->h; y++) { if(myArray[x][y]==1) {putpix(s, x, y, SDL_MapRGB(s->format, 0, 0, 0));} else {putpix(s, x, y, SDL_MapRGB(s->format, 255, 255, 255));} } } }
void grayscale(SDL_Surface *s) { int x, y; Uint8 r, g, b, c; for(x = 0; x < s->w; x++) { for(y = 0; y < s->h; y++) { SDL_GetRGB(getpix(s, x, y), s->format, &r, &g, &b); c = r * .3 + g * .6 + b * .1; putpix(s, x, y, SDL_MapRGB(s->format, c, c, c)); } } }
int drawscrn (struct chip8_state *s,SDL_Surface *screen) { int k,l; for(k=0;k<32;k++) { for(l=0;l<64;l++) { putpix(s->scrn[l][k],l*10,k*10,screen); } } SDL_Flip(screen); return 0; }
void tick_frame() { for(int y = 0; y<(vdisp); y++) { for(int x = 0; x<(hdisp+1); x++) { u8 chr = RAM::RAM[0xB0000 + (x + (y*(hdisp+1))<<1)]; u8 attr = RAM::RAM[0xB0001 + (x + (y*(hdisp+1))<<1)]; u8 fg[3], bg[3]; bool blink = false; bool underline = false; switch(attr) { case 0x00: case 0x08: case 0x80: case 0x88: { for(int i = 0; i<3; i++) { fg[i] = 0; bg[i] = 0; } break; } case 0x70: { for(int i = 0; i<3; i++) { fg[i] = 0; bg[i] = 192; } break; } case 0x78: { for(int i = 0; i<3; i++) { fg[i] = 128; bg[i] = 192; } break; } case 0xF0: { for(int i = 0; i<3; i++) { fg[i] = 128; bg[i] = 192; } blink = true; break; } case 0xF8: { for(int i = 0; i<3; i++) { fg[i] = 128; bg[i] = 192; } blink = true; break; } default: { for(int i = 0; i<3; i++) { fg[i] = 128; bg[i] = 0; } if(attr & 2) underline = true; if(attr & 8) { fg[0] = fg[1] = fg[2] = 192; } if(attr & 0x80) blink = true; break; } } for (int iy = 0; iy<(maxscan+1); iy++) { if (iy==maxscan && underline) { for (int i = 0; i<9; i++) { putpix((x*9)+i,(y*(maxscan+1))+iy,192,192,192); } } int tmp = iy % (maxscan+1); u8 chrdata; if (tmp & 8) chrdata = ROM[(0x800 | (tmp & 7)) + (chr*8)]; else chrdata = ROM[(chr*8) + tmp]; if (chrdata & 0x80) putpix(x*9,(y*(maxscan+1))+iy,fg[0],fg[1],fg[2]); if (blink && (framecount & 0x10)) putpix(x*9,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x40) putpix((x*9)+1,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+1,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x20) putpix((x*9)+2,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+2,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x10) putpix((x*9)+3,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+3,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x08) putpix((x*9)+4,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+4,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x04) putpix((x*9)+5,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+5,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x02) putpix((x*9)+6,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+6,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); if (chrdata & 0x01) putpix((x*9)+7,(y*(maxscan+1))+iy,fg[2],fg[2],fg[2]); if (blink && (framecount & 0x10)) putpix((x*9)+7,(y*(maxscan+1))+iy,bg[2],bg[2],bg[2]); putpix((x*9)+8,(y*(maxscan+1))+iy,0,0,0); } } } framecount++; if(framecount == 0x1F) framecount = 0; #ifdef USE_SDL SDL_BlitSurface(mdscr, nullptr, INTERFACE::screen, NULL); #else memcpy(INTERFACE::screen, mdscr, 720*350*4); #endif }
void render_triangle (struct msscene *ms, struct surface *phn, double fine_pixel, long interpolate, double tcen[3], double trad, double tvertices[3][3], double tnormals[4][3], double tvalues[3], int check, int comp, int trihue, int atm) { int k, point_clipped, x, y, z; int hue, shade, inner, shape; double input_opacity; double xf, yf, zf; double xmin, xmax, ymin, ymax, xp, yp; double xinc, yinc, zp, value; double norvect[3], pnt[3], horvect[3]; double a0, a1, a2, b0, b1, b2, c0, c1, c2; double b1c1, b0c0, a0c0, a1c1, xpc0, ypc1; double denom, numa, numb; double fa, fb, fc; double opacity; struct object_scheme *scheme; scheme = phn -> scheme; inner = 0; xmin = tcen[0] - trad; xmax = tcen[0] + trad; ymin = tcen[1] - trad; ymax = tcen[1] + trad; /* extra kludge factor of 0.7 */ xinc = 0.7 * fine_pixel; yinc = 0.7 * fine_pixel; if (xinc <= 0.0) xinc = EPSILON; if (yinc <= 0.0) yinc = EPSILON; a0 = tvertices[0][0]; a1 = tvertices[0][1]; a2 = tvertices[0][2]; b0 = tvertices[1][0]; b1 = tvertices[1][1]; b2 = tvertices[1][2]; c0 = tvertices[2][0]; c1 = tvertices[2][1]; c2 = tvertices[2][2]; denom = (a0 - c0) * (b1 - c1) - (a1 - c1) * (b0 - c0); if (denom == 0.0) return; b1c1 = b1 - c1; b0c0 = b0 - c0; a0c0 = a0 - c0; a1c1 = a1 - c1; for (xp = xmin; xp <= xmax; xp += xinc) { xpc0 = xp - c0; for (yp = ymin; yp <= ymax; yp += yinc) { ypc1 = yp - c1; numa = b1c1 * xpc0 - b0c0 * ypc1; fa = numa / denom; if (fa <= 0.0) continue; if (fa >= 1.0) continue; numb = a0c0 * ypc1 - a1c1 * xpc0; fb = numb / denom; if (fb <= 0.0) continue; if (fb >= 1.0) continue; fc = 1.0 - fa - fb; if (fc <= 0.0) continue; if (fc >= 1.0) continue; zp = fa * a2 + fb * b2 + fc * c2; pnt[0] = xp; pnt[1] = yp; pnt[2] = zp; if (check) { point_clipped = ms -> clipping && phn -> clipping && clipped (ms, pnt); if (point_clipped) continue; } if (interpolate) { for (k = 0; k < 3; k++) norvect[k] = fa * tnormals[0][k] + fb * tnormals[1][k] + fc * tnormals[2][k]; } else { for (k = 0; k < 3; k++) norvect[k] = tnormals[3][k]; } normalize(norvect); if (norvect[2] < 0.0) { if (!phn -> clipping) continue; if (!ms -> clipping) continue; /* reverse direction of vector */ inner = 1; for (k = 0; k < 3; k++) norvect[k] *= (-1.0); for (k = 0; k < 3; k++) horvect[k] = norvect[k]; horvect[2] = 0.0; /* move inner side in one & half pixel widths */ xf = xp + phn -> surface_thickness * ms -> pixel_width * horvect[0]; yf = yp + phn -> surface_thickness * ms -> pixel_width * horvect[1]; zf = zp + phn -> surface_thickness * ms -> pixel_width * horvect[2]; } else { inner = 0; xf = xp; yf = yp; zf = zp; } /* convert from surface to screen coordinates */ x = ftoi (ms, xf, 0); y = ftoi (ms, yf, 1); z = ftoi (ms, zf, 2); value = fa * tvalues[0] + fb * tvalues[1] + fc * tvalues[2]; shape = 1; /* hue from color scheme */ hue = determine_hue (ms -> table, scheme, atm, comp, shape, inner, trihue, value); if (error()) return; /* get shade (intensity) */ shade = detsh(ms, norvect, z); if (error()) return; input_opacity = 1 - trihue % 2; opacity = detopac (atm, inner, comp, shape, phn -> scheme, input_opacity); /* put pixel into depth buffer */ putpix (ms, phn, inner, shade, hue, opacity, x, y, z); if (error()) return; } } }
void render_leaf (struct msscene *ms, struct surface *current_surface, double fine_pixel, struct leaf *lf) { int i, k, npoint, x, y, z, shade, hue, atm; int inner, point_clipped, point_if; int shape, comp, input_hue; double input_opacity; double opacity; double norvect[3], horvect[3], pnt[3]; static double *points = (double *) NULL; struct circle *cir; cir = lf -> cir; if (cir -> radius <= 0.0) { ms -> n_missing_leaves++; return; } /* check leaf circle versus window */ for (k = 0; k < 3; k++) { if (cir -> center[k] + cir -> radius < ms -> window[0][k]) return; if (cir -> center[k] - cir -> radius > ms -> window[1][k]) return; } /* subdivide leaf into points (pixels) */ npoint = subdivide_leaf (fine_pixel, lf, &points); if (npoint < 2) { ms -> n_missing_leaves++; return; } /* for each point, compute normal vector, shade, hue */ for (i = 0; i < npoint; i++) { for (k = 0; k < 3; k++) pnt[k] = *(points+3*i+k); for (k = 0; k < 3; k++) norvect[k] = (*(points+3*i+k) - lf -> focus[k]); normalize (norvect); if (lf -> shape != CONVEX) reverse (norvect); /* check every point ? */ if (lf -> cep) { /* check accessibility for problem leaf */ point_if = point_in_face (points + 3 * i, lf -> fac, 1); if (error()) return; if (!point_if) continue; } if (lf -> clip_ep) { /* check clipping for problem leaf */ point_clipped = current_surface -> clipping && clipped (ms, points + 3 * i); if (point_clipped) continue; } inner = 0; /* if no clipping, do not render backward facing pixels */ if (norvect[2] <= 0.0) { if (!(current_surface -> clipping)) continue; /* reverse direction of vector */ inner = 1; for (k = 0; k < 3; k++) norvect[k] *= (-1.0); for (k = 0; k < 3; k++) horvect[k] = norvect[k]; horvect[2] = 0.0; /* move inner side in one & half pixel widths */ for (k = 0; k < 3; k++) *(points+3*i+k) += current_surface -> surface_thickness * ms -> pixel_width * horvect[k]; } /* convert from surface to screen coordinates */ x = ftoi (ms, *(points + 3 * i), 0); y = ftoi (ms, *(points + 3 * i + 1), 1); z = ftoi (ms, *(points + 3 * i + 2), 2); /* closest atom of (1, 2, 3) atoms to point */ atm = closest (current_surface, lf, points + 3 * i); if (error()) return; if (atm <= 0) continue; shape = lf -> shape; if (lf -> type == CYLINDER) shape = 2; if (shape < 1) shape = 1; if (shape > 3) shape = 3; comp = lf -> comp; input_hue = lf -> input_hue; input_opacity = 1 - input_hue % 2; opacity = detopac (atm, inner, comp, shape, current_surface -> scheme, input_opacity); if (error()) return; /* hue from color scheme */ hue = determine_hue (ms -> table, current_surface -> scheme, atm, comp, shape, inner, input_hue, 0.0); if (error()) return; /* get shade (intensity) */ shade = detsh(ms, norvect, z); if (error()) return; /* put pixel into depth buffer */ putpix (ms, current_surface, inner, shade, hue, opacity, x, y, z); if (error()) return; } /* free points of leaf */ free_doubles (points, 0, VERTS); }