?? grayramp.c
字號(hào):
#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 deg *3.14159265358979323/180/* convert deg to rad */#define FirstColour 0.0, 0.0, 0.0 /* dimmest colour: black */#define LastColour 1.0, 1.0, 1.0 /* brightest colour: white */main() /* file "GrayRamp.c" */{ Pint WorkstnID = 1; /* workstation identifier */ Pint ConnID; /* connection identifier */ Pint WorkstnType = POIDDX; /* out/in, direct, dbl bfr, Xwindow */ Pint SphereStruc=1, Scene=2; /* structure IDs (chosen by user) */ static Pfloat WeightVecVals[3] = {0.30, 0.59, 0.11}; static Pfloat_list WeightVector = {3, WeightVecVals}; static Pint Lights[3] = {1, 2, 3}; /* light-state array */ Pint_list LightsOn, LightsOff; /* light-state variables */ Pmatrix3 Matrix; /* transformation matrix */ static Ppoint3 Loc = {0.0, 0.0, 0.0}; /* for building a matrix */ static Pvec3 Shift1 = {0.3, 0.7, 0.7}; /* for building a matrix */ static Pvec3 Shift2 = {0.7, 0.3, 0.3}; /* for building a matrix */ static Pvec3 Scale = {0.3, 0.3, 0.3}; /* for building a matrix */ 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 colour environment: direct */ popen_struct(SphereStruc); CreateSphere(32, 16); /* define the sphere structure */ pclose_struct(); DefineLightSources(WorkstnID); /* define all lights */ popen_struct(Scene); DefineColourRamp(WorkstnID, FirstColour, LastColour, 101, 1, &WeightVector); pset_colr_map_ind(1); pset_facet_cull_mode(PFACET_CULL_BACKFACING); pset_int_style(PSTYLE_SOLID); pset_refl_model(PREFL_MODEL_AMB_DIFF_SPEC_REFL); pset_int_shad_method(PINT_SHAD_METHOD_NONE); SetReflectanceProperties(WorkstnID, 1.0, 1.0, 1.0, /* amb/diff/spec reflections */ 1.0, 1.0, 1.0, /* specular colour */ 20.0); /* specular exponent */ LightsOn.num_ints = 3; LightsOn.ints = Lights; LightsOff.num_ints = 0; pset_light_source_st(&LightsOn, &LightsOff); pbuild_tran_matrix3(&Loc, &Shift1, -60 deg, 30 deg, -5 deg, &Scale, &Error, Matrix); pset_local_tran3(Matrix, PTYPE_REPLACE); pexec_struct(SphereStruc); pset_int_shad_method(PINT_SHAD_METHOD_COLR); pbuild_tran_matrix3(&Loc, &Shift2, -60 deg, 30 deg, -5 deg, &Scale, &Error, Matrix); pset_local_tran3(Matrix, PTYPE_REPLACE); pexec_struct(SphereStruc); pclose_struct(); ppost_struct(WorkstnID, Scene, 1.0); pclose_ws(WorkstnID); pclose_phigs();}/****************************************************************************/CreateSphere(Longitudes, Latitudes)Pint Longitudes, Latitudes;{ /*--- arbitrary limit of 50 for number of latitudes and longitudes -----*/ Pfloat Sphere[3][51][51]; Pint Longitude, Latitude, I; /* loop control variables */ Pfloat Theta, Phi, CosPhi; /* working variables */ Ppoint3 Vertices[4]; /* vertex XYZ data */ Pfacet Facets; /* facet data */ Pvertex3_set VertexSet; /* vertex data */ Pvertex3_list VertexList[1]; static Prgb Red = {1.0, 0.0, 0.0}; /* RGB for red */ static Prgb LightBlue = {0.0, 0.5, 0.7}; /* RGB for a light blue */ /*---- define sphere's data base ----------------------------------------*/ for (Longitude = 0; Longitude <= Longitudes; Longitude++) { Theta = 360.0 * Longitude / Longitudes; for (Latitude = 0; Latitude <= Latitudes; Latitude++) { Phi = -89.99 + 179.98 * Latitude / Latitudes; CosPhi = cos(Phi deg); Sphere[0][Longitude][Latitude] = CosPhi * cos(Theta deg); Sphere[1][Longitude][Latitude] = CosPhi * sin(Theta deg); Sphere[2][Longitude][Latitude] = sin(Phi deg); } } /*--- create the sphere out of unicolour patches -----------------------*/ Facets.num_data_per_facet = 0; Facets.facet_norm = NULL; Facets.facet_data = NULL; 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 = NULL; VertexList[0].vertex_norms = (Pvec3 *) Vertices; VertexList[0].vertex_data = NULL; for (Latitude = 0; Latitude < Latitudes; Latitude++) { for (Longitude = 0; Longitude < Longitudes; Longitude++) { Vertices[0].x = Sphere[0][Longitude][Latitude]; Vertices[0].y = Sphere[1][Longitude][Latitude]; Vertices[0].z = Sphere[2][Longitude][Latitude]; Vertices[1].x = Sphere[0][Longitude + 1][Latitude]; Vertices[1].y = Sphere[1][Longitude + 1][Latitude]; Vertices[1].z = Sphere[2][Longitude + 1][Latitude]; Vertices[2].x = Sphere[0][Longitude + 1][Latitude + 1]; Vertices[2].y = Sphere[1][Longitude + 1][Latitude + 1]; Vertices[2].z = Sphere[2][Longitude + 1][Latitude + 1]; Vertices[3].x = Sphere[0][Longitude][Latitude + 1]; Vertices[3].y = Sphere[1][Longitude][Latitude + 1]; Vertices[3].z = Sphere[2][Longitude][Latitude + 1]; if ((Longitude >> 1) & 1) Facets.facet_colrv = (void *) &LightBlue; else Facets.facet_colrv = (void *) &Red; pfill_area_set3_data(PCOLR_RGB, &Facets, NULL, &VertexSet); } } return;}/****************************************************************************/SetReflectanceProperties(WorkstnID, AmbientRefl, DiffuseRefl, SpecRefl, SpecR, SpecG, SpecB, SpecExp)Pint WorkstnID; /* workstation ID */Pfloat AmbientRefl; /* ambient refl. coefficient (0.0->1.0) */Pfloat DiffuseRefl; /* diffuse refl. coefficient (0.0->1.0) */Pfloat SpecRefl; /* specular refl coefficient (0.0->1.0) */Pfloat SpecR, SpecG, SpecB; /* specular reflection colour */Pfloat SpecExp; /* specular exponent ("shininess") */{ Prefl_prop ReflProp; ReflProp.type = PREFL_PROPS_SIMPLE_REFL; ReflProp.data.simple_refl.amb_coeff = AmbientRefl; ReflProp.data.simple_refl.diff_coeff = DiffuseRefl; ReflProp.data.simple_refl.spec_coeff = SpecRefl; ReflProp.data.simple_refl.spec_colr.colr_type = PCOLR_RGB; ReflProp.data.simple_refl.spec_colr.colr_value.colr_rep.rgb.red = SpecR; ReflProp.data.simple_refl.spec_colr.colr_value.colr_rep.rgb.green = SpecG; ReflProp.data.simple_refl.spec_colr.colr_value.colr_rep.rgb.blue = SpecB; ReflProp.data.simple_refl.spec_exp = SpecExp; pset_refl_props(&ReflProp);}/****************************************************************************/DefineLightSources(WorkstnID)Pint WorkstnID;{ DefineLightSource(WorkstnID, 1, /* light source 1 */ PLIGHT_SOURCE_AMB, /* ambient */ 0.4, 0.4, 0.4, /* colour (dark gray) */ 0.0, 0.0, 0.0, /* position (unused) */ 0.0, 0.0, 0.0, /* direction (unused) */ 0.0, 0.0, 0.0, 0.0); /* conc/spread/atten (unused) */ DefineLightSource(WorkstnID, 2, /* light source 2 */ PLIGHT_SOURCE_DIR, /* directional */ 0.8, 0.8, 0.8, /* colour (dim white) */ 0.0, 0.0, 0.0, /* position (unused) */ 10.0, 10.0, -10.0, /* direction */ 0.0, 0.0, 0.0, 0.0); /* conc/spread/atten (unused) */ DefineLightSource(WorkstnID, 3, /* light source 3 */ PLIGHT_SOURCE_DIR, /* directional */ 0.7, 0.7, 0.7, /* colour (dim white) */ 0.0, 0.0, 0.0, /* position (unused) */ -10.0, -10.0, -2.0, /* direction */ 0.0, 0.0, 0.0, 0.0); /* conc/spread/atten (unused) */}/****************************************************************************/DefineLightSource(WorkstnID, LightNo, LightType, R, G, B, X, Y, Z, dX, dY, dZ, Exponent, Spread, Att1, Att2)Pint WorkstnID; /* workstation ID */Pint LightNo, LightType; /* index and type */Pfloat R, G, B; /* colour */Pfloat X, Y, Z; /* position (positional) */Pfloat dX, dY, dZ; /* direction (all but ambient) */Pfloat Exponent; /* concentration exponent (spot) */Pfloat Spread; /* spread angle (spot) */Pfloat Att1, Att2; /* attenuation factors (pos., spot) */{ Plight_source_rep LightSource; /* light source representation */ LightSource.type = LightType; switch (LightType) { case PLIGHT_SOURCE_AMB: SetGColr(LightSource.data.amb.colr, R, G, B); break; case PLIGHT_SOURCE_DIR: SetGColr(LightSource.data.dir.colr, R, G, B); LightSource.data.dir.dir.delta_x = dX; LightSource.data.dir.dir.delta_y = dY; LightSource.data.dir.dir.delta_z = dZ; break; case PLIGHT_SOURCE_POS: SetGColr(LightSource.data.pos.colr, R, G, B); LightSource.data.pos.pos.x = X; LightSource.data.pos.pos.y = Y; LightSource.data.pos.pos.z = Z; LightSource.data.pos.c1 = Att1; LightSource.data.pos.c2 = Att2; break; case PLIGHT_SOURCE_SPOT: SetGColr(LightSource.data.spot.colr, R, G, B); LightSource.data.spot.pos.x = X; LightSource.data.spot.pos.y = Y; LightSource.data.spot.pos.z = Z; LightSource.data.spot.dir.delta_x = dX; LightSource.data.spot.dir.delta_y = dY; LightSource.data.spot.dir.delta_z = dZ; LightSource.data.spot.concent_exp = Exponent; LightSource.data.spot.c1 = Att1; LightSource.data.spot.c2 = Att2; LightSource.data.spot.spread_angle = Spread; break; } pset_light_source_rep(WorkstnID, LightNo, &LightSource);}/****************************************************************************/DefineColourRamp(WorkstnID, FirstR, FirstG, FirstB, LastR, LastG, LastB, RampSize, CMappingIndex, WeightVector)Pint WorkstnID; /* workstation identifier */Pfloat FirstR, FirstG, FirstB; /* RGB of first colour */Pfloat LastR, LastG, LastB; /* RGB of last colour */Pint RampSize; /* size of ramp */Pint CMappingIndex; /* colr mapping table index */Pfloat_list *WeightVector; /* for colr->gray conversion */{ Pcolr_map_rep ColourMapRep; /* c-mapping representation */ Prgb Colours[256]; /* max needed */ Pcolrv_list ColourList; /* list of colours */ Pfloat DeltaR, DeltaG, DeltaB; /* diff: colr(i), colr(i+1) */ Pint Error; /* error-return variable */ Pint I; /* loop control variable */ DeltaR = (LastR - FirstR) / (RampSize - 1); DeltaG = (LastG - FirstG) / (RampSize - 1); DeltaB = (LastB - FirstB) / (RampSize - 1); for (I = 0; I < RampSize; I++) { Colours[I].red = FirstR + DeltaR * I; Colours[I].green = FirstG + DeltaG * I; Colours[I].blue = FirstB + DeltaB * I; } ColourList.colrs.colr_reps.rgb = Colours; ColourList.num_colrs = RampSize; 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);}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -