static void draw_wireframe_cylinder (gint startx, gint starty, gint pw, gint ph) { GimpVector3 p[2*8], a, axis, scale; gint n = 0, i; gdouble cx1, cy1, cx2, cy2; gfloat m[16], l, angle; /* Compute wireframe points */ /* ======================== */ init_compute (); scale = mapvals.scale; gimp_vector3_mul (&scale, 0.5); l = mapvals.cylinder_length / 2.0; angle = 0; gimp_vector3_set (&axis, 0.0, 1.0, 0.0); for (i = 0; i < 8; i++) { rotatemat (angle, &axis, m); gimp_vector3_set (&a, mapvals.cylinder_radius, 0.0, 0.0); vecmulmat (&p[i], &a, m); p[i+8] = p[i]; p[i].y += l; p[i+8].y -= l; angle += 360.0 / 8.0; } /* Rotate and translate points */ /* =========================== */ for (i = 0; i < 16; i++) { vecmulmat (&a, &p[i], rotmat); gimp_vector3_add (&p[i], &a, &mapvals.position); } /* Draw the box */ /* ============ */ cx1 = (gdouble) startx; cy1 = (gdouble) starty; cx2 = cx1 + (gdouble) pw; cy2 = cy1 + (gdouble) ph; for (i = 0; i < 7; i++) { n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[i],p[i+1]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[i+8],p[i+9]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[i],p[i+8]); } n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[7],p[0]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[15],p[8]); /* Mark end of lines */ /* ================= */ linetab[n].x1 = -1; }
static void draw_wireframe_box (gint startx, gint starty, gint pw, gint ph) { GimpVector3 p[8], tmp, scale; gint n = 0, i; gdouble cx1, cy1, cx2, cy2; /* Compute wireframe points */ /* ======================== */ init_compute (); scale = mapvals.scale; gimp_vector3_mul (&scale, 0.5); gimp_vector3_set (&p[0], -scale.x, -scale.y, scale.z); gimp_vector3_set (&p[1], scale.x, -scale.y, scale.z); gimp_vector3_set (&p[2], scale.x, scale.y, scale.z); gimp_vector3_set (&p[3], -scale.x, scale.y, scale.z); gimp_vector3_set (&p[4], -scale.x, -scale.y, -scale.z); gimp_vector3_set (&p[5], scale.x, -scale.y, -scale.z); gimp_vector3_set (&p[6], scale.x, scale.y, -scale.z); gimp_vector3_set (&p[7], -scale.x, scale.y, -scale.z); /* Rotate and translate points */ /* =========================== */ for (i = 0; i < 8; i++) { vecmulmat (&tmp, &p[i], rotmat); gimp_vector3_add (&p[i], &tmp, &mapvals.position); } /* Draw the box */ /* ============ */ cx1 = (gdouble) startx; cy1 = (gdouble) starty; cx2 = cx1 + (gdouble) pw; cy2 = cy1 + (gdouble) ph; n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[0],p[1]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[1],p[2]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[2],p[3]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[3],p[0]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[4],p[5]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[5],p[6]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[6],p[7]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[7],p[4]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[0],p[4]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[1],p[5]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[2],p[6]); n = draw_line (n, startx,starty,pw,ph, cx1,cy1,cx2,cy2, p[3],p[7]); /* Mark end of lines */ /* ================= */ linetab[n].x1 = -1; }
static void draw_wireframe_sphere (gint startx, gint starty, gint pw, gint ph) { GimpVector3 p[2 * (WIRESIZE + 5)]; gint cnt, cnt2, n = 0; gdouble x1, y1, x2, y2, twopifac, cx1, cy1, cx2, cy2; /* Compute wireframe points */ /* ======================== */ twopifac = (2.0 * G_PI) / WIRESIZE; for (cnt = 0; cnt < WIRESIZE; cnt++) { p[cnt].x = mapvals.radius * cos ((gdouble) cnt * twopifac); p[cnt].y = 0.0; p[cnt].z = mapvals.radius * sin ((gdouble) cnt * twopifac); gimp_vector3_rotate (&p[cnt], gimp_deg_to_rad (mapvals.alpha), gimp_deg_to_rad (mapvals.beta), gimp_deg_to_rad (mapvals.gamma)); gimp_vector3_add (&p[cnt], &p[cnt], &mapvals.position); } p[cnt] = p[0]; for (cnt = WIRESIZE + 1; cnt < 2 * WIRESIZE + 1; cnt++) { p[cnt].x = mapvals.radius * cos ((gdouble) (cnt-(WIRESIZE+1))*twopifac); p[cnt].y = mapvals.radius * sin ((gdouble) (cnt-(WIRESIZE+1))*twopifac); p[cnt].z = 0.0; gimp_vector3_rotate (&p[cnt], gimp_deg_to_rad (mapvals.alpha), gimp_deg_to_rad (mapvals.beta), gimp_deg_to_rad (mapvals.gamma)); gimp_vector3_add (&p[cnt], &p[cnt], &mapvals.position); } p[cnt] = p[WIRESIZE+1]; cnt++; cnt2 = cnt; /* Find rotated axis */ /* ================= */ gimp_vector3_set (&p[cnt], 0.0, -0.35, 0.0); gimp_vector3_rotate (&p[cnt], gimp_deg_to_rad (mapvals.alpha), gimp_deg_to_rad (mapvals.beta), gimp_deg_to_rad (mapvals.gamma)); p[cnt+1] = mapvals.position; gimp_vector3_set (&p[cnt+2], 0.0, 0.0, -0.35); gimp_vector3_rotate (&p[cnt+2], gimp_deg_to_rad (mapvals.alpha), gimp_deg_to_rad (mapvals.beta), gimp_deg_to_rad (mapvals.gamma)); p[cnt+3] = mapvals.position; p[cnt + 4] = p[cnt]; gimp_vector3_mul (&p[cnt + 4], -1.0); p[cnt + 5] = p[cnt + 1]; gimp_vector3_add (&p[cnt], &p[cnt], &mapvals.position); gimp_vector3_add (&p[cnt + 2], &p[cnt + 2], &mapvals.position); gimp_vector3_add (&p[cnt + 4], &p[cnt + 4], &mapvals.position); /* Draw the circles (equator and zero meridian) */ /* ============================================ */ cx1 = (gdouble) startx; cy1 = (gdouble) starty; cx2 = cx1 + (gdouble) pw; cy2 = cy1 + (gdouble) ph; for (cnt = 0; cnt < cnt2 - 1; cnt++) { if (p[cnt].z > mapvals.position.z && p[cnt + 1].z > mapvals.position.z) { gimp_vector_3d_to_2d (startx, starty, pw, ph, &x1, &y1, &mapvals.viewpoint, &p[cnt]); gimp_vector_3d_to_2d (startx, starty, pw, ph, &x2, &y2, &mapvals.viewpoint, &p[cnt + 1]); if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE) { linetab[n].x1 = (gint) (x1 + 0.5); linetab[n].y1 = (gint) (y1 + 0.5); linetab[n].x2 = (gint) (x2 + 0.5); linetab[n].y2 = (gint) (y2 + 0.5); linetab[n].linewidth = 3; linetab[n].linestyle = GDK_LINE_SOLID; gdk_gc_set_line_attributes (gc, linetab[n].linewidth, linetab[n].linestyle, GDK_CAP_NOT_LAST, GDK_JOIN_MITER); gdk_draw_line (previewarea->window, gc, linetab[n].x1, linetab[n].y1, linetab[n].x2, linetab[n].y2); n++; } } } /* Draw the axis (pole to pole and center to zero meridian) */ /* ======================================================== */ for (cnt = 0; cnt < 3; cnt++) { gimp_vector_3d_to_2d (startx, starty, pw, ph, &x1, &y1, &mapvals.viewpoint, &p[cnt2]); gimp_vector_3d_to_2d (startx, starty, pw, ph, &x2, &y2, &mapvals.viewpoint, &p[cnt2 + 1]); if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE) { linetab[n].x1 = RINT (x1); linetab[n].y1 = RINT (y1); linetab[n].x2 = RINT (x2); linetab[n].y2 = RINT (y2); if (p[cnt2].z < mapvals.position.z || p[cnt2+1].z < mapvals.position.z) { linetab[n].linewidth = 1; linetab[n].linestyle = GDK_LINE_DOUBLE_DASH; } else { linetab[n].linewidth = 3; linetab[n].linestyle = GDK_LINE_SOLID; } gdk_gc_set_line_attributes (gc, linetab[n].linewidth, linetab[n].linestyle, GDK_CAP_NOT_LAST, GDK_JOIN_MITER); gdk_draw_line (previewarea->window, gc, linetab[n].x1, linetab[n].y1, linetab[n].x2, linetab[n].y2); n++; } cnt2 += 2; } /* Mark end of lines */ /* ================= */ linetab[n].x1 = -1; }
static void draw_wireframe_plane (gint startx, gint starty, gint pw, gint ph) { GimpVector3 v1, v2, a, b, c, d, dir1, dir2; gint cnt, n = 0; gdouble x1, y1, x2, y2, cx1, cy1, cx2, cy2, fac; /* Find rotated box corners */ /* ======================== */ gimp_vector3_set (&v1, 0.5, 0.0, 0.0); gimp_vector3_set (&v2, 0.0, 0.5, 0.0); gimp_vector3_rotate (&v1, gimp_deg_to_rad (mapvals.alpha), gimp_deg_to_rad (mapvals.beta), gimp_deg_to_rad (mapvals.gamma)); gimp_vector3_rotate (&v2, gimp_deg_to_rad (mapvals.alpha), gimp_deg_to_rad (mapvals.beta), gimp_deg_to_rad (mapvals.gamma)); dir1 = v1; gimp_vector3_normalize (&dir1); dir2 = v2; gimp_vector3_normalize (&dir2); fac = 1.0 / (gdouble) WIRESIZE; gimp_vector3_mul (&dir1, fac); gimp_vector3_mul (&dir2, fac); gimp_vector3_add (&a, &mapvals.position, &v1); gimp_vector3_sub (&b, &a, &v2); gimp_vector3_add (&a, &a, &v2); gimp_vector3_sub (&d, &mapvals.position, &v1); gimp_vector3_sub (&d, &d, &v2); c = b; cx1 = (gdouble) startx; cy1 = (gdouble) starty; cx2 = cx1 + (gdouble) pw; cy2 = cy1 + (gdouble) ph; for (cnt = 0; cnt <= WIRESIZE; cnt++) { gimp_vector_3d_to_2d (startx, starty, pw, ph, &x1, &y1, &mapvals.viewpoint, &a); gimp_vector_3d_to_2d (startx, starty, pw, ph, &x2, &y2, &mapvals.viewpoint, &b); if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE) { linetab[n].x1 = RINT (x1); linetab[n].y1 = RINT (y1); linetab[n].x2 = RINT (x2); linetab[n].y2 = RINT (y2); linetab[n].linewidth = 1; linetab[n].linestyle = GDK_LINE_SOLID; gdk_gc_set_line_attributes (gc, linetab[n].linewidth, linetab[n].linestyle, GDK_CAP_NOT_LAST, GDK_JOIN_MITER); gdk_draw_line (previewarea->window, gc, linetab[n].x1, linetab[n].y1, linetab[n].x2, linetab[n].y2); n++; } gimp_vector_3d_to_2d (startx, starty, pw, ph, &x1, &y1, &mapvals.viewpoint, &c); gimp_vector_3d_to_2d (startx, starty, pw, ph, &x2, &y2, &mapvals.viewpoint, &d); if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE) { linetab[n].x1 = RINT (x1); linetab[n].y1 = RINT (y1); linetab[n].x2 = RINT (x2); linetab[n].y2 = RINT (y2); linetab[n].linewidth = 1; linetab[n].linestyle = GDK_LINE_SOLID; gdk_gc_set_line_attributes (gc, linetab[n].linewidth, linetab[n].linestyle, GDK_CAP_NOT_LAST, GDK_JOIN_MITER); gdk_draw_line (previewarea->window, gc, linetab[n].x1, linetab[n].y1, linetab[n].x2, linetab[n].y2); n++; } gimp_vector3_sub (&a, &a, &dir1); gimp_vector3_sub (&b, &b, &dir1); gimp_vector3_add (&c, &c, &dir2); gimp_vector3_add (&d, &d, &dir2); } /* Mark end of lines */ /* ================= */ linetab[n].x1 = -1; }
static GimpRGB phong_shade (GimpVector3 *position, GimpVector3 *viewpoint, GimpVector3 *normal, GimpVector3 *lightposition, GimpRGB *diff_col, GimpRGB *light_col, LightType light_type) { GimpRGB diffuse_color, specular_color; gdouble nl, rv, dist; GimpVector3 l, v, n, lnormal, h; /* Compute ambient intensity */ /* ========================= */ n = *normal; /* Compute (N*L) term of Phong's equation */ /* ====================================== */ if (light_type == POINT_LIGHT) gimp_vector3_sub (&l, lightposition, position); else { l = *lightposition; gimp_vector3_normalize (&l); } dist = gimp_vector3_length (&l); if (dist != 0.0) gimp_vector3_mul (&l, 1.0 / dist); nl = MAX (0., 2.0 * gimp_vector3_inner_product (&n, &l)); lnormal = l; gimp_vector3_normalize (&lnormal); if (nl >= 0.0) { /* Compute (R*V)^alpha term of Phong's equation */ /* ============================================ */ gimp_vector3_sub (&v, viewpoint, position); gimp_vector3_normalize (&v); gimp_vector3_add (&h, &lnormal, &v); gimp_vector3_normalize (&h); rv = MAX (0.01, gimp_vector3_inner_product (&n, &h)); rv = pow (rv, mapvals.material.highlight); rv *= nl; /* Compute diffuse and specular intensity contribution */ /* =================================================== */ diffuse_color = *light_col; gimp_rgb_multiply (&diffuse_color, mapvals.material.diffuse_int); diffuse_color.r *= diff_col->r; diffuse_color.g *= diff_col->g; diffuse_color.b *= diff_col->b; gimp_rgb_multiply (&diffuse_color, nl); specular_color = *light_col; if (mapvals.material.metallic) /* for metals, specular color = diffuse color */ { specular_color.r *= diff_col->r; specular_color.g *= diff_col->g; specular_color.b *= diff_col->b; } gimp_rgb_multiply (&specular_color, mapvals.material.specular_ref); gimp_rgb_multiply (&specular_color, rv); gimp_rgb_add (&diffuse_color, &specular_color); gimp_rgb_clamp (&diffuse_color); } gimp_rgb_clamp (&diffuse_color); return diffuse_color; }
void precompute_normals (gint x1, gint x2, gint y) { GimpVector3 *tmpv, p1, p2, p3, normal; gdouble *tmpd; gint n, i, nv; guchar *map = NULL; gint bpp = 1; guchar mapval; /* First, compute the heights */ /* ========================== */ tmpv = triangle_normals[0]; triangle_normals[0] = triangle_normals[1]; triangle_normals[1] = tmpv; tmpv = vertex_normals[0]; vertex_normals[0] = vertex_normals[1]; vertex_normals[1] = vertex_normals[2]; vertex_normals[2] = tmpv; tmpd = heights[0]; heights[0] = heights[1]; heights[1] = heights[2]; heights[2] = tmpd; if (mapvals.bumpmap_id != -1) { bpp = gimp_drawable_bpp(mapvals.bumpmap_id); } gimp_pixel_rgn_get_row (&bump_region, bumprow, x1, y, x2 - x1); if (mapvals.bumpmaptype > 0) { switch (mapvals.bumpmaptype) { case 1: map = logmap; break; case 2: map = sinemap; break; default: map = spheremap; break; } for (n = 0; n < (x2 - x1); n++) { if (bpp>1) { mapval = (guchar)((float)((bumprow[n * bpp] +bumprow[n * bpp +1] + bumprow[n * bpp + 2])/3.0 )) ; } else { mapval = bumprow[n * bpp]; } heights[2][n] = (gdouble) mapvals.bumpmax * (gdouble) map[mapval] / 255.0; } } else { for (n = 0; n < (x2 - x1); n++) { if (bpp>1) { mapval = (guchar)((float)((bumprow[n * bpp] +bumprow[n * bpp +1] + bumprow[n * bpp + 2])/3.0 )) ; } else { mapval = bumprow[n * bpp]; } heights[2][n] = (gdouble) mapvals.bumpmax * (gdouble) mapval / 255.0; } } /* Compute triangle normals */ /* ======================== */ i = 0; for (n = 0; n < (x2 - x1 - 1); n++) { p1.x = 0.0; p1.y = ystep; p1.z = heights[2][n] - heights[1][n]; p2.x = xstep; p2.y = ystep; p2.z = heights[2][n+1] - heights[1][n]; p3.x = xstep; p3.y = 0.0; p3.z = heights[1][n+1] - heights[1][n]; triangle_normals[1][i] = gimp_vector3_cross_product (&p2, &p1); triangle_normals[1][i+1] = gimp_vector3_cross_product (&p3, &p2); gimp_vector3_normalize (&triangle_normals[1][i]); gimp_vector3_normalize (&triangle_normals[1][i+1]); i += 2; } /* Compute vertex normals */ /* ====================== */ i = 0; gimp_vector3_set (&normal, 0.0, 0.0, 0.0); for (n = 0; n < (x2 - x1 - 1); n++) { nv = 0; if (n > 0) { if (y > 0) { gimp_vector3_add (&normal, &normal, &triangle_normals[0][i-1]); gimp_vector3_add (&normal, &normal, &triangle_normals[0][i-2]); nv += 2; } if (y < pre_h) { gimp_vector3_add (&normal, &normal, &triangle_normals[1][i-1]); nv++; } } if (n <pre_w) { if (y > 0) { gimp_vector3_add (&normal, &normal, &triangle_normals[0][i]); gimp_vector3_add (&normal, &normal, &triangle_normals[0][i+1]); nv += 2; } if (y < pre_h) { gimp_vector3_add (&normal, &normal, &triangle_normals[1][i]); gimp_vector3_add (&normal, &normal, &triangle_normals[1][i+1]); nv += 2; } } gimp_vector3_mul (&normal, 1.0 / (gdouble) nv); gimp_vector3_normalize (&normal); vertex_normals[1][n] = normal; i += 2; } }