📄 flatshading.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 deg *3.1415926535897932384/180 /* deg to rad */main() /* file "FlatShading.c" */{ Pint WorkstnID = 1; /* workstation identifier */ Pint ConnID; /* connection identifier */ Pint WorkstnType = POIDDX; /* out/in, direct, dbl bfr, Xwindow */ Pint Cube = 1, Scene = 2; /* structure identifiers */ Pgcolr White; /* for white interiors, edges */ Pint_list LightsOn, LightsOff; /* for setting light source state */ Pint AllLights[4] = {1, 2, 3, 4}; Pvec3 Vec; /* for various scaling/translation */ Pmatrix3 xform; /* transformation 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 env: direct */ DefineCube(Cube); DefineLightSources(WorkstnID); ppost_struct(WorkstnID, Scene, 1.0); /*=== define the scene =================================================*/ popen_struct(Scene); pset_facet_cull_mode(PFACET_CULL_BACKFACING); pset_edge_flag(PEDGE_ON); SetGColr(White, 1.0, 1.0, 1.0); pset_edge_colr(&White); pset_int_style(PSTYLE_SOLID); pset_int_colr(&White); pset_refl_model(PREFL_MODEL_AMB_DIFF_REFL); LightsOn.num_ints = 4; LightsOn.ints = AllLights; LightsOff.num_ints = 0; pset_light_source_st(&LightsOn, &LightsOff); /*--- create modelling transformation ----------------------------------*/ Vec.delta_x = 0.3, Vec.delta_y = 0.3, Vec.delta_z = 0.3; pscale3(&Vec, &Error, xform); if (Error) printf("Error", Error, " in pscale3.\n"), exit(1); pset_local_tran3(xform, PTYPE_REPLACE); protate_y(15 deg, &Error, xform); if (Error) printf("Error", Error, " in protate_y.\n"), exit(1); pset_local_tran3(xform, PTYPE_POSTCONCAT); protate_x(15 deg, &Error, xform); if (Error) printf("Error", Error, " in protate_x.\n"), exit(1); pset_local_tran3(xform, PTYPE_POSTCONCAT); Vec.delta_x = 0.5, Vec.delta_y = 0.5, Vec.delta_z = 0.5; ptranslate3(&Vec, &Error, xform); if (Error) printf("Error", Error, " in ptranslate3.\n"), exit(1); pset_local_tran3(xform, PTYPE_POSTCONCAT); pexec_struct(Cube); /* execute structure */ pclose_struct(); /* close structure */ /*=== close up shop ====================================================*/ pclose_ws(WorkstnID); pclose_phigs();}/****************************************************************************/DefineCube(Cube)Pint Cube;{ static Ppoint3 TopPts[5] = {{-1.0, 1.0, -1.0}, {-1.0, 1.0, 1.0}, { 0.0, 1.0, 1.0}, { 1.0, 1.0, 0.0}, { 1.0, 1.0, -1.0}}; static Ppoint_list3 Top = {5, TopPts}; static Ppoint_list_list3 TopList = {1, &Top}; static Ppoint3 BottomPts[4] = {{-1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0}}; static Ppoint_list3 Bottom = {4, BottomPts}; static Ppoint_list_list3 BottomList = {1, &Bottom}; static Ppoint3 RightPts[5] = {{ 1.0, -1.0, -1.0}, { 1.0, 1.0, -1.0}, { 1.0, 1.0, 0.0}, { 1.0, 0.0, 1.0}, { 1.0, -1.0, 1.0}}; static Ppoint_list3 Right = {5, RightPts}; static Ppoint_list_list3 RightList = {1, &Right}; static Ppoint3 LeftPts[4] = {{-1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0}, {-1.0, 1.0, -1.0}, {-1.0, -1.0, -1.0}}; static Ppoint_list3 Left = {4, LeftPts}; static Ppoint_list_list3 LeftList = {1, &Left}; static Ppoint3 FrontPts[5] = {{ 1.0, -1.0, 1.0}, { 1.0, 0.0, 1.0}, { 0.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}, {-1.0, -1.0, 1.0}}; static Ppoint_list3 Front = {5, FrontPts}; static Ppoint_list_list3 FrontList = {1, &Front}; static Ppoint3 BackPts[4] = {{-1.0, -1.0, -1.0}, {-1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0}, { 1.0, -1.0, -1.0}}; static Ppoint_list3 Back = {4, BackPts}; static Ppoint_list_list3 BackList = {1, &Back}; static Ppoint3 CornerPts[3] = {{ 1.0, 0.0, 1.0}, { 1.0, 1.0, 0.0}, { 0.0, 1.0, 1.0}}; static Ppoint_list3 Corner = {3, CornerPts}; static Ppoint_list_list3 CornerList = {1, &Corner}; popen_struct(Cube); pfill_area_set3(&TopList); /* \ */ pfill_area_set3(&RightList); /* \ */ pfill_area_set3(&FrontList); /* \ pfill_area_set3 is used */ pfill_area_set3(&BottomList); /* > instead of pfill_area3 */ pfill_area_set3(&LeftList); /* / so edges would be visible */ pfill_area_set3(&BackList); /* / */ pfill_area_set3(&CornerList); /* / */ pclose_struct();}/****************************************************************************/DefineLightSources(WorkstnID)Pint WorkstnID;{ DefineLightSource(WorkstnID, 1, /* light source 1 */ PLIGHT_SOURCE_AMB, /* ambient */ 0.2, 0.2, 0.2, /* 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_POS, /* positional */ 0.6, 0.0, 0.0, /* colour (dark red) */ 0.0, 5.0, 0.0, /* position */ 0.0, 0.0, 0.0, /* direction (unused) */ 0.0, 0.0, 1.0, 0.0); /* conc/spread (unused), atten */ DefineLightSource(WorkstnID, 3, /* light source 3 */ PLIGHT_SOURCE_DIR, /* directional */ 0.0, 0.0, 0.7, /* colour (dark blue) */ 0.0, 0.0, 0.0, /* position (unused) */ 0.0, 0.0, -5.0, /* direction */ 0.0, 0.0, 0.0, 0.0); /* conc/spread/atten (unused) */ DefineLightSource(WorkstnID, 4, /* light source 4 */ PLIGHT_SOURCE_POS, /* positional */ 0.0, 0.5, 0.0, /* colour (dark green) */ -1.5, 0.0, 0.0, /* position */ 0.0, 0.0, 0.0, /* direction (unused) */ 0.0, 0.0, 1.0, 0.0); /* conc/spread (unused), atten */}/****************************************************************************/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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -