📄 flatsmooth.c
字号:
#include <phigs.h> /* get HP-PHIGS definitions for C */#include <stdio.h> /* get standard I/O definitions */#include <math.h> /* link with library "-lm" */#define SetGColr(Vl,Rd,Gr,Bl) Vl.colr_type = PCOLR_RGB; \ Vl.colr_value.colr_rep.rgb.red = Rd; \ Vl.colr_value.colr_rep.rgb.green = Gr; \ Vl.colr_value.colr_rep.rgb.blue = Bl#define RampSize 50 /* size of colour ramps */#define Black 0.0, 0.0, 0.0 /* RGB for black */#define White 1.0, 1.0, 1.0 /* RGB for white */#define Blue 0.0, 0.0, 1.0 /* RGB for blue */#define Green 0.0, 1.0, 0.0 /* RGB for green */#define Yellow 1.0, 1.0, 0.0 /* RGB for yellow */#define Red 1.0, 0.0, 0.0 /* RGB for red */main() /* file "FlatSmooth.c" */{ Pint WorkstnID = 1; /* workstation identifier */ Pint ConnID; /* connection identifier */ Pint WorkstnType = POIDDX; /* out/in, direct, dbl bfr, Xwindow */ Pint FillArea = 1, Scene = 2;/* structure IDs chosen by user */ Pgcolr Colour; /* for color table value */ Pmatrix3 Matrix; /* for transformation matrices */ static Pfloat WeightVecVals[3] = /* for determining colour index */ {1.0, 0.0, 0.0}; /* arbitrarily use red */ static Pfloat_list WeightVector = {3, WeightVecVals}; static char String[2][15] = /* for textual labels */ {"Flat Shading", "Smooth Shading"}; static Pvec NullVec = {0.0, 0.0}; /* for panno_text_rel */ Ppoint Loc; /* location of annotation text */ Pvec3 Vec; /* for translation */ Pint Error; /* error-return variable */ popen_phigs((char *) stderr, 0); /* errors go to "stderr" */ pescape_u4("/dev/screen/phigs_window", &ConnID); popen_ws(WorkstnID, (void *) ConnID, WorkstnType); pescape_u250(WorkstnID, 0); /* set color env: direct */ pescape_u240(WorkstnID, 1); /* turn off dithering (cell=1x1) */ /*--- define the filled areas ------------------------------------------*/ popen_struct(FillArea); CreatePrimitives(); /* make filled areas */ pclose_struct(); /*--- define whole scene -----------------------------------------------*/ popen_struct(Scene); DefineColourRamps(WorkstnID, 1, &WeightVector); pset_colr_map_ind(1); pset_text_font(-4); /*--- white text, which is in second entry of color list ---------------*/ SetGColr(Colour, 2.0 / (3 * RampSize + 2), 0.0, 0.0); pset_text_colr(&Colour); pset_anno_char_ht(0.05); Loc.x = 0.1, Loc.y = 0.9; panno_text_rel(&Loc, &NullVec, String[0]); Loc.x = 0.1, Loc.y = 0.4; panno_text_rel(&Loc, &NullVec, String[1]); pset_int_style(PSTYLE_SOLID); pset_refl_model(PREFL_MODEL_NO_REFL); pset_int_shad_method(PINT_SHAD_METHOD_NONE); pexec_struct(FillArea); Vec.delta_x = 0.0, Vec.delta_y = -0.5, Vec.delta_z = 0.0; ptranslate3(&Vec, &Error, Matrix); pset_local_tran3(Matrix, PTYPE_REPLACE); pset_int_shad_method(PINT_SHAD_METHOD_COLR); pexec_struct(FillArea); pclose_struct(); /*--- close up shop ----------------------------------------------------*/ ppost_struct(WorkstnID, Scene, 1.0); pclose_ws(WorkstnID); pclose_phigs();}/****************************************************************************/CreatePrimitives(){ Ppoint3 Points[4]; /* XYZ data */ Pfacet Facets; /* facet data */ Pvertex3_set VertexSet; /* vertex data */ Pvertex3_list VertexList[1]; /* array of lists for set */ Ppoint3 Vertices[4]; /* vertices' XYZ data */ Prgb FacetColour; /* direct RGB color */ Prgb VertexColour[4]; /* direct RGB color */ Pfloat Colour, dColour; /* value to increment Colour */ Pint I, J; /* loop control variables */ Facets.num_data_per_facet = 0; Facets.facet_norm = NULL; Facets.facet_data = NULL; Facets.facet_colrv = (void *) &FacetColour; VertexSet.num_lists = 1; VertexSet.vertices = VertexList; VertexList[0].num_vertices = 4; VertexList[0].num_data_per_vertex = 0; VertexList[0].vertex_points = Vertices; VertexList[0].vertex_colrvs.colr_reps.rgb = VertexColour; VertexList[0].vertex_norms = NULL; VertexList[0].vertex_data = NULL; Colour = 3.0/(3 * RampSize + 2); dColour = (3 * RampSize)/(3.0 * RampSize + 2)/9; for (I = 0; I < 10; I++) { for (J = 0; J < 4; J++) { Vertices[J].x = (I + 1 + (J == 1 || J == 2)) / 12.0; Vertices[J].y = 0.55 + 0.3 * (J >= 2); Vertices[J].z = 0.0; } FacetColour.red = Colour; FacetColour.green = FacetColour.blue = 0.0; VertexColour[0].red = Colour; VertexColour[0].green = VertexColour[0].blue = 0.0; VertexColour[1].red = Colour + dColour; VertexColour[1].green = VertexColour[1].blue = 0.0; VertexColour[2].red = Colour + dColour; VertexColour[2].green = VertexColour[2].blue = 0.0; VertexColour[3].red = Colour; VertexColour[3].green = VertexColour[3].blue = 0.0; pfill_area_set3_data(PCOLR_RGB, &Facets, NULL, &VertexSet); Colour += dColour; }}/****************************************************************************/DefineColourRamps(WorkstnID, CMappingIndex, WeightVector)Pint WorkstnID; /* workstation identifier */Pint CMappingIndex; /* colour mapping table index */Pfloat_list *WeightVector; /* for colour->gray conversion */{ Pcolr_map_rep ColourMapRep; /* c-mapping representation */ Prgb Colours[256]; /* max needed */ Pcolrv_list ColourList; /* list of colours */ Pint Error; /* error-return variable */ Pint Idx; /* ramp-starting locations */ Idx = 0; /* start location of 2-entry ramp */ DefineColourRamp(Black, White, Idx, 2, Colours); Idx = Idx + 2; /* increment by size of last ramp */ DefineColourRamp(Blue, Green, Idx, RampSize, Colours); Idx = Idx + RampSize; /* increment by size of last ramp */ DefineColourRamp(Green, Yellow, Idx, RampSize + 1, Colours); Idx = Idx + RampSize; /* increment by size of last ramp */ DefineColourRamp(Yellow, Red, Idx, RampSize, Colours); ColourList.num_colrs = 2 + 3 * RampSize; /* b/w ramp + 3 colr ramps */ ColourList.colrs.colr_reps.rgb = Colours; ColourMapRep.method = PCOLR_MAP_METHOD_PSEUDO; ColourMapRep.data.pseudo.model = PMODEL_RGB; ColourMapRep.data.pseudo.weight_vector.num_floats=WeightVector->num_floats; ColourMapRep.data.pseudo.weight_vector.floats = WeightVector->floats; ColourMapRep.data.pseudo.colrs = ColourList; pset_colr_map_rep(WorkstnID, CMappingIndex, &ColourMapRep);}/****************************************************************************/DefineColourRamp(R1, G1, B1, R2, G2, B2, Idx, RampSz, Colours)Pfloat R1, G1, B1, R2, G2, B2; /* extremes of ramp */Pint Idx; /* location in Colours array */Pint RampSz; /* "local ramp size" */Prgb Colours[]; /* array being filled */{ Pfloat dR, dG, dB; /* diff. between colr(i), colr(i+1) */ Pint I; /* loop control variable */ dR = (R2 - R1) / (RampSz - 1); dG = (G2 - G1) / (RampSz - 1); dB = (B2 - B1) / (RampSz - 1); for (I = 0; I < RampSz; I++) { Colours[Idx + I].red = R1 + dR * I; Colours[Idx + I].green = G1 + dG * I; Colours[Idx + I].blue = B1 + dB * I; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -