⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 planukit.c

📁 linux 下svgalib编的一个界面程序示例
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    3DKIT   version   1.3    High speed 3D graphics and rendering library for Linux.    1996  Paul Sheer   psheer@icon.co.za    This file is an example program demonstrating the use of the    3dkit library. It is not part of the library and is not copyright.    The author takes no responsibility, for the results    of compilation, execution or other usage of this program.*//*File: planukit.ccomments or suggestions welcome, send to:  psheer@icon.co.zaDemo of 3D graphics tool for drawing shaded 3D surfaces with a light source.This demo sets up the surfaces (in a crude fashion) and the functiondrawobject from 3dkit.c draws them at the specified angle of azimuth,rotation and elevation. The surfaces are sorted from furthest to closestand drawn from their furthest corner forward toward their second furthestcorner. So any object made up of reasonable surfaces will be drawn solidwith hidden surfaces properly removed. Backfaced triangles are not drawnto improve speed.This demo draws a turbo-prop aeroplane (done originally for a 3rd yearaeronautical engineering design project).see the handle_key function below for what all the keys do.*/#include <stdarg.h>#include <stdlib.h>#include <stdio.h>		/*for stderr */#include <math.h>#include <string.h>#include <vga.h>#include <vgagl.h>#include "3dkit.h"#include "3dinit.h"#include "plane.h"#ifdef WORLD_VIEW#define PL_TDOPTION_ROTATE_OBJECT 0#define PL_TDOPTION_LIGHT_SOURCE_CAM 0#else#define PL_TDOPTION_ROTATE_OBJECT TDOPTION_ROTATE_OBJECT	/*Lighting vector follows camera: */#define PL_TDOPTION_LIGHT_SOURCE_CAM TDOPTION_LIGHT_SOURCE_CAM#endif/*closer to 1.25 on my screen: */#define PL_SCREEN_ASPECT 1.333/*Number of surfaces in ths plane */#ifdef WORLD_VIEW#define PL_NUMSURFACES 62#else#define PL_NUMSURFACES 52/*52 */#endif/*maximum width or length of a surface (for malloc) */#define PL_SURF_SIZE 20/*globals used for initialisation of surfaces *//*width and breadth of body surfaces (in grid points) */int DENS = 2;/*width of wing surfaces (in grid points). */int DENS2 = 2;/* length of wing surfaces is inherent in the following    made-up aerofoil: */int gmode;int PL_screen_width;int PL_screen_height;/*A trivial example of how to initialise a surface: */void initplate (TD_Surface * surf, float xstart, float ystart, float zstart, float x, float y, int w, int l){    int i, k, j;/*setup width and length */    surf->w = w + 1;    surf->l = l + 1;/*initialise a 6 meter square plate with its centre at the origin */    for (k = 0; k < w + 1; k++)	for (i = 0; i < l + 1; i++) {	    j = l - i;	    surf->point[i * (w + 1) + k].x = (float) PL_METER *(xstart + (float) x * k / w);	    surf->point[i * (w + 1) + k].y = (float) PL_METER *(ystart + (float) y * j / l);	    surf->point[i * (w + 1) + k].z = (float) PL_METER *zstart;	}}/*exchanges the x and y values of a surface, making y negative *//* This is a patch to get the coords aligned with flight-dynamic's   axes. */void xchgxy (TD_Surface * surf){    int j;    long t;    for (j = 0; j < surf->l * surf->w; j++) {	t = surf->point[j].x;	surf->point[j].x = surf->point[j].y;	surf->point[j].y = -t;    }}/*void gl_triangle (int x1, int y1, int z1, int x2, int y2, int z2,		  int x3, int y3, int z3, int bf);void gl_striangle (int x1, int y1, int x2, int y2, int x3, int y3,		   int c, int bf);*//*returns 0 on error */TD_Solid *PL_init_solid (void){    TD_Solid *plane_demo;    int i;    int n = PL_NUMSURFACES;    if ((plane_demo = malloc (sizeof (TD_Solid))) == NULL)	return 0;    memset (plane_demo, 0, sizeof (TD_Solid));    plane_demo->num_surfaces = n;    if ((plane_demo->surf = calloc (n , sizeof (TD_Surface))) == NULL)	return 0;    for (i = 0; i < n; i++) {	if ((plane_demo->surf[i].point	     = malloc (PL_SURF_SIZE * PL_SURF_SIZE * sizeof (TD_Point))) == NULL)	    return 0;	/*      plane_demo->surf[i].render = TD_MESH_AND_SOLID; *//*can leave out and set option ALL_SAME_RENDER */	plane_demo->surf[i].shadow = TD_DEFAULT_COLOR + TD_DEFAULT_SHADOW;	plane_demo->surf[i].maxcolor = TD_DEFAULT_COLOR + TD_DEFAULT_MAXCOLOR;	plane_demo->surf[i].mesh_color = 191;	/*navy blue in from the palette set */	plane_demo->surf[i].backfacing = 1;	/*don't draw any of surface that faces away */	plane_demo->surf[i].depth_per_color = 6;	/*2^6 = 64 colors in the grey scale */    }    plane_demo->alpha = 0;	/* begin all at zero (flight dynamics */    plane_demo->beta = 0;	/* says plane is level */    plane_demo->gamma = 0;    plane_demo->xlight = -147;	/* lighting out of the screen,... */    plane_demo->ylight = -147;	/* ...to the right,... */    plane_demo->zlight = 147;	/* ...and from the top. */    plane_demo->distance = PL_METER * 35;	/* distance of the camera from the */    /* origin, PL_METER * meters. *//*if PL_TDOPTION_ROTATE_OBJECT is set to zero then we need to   define the full camera position instead: */    plane_demo->x_cam = PL_METER * 35;    plane_demo->y_cam = PL_METER * 0;    plane_demo->z_cam = PL_METER * 0;/* These two are scale factors for the screen: *//* xscale is now calculated so that the maximum volume (-2^15 to 2^15 or   -2^31 to 2^31) will just fit inside the screen width at this distance: */    plane_demo->xscale = (long) plane_demo->distance * PL_screen_width / (32768 * 2);    plane_demo->yscale = (float) plane_demo->xscale * PL_SCREEN_ASPECT	* PL_screen_height / PL_screen_width;	/*to get display aspect square *//*The above gives an average (not to telescopic, and not to wide angle) view *//*use any triangle or linedrawing routine: */    plane_demo->draw_triangle = gl_triangle;    plane_demo->draw_striangle = gl_striangle;    plane_demo->draw_line = gl_line;/* very important to set TDOPTION_INIT_ROTATION_MATRIX if you don't   calculate the rotation matrix yourself. */    plane_demo->option_flags = TDOPTION_INIT_ROTATION_MATRIX	| TDOPTION_ALL_SAME_RENDER | TDOPTION_SORT_SURFACES	| PL_TDOPTION_ROTATE_OBJECT | PL_TDOPTION_LIGHT_SOURCE_CAM;    plane_demo->render = TD_MESH_AND_SOLID;	/*how we want to render it */    return plane_demo;}void PL_init_surfaces (TD_Solid * plane){    int i;/* To see what an example of the ellipsoid initialisation: *//*   TD_initsellipsoid (plane, 0, 0, 0, 0,    PL_METER * 8, PL_METER * 4, PL_METER * 4, 3);   for(i=0;i<6;i++)   TD_initcolor (&plane->surf[i], -256);    return; */    for (i = 0; i < 4; i++) {	initfus (&plane->surf[i], i * TD_PI / 2);    }    for (i = 0; i < 4; i++) {	initfus1 (&plane->surf[i + 4], i * TD_PI / 2);    }    for (i = 0; i < 4; i++) {	initfus2 (&plane->surf[i + 8], i * TD_PI / 2);    }    initwing (&plane->surf[12], 1, 1, 0);    initwing (&plane->surf[13], -1, 1, 0);    initwing (&plane->surf[14], 1, -1, 0);    initwing (&plane->surf[15], -1, -1, 0);    initwing (&plane->surf[16], 1, 1, 1);    initwing (&plane->surf[17], -1, 1, 1);    initwing (&plane->surf[18], 1, -1, 1);    initwing (&plane->surf[19], -1, -1, 1);    initwing (&plane->surf[20], 1, 1, 2);    initwing (&plane->surf[21], -1, 1, 2);    initwing (&plane->surf[22], 1, -1, 2);    initwing (&plane->surf[23], -1, -1, 2);    initwing (&plane->surf[24], 1, 1, 3);    initwing (&plane->surf[25], -1, 1, 3);    initwing (&plane->surf[26], 1, -1, 3);    initwing (&plane->surf[27], -1, -1, 3);    initstab (&plane->surf[28], 1, 1);    initstab (&plane->surf[29], -1, 1);    initstab (&plane->surf[30], 1, -1);    initstab (&plane->surf[31], -1, -1);    initfin (&plane->surf[32], 1);    initfin (&plane->surf[33], -1);    for (i = 0; i < 4; i++) {	initfus3 (&plane->surf[i + 34], i * TD_PI / 2);    }    for (i = 0; i < 4; i++) {	initnacelle (&plane->surf[i + 38], i * TD_PI / 2, -1);    }    for (i = 0; i < 4; i++) {	initnacelle (&plane->surf[i + 42], i * TD_PI / 2, 1);    }    for (i = 0; i < 2; i++) {	initnacelle2 (&plane->surf[i + 46], i * TD_PI / 2, -1);    }    for (i = 0; i < 2; i++) {	initnacelle2 (&plane->surf[i + 48], i * TD_PI / 2, 1);    }    inittips (&plane->surf[50], 1);    inittips (&plane->surf[51], -1);#ifdef WORLD_VIEW    for (i = 0; i < 10; i++)	initplate (&plane->surf[i + 52], -20 + (float) i * 4.44, -20, -3.5, 0.4, 40, 1, 10);#endif    for (i = 0; i < PL_NUMSURFACES; i++) {	xchgxy (&plane->surf[i]);	TD_initcolor (&plane->surf[i], -256);	/*initialises the color vector (vector normal to each point) */    }}/*returns 1 on error */int PL_init_plane (TD_Solid ** plane){    if (!(*plane = PL_init_solid ()))	return 1;    PL_init_surfaces (*plane);    return 0;}void PL_init_palette (void){/*  Here the depth_per_color is 5 (for 64 colors).   256 / 64 gives 4 colors so TD_Surface->color    can be 0, 64, 128, OR 192 */    int i;    unsigned char palette[768];    for (i = 0; i < 64; i++) {	palette[i * 3] = i;	palette[i * 3 + 1] = i;	palette[i * 3 + 2] = 16 + i / 2;    }    for (i = 0; i < 64; i++) {	palette[(i + 64) * 3 + 0] = i;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -