- initial import

git-svn-id: http://moon:8086/svn/mips@1 a8ebac50-d88d-4704-bea3-6648445a41b3
This commit is contained in:
2014-07-20 15:01:37 +00:00
commit 26291bca3d
1549 changed files with 3997969 additions and 0 deletions
+367
View File
@@ -0,0 +1,367 @@
// ------------------------------------------------------------
// bintree.c
// Implementation of a binary tree
//
// 27.02.2005, J.Ahrensfeld
// ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bintree.h"
// ------------------------------------------------------------
node_t* GetParent(node_t *pObj)
{
if (!pObj)
return pObj;
if (!pObj->pParent)
return pObj;
return GetParent(pObj->pParent);
}
node_t* node_init(node_t *pObj, double value, void *pData, int size)
{
node_t *pNode;
pNode = (node_t*)malloc(sizeof(node_t));
pNode->pData = NULL;
if (size)
pNode->pData = malloc(size);
if (pData)
memcpy(pNode->pData, pData, size);
pNode->pParent = pObj;
pNode->pLeft = NULL;
pNode->pRight = NULL;
pNode->value = value;
return pNode;
}
node_t* node_insert(node_t *pObj, double value, void *pData, int size)
{
if(pObj == NULL)
{
return node_init(pObj, value, pData, size);
}
else
{
if(value <= pObj->value)
{
if (pObj->pLeft)
node_insert(pObj->pLeft, value, pData, size);
else
pObj->pLeft = node_init(pObj, value, pData, size);
}
else
{
if (pObj->pRight)
node_insert(pObj->pRight, value, pData, size);
else
pObj->pRight = node_init(pObj, value, pData, size);
}
}
return pObj;
}
node_t* node_lookup(node_t *pObj, double value)
{
return pObj;
}
node_t* node_remove(node_t *pNode)
{
node_t *pMin_leaf;
node_t *pParent;
void *pData;
double value;
if(pNode == NULL)
{
return pNode;
}
pParent = pNode->pParent;
if(!pNode->pLeft && !pNode->pRight)
{
if (pParent)
{
if(pParent->pLeft == pNode)
{
pParent->pLeft = NULL;
}
if(pParent->pRight == pNode)
{
pParent->pRight = NULL;
}
}
}
else
{
if(pNode->pLeft && !pNode->pRight)
{
pNode->pLeft->pParent = pParent;
if (pParent)
{
if(pParent->pLeft == pNode)
pParent->pLeft = pNode->pLeft;
if(pParent->pRight == pNode)
pParent->pRight = pNode->pLeft;
}
else
pParent = pNode->pLeft;
}
else
{
if(!pNode->pLeft && pNode->pRight)
{
pNode->pRight->pParent = pParent;
if (pParent)
{
if(pParent->pLeft == pNode)
pParent->pLeft = pNode->pRight;
if(pParent->pRight == pNode)
pParent->pRight = pNode->pRight;
}
else
pParent = pNode->pRight;
}
else
{
if(pNode->pLeft && pNode->pRight)
{
pMin_leaf = bintree_GetLeafMin(pNode);
value = pMin_leaf->value;
pData = pMin_leaf->pData;
pParent = node_remove(pMin_leaf);
pNode->pData = pData;
pNode->value = value;
}
}
}
}
if (pNode->pData)
free(pNode->pData);
free(pNode);
pNode = NULL;
return GetParent(pParent);
}
// ------------------------------------------------------------
void bintree_print(node_t *pObj)
{
if (pObj == NULL)
return;
bintree_print(pObj->pLeft);
printf("%g ", pObj->value);
bintree_print(pObj->pRight);
}
int bintree_size(node_t *pObj)
{
if (pObj==NULL)
{
return 0;
}
else
{
return(bintree_size(pObj->pLeft) + 1 + bintree_size(pObj->pRight));
}
}
int bintree_maxdepth(node_t *pObj)
{
int lDepth;
int rDepth;
if (pObj==NULL)
{
return(0);
}
else
{
// compute the depth of each subtree
lDepth = bintree_maxdepth(pObj->pLeft);
rDepth = bintree_maxdepth(pObj->pRight);
// use the larger one
if (lDepth > rDepth)
return(lDepth+1);
else
return(rDepth+1);
}
}
double bintree_minvalue(node_t *pObj)
{
node_t *current = pObj;
// loop down to find the leftmost leaf
while (current->pLeft != NULL)
{
current = current->pLeft;
}
return(current->value);
}
double bintree_maxvalue(node_t *pObj)
{
node_t *current = pObj;
// loop down to find the leftmost leaf
while (current->pRight != NULL)
{
current = current->pRight;
}
return(current->value);
}
// ------------------------------------------------------------
node_t *bintree_GetNodeMax(node_t *pObj)
{
node_t *pNode = pObj;
node_t *current = pNode;
if (!pNode)
return pNode;
// loop down to find the leftmost leaf
while (current->pRight != NULL)
{
current = current->pRight;
}
return current;
}
// ------------------------------------------------------------
node_t *bintree_GetNodeMin(node_t *pObj)
{
node_t *pNode = pObj;
node_t *current = pNode;
if (!pNode)
return pNode;
// loop down to find the leftmost leaf
while (current->pLeft != NULL)
{
current = current->pLeft;
}
return current;
}
// ------------------------------------------------------------
node_t *bintree_GetLeafMax(node_t *pObj)
{
node_t *current = pObj;
if(current->pRight != NULL)
current = bintree_GetLeafMax(current->pRight);
else
if(current->pLeft != NULL)
current = bintree_GetLeafMax(current->pLeft);
return current;
}
node_t *bintree_GetLeafMin(node_t *pObj)
{
node_t *current = pObj;
if(current->pLeft != NULL)
current = bintree_GetLeafMin(current->pLeft);
else
if(current->pRight != NULL)
current = bintree_GetLeafMin(current->pRight);
return current;
}
int bintree_isBST(node_t *pObj)
{
if (pObj==NULL)
return(1);
// false if the max of the left is > than us
// (bug -- an earlier version had min/max backwards here)
if (pObj->pLeft != NULL && bintree_maxvalue(pObj->pLeft) > pObj->value)
return(0);
// false if the min of the right is <= than us
if (pObj->pRight !=NULL && bintree_minvalue(pObj->pRight) <= pObj->value)
return(0);
// false if, recursively, the left or right is not a BST
if (!bintree_isBST(pObj->pLeft) || !bintree_isBST(pObj->pRight))
return(0);
// passing all that, it's a BST
return(1);
}
int bintree_isBSTUtil(node_t *pObj, double min, double max)
{
if (pObj==NULL)
return(1);
// false if this node violates the min/max constraint
if (pObj->value<min || pObj->value>max)
return(0);
// otherwise check the subtrees recursively,
// tightening the min or max constraint
return
(
bintree_isBSTUtil(pObj->pLeft, min, pObj->value) &&
bintree_isBSTUtil(pObj->pRight, pObj->value+1, max)
);
}
int bintree_destroy(node_t *pObj)
{
int size;
node_t *pRoot, *pMaxLeaf;
pRoot = pObj;
size = 1;
while(pRoot)
{
size = bintree_size(pRoot);
pMaxLeaf = bintree_GetLeafMax(pRoot);
pRoot = node_remove(pMaxLeaf);
};
if (size != 1)
return -1;
return 0;
}
// ------------------------------------------------------------
+42
View File
@@ -0,0 +1,42 @@
// ------------------------------------------------------------
// bintree.h
// Implementation of a binary tree
//
// 27.02.2005, J.Ahrensfeld
// ------------------------------------------------------------
#ifndef BINTREE_H
#define BINTREE_H
// ------------------------------------------------------------
typedef struct _node_t
{
double value;
void *pData;
struct _node_t *pParent;
struct _node_t *pLeft;
struct _node_t *pRight;
} node_t;
// ------------------------------------------------------------
node_t* node_init(node_t *pObj, double value, void *pData, int size);
node_t* node_insert(node_t *pObj, double value, void *pData, int size);
node_t* node_lookup(node_t *pObj, double value);
node_t* node_remove(node_t *pObj);
void bintree_print(node_t *pObj);
int bintree_size(node_t *pObj);
int bintree_maxdepth(node_t *pObj);
double bintree_minvalue(node_t *pObj);
double bintree_maxvalue(node_t *pObj);
node_t *bintree_GetLeafMax(node_t *pObj);
node_t *bintree_GetLeafMin(node_t *pObj);
int bintree_isBST(node_t *pObj);
int bintree_isBSTUtil(node_t *pObj, double min, double max);
node_t *bintree_GetNodeMax(node_t *pObj);
node_t *bintree_GetNodeMin(node_t *pObj);
int bintree_destroy(node_t *pObj);
// ------------------------------------------------------------
#endif // BINTREE_H
+117
View File
@@ -0,0 +1,117 @@
#include <math.h>
#include "ri.h"
#include "camset.h"
#define min(a,b) ((a)<(b)?(a):(b))
#define PI 3.1415926535897932384626433832795
/* PlaceCamera(): establish a viewpoint, viewing direction and orientation
* for a scene. This routine must be called before RiWorldBegin().
* position: a point giving the camera position
* direction: a point giving the camera direction relative to position
* roll: an optional rotation of the camera about its direction axis
*/
void PlaceCamera(RtPoint position, RtPoint direction, float roll)
{
RiIdentity(); /* Initialize the camera transformation */
if (roll != 0.0)
RiRotate(-roll, 0.0, 0.0, 1.0);
AimZ(direction);
RiTranslate(-position[0], -position[1], -position[2]);
}
void AimZ(RtPoint direction)
{
double xzlen, yzlen, yrot, xrot;
if (direction[0]==0 && direction[1]==0 && direction[2]==0)
return;
/*
* The initial rotation about the y axis is given by the projection of
* the direction vector onto the x,z plane: the x and z components
* of the direction.
*/
xzlen = sqrt(direction[0]*direction[0]+direction[2]*direction[2]);
if (xzlen == 0)
yrot = (direction[1] < 0) ? 180 : 0;
else
yrot = 180*acos(direction[2]/xzlen)/PI;
/*
* The second rotation, about the x axis, is given by the projection on
* the y,z plane of the y-rotated direction vector: the original y
* component, and the rotated x,z vector from above.
*/
yzlen = sqrt(direction[1]*direction[1]+xzlen*xzlen);
xrot = 180*acos(xzlen/yzlen)/PI; /* yzlen should never be 0 */
if (xrot != 0.0)
{
if (direction[1] > 0)
RiRotate(xrot, 1.0, 0.0, 0.0);
else
RiRotate(-xrot, 1.0, 0.0, 0.0);
}
/* The last rotation declared gets performed first */
if (yrot != 0.0)
{
if (direction[0] > 0)
RiRotate(-yrot, 0.0, 1.0, 0.0);
else
RiRotate(yrot, 0.0, 1.0, 0.0);
}
}
/* FrameCamera(): give physical parameters for a camera.
Parameters:
focallength: the "camera's" focal length.
framewidth: the width of the film frame
frameheight: the height of the film frame
*/
void FrameCamera(float focallength, float framewidth, float frameheight)
{
RtFloat normwidth, normheight;
/* Focal length of 0 is taken to be an orthographic projection */
if (focallength != 0.0)
{
RiProjection("perspective", RI_NULL);
normwidth = (framewidth*.5)/focallength;
normheight = (frameheight*.5)/focallength;
}
else
{
RiProjection("orthographic", RI_NULL);
normwidth = framewidth*.5;
normheight = frameheight*.5;
}
RiScreenWindow(-normwidth, normwidth, -normheight, normheight);
}
/* FrameCamera2(): give physical parameters for a camera.
* Parameters:
* focallength: controls the width of the camera's field of view.
* framewidth: the width of the film image
* frameheight: the height of the film image*/
void FrameCamera2(float focallength, float framewidth, float frameheight)
{
RtFloat fov;
/* A nonzero focal length is taken to be a "normal" lens */
if(focallength != 0.0)
{
fov = 2 * atan((min(framewidth,frameheight)*.5)/focallength) * 180.0/3.14159;
RiProjection("perspective", RI_FOV, (RtPointer)&fov, RI_NULL);
}
else
{
RiProjection("orthographic", RI_NULL);
}
RiFrameAspectRatio((RtFloat)(framewidth/frameheight));
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef CAMSET_H
#define CAMSET_H
//#include "ri.h"
void PlaceCamera(RtPoint position, RtPoint direction, float roll);
void AimZ(RtPoint direction);
void FrameCamera(float focallength, float framewidth, float frameheight);
void FrameCamera2(float focallength, float framewidth, float frameheight);
#endif // CAMSET_H
+36
View File
@@ -0,0 +1,36 @@
// ------------------------------------------------------------
// colortypes.h
//
// Reading writing tiff images
//
// 12.03.2005, J.Ahrensfeld
// ------------------------------------------------------------
#ifndef COLORTYPES_H
#define COLORTYPES_H
// ------------------------------------------------------------
#define COMP_RED 0
#define COMP_GREEN 1
#define COMP_BLUE 2
#define COMP_L 0
#define COMP_A 1
#define COMP_B 2
#define COORD_X 0
#define COORD_Y 1
typedef double color_float_t;
typedef color_float_t color_t[3];
typedef color_float_t color_vector_t[3];
typedef color_t color_matrix_t[3];
typedef color_float_t rgb_t[3];
typedef color_float_t rgb_vector_t[3];
typedef rgb_t rgb_matrix_t[3];
typedef color_float_t rgba_t[4];
typedef color_float_t rgba_vector_t[4];
typedef rgba_t rgba_matrix_t[4];
// ------------------------------------------------------------
#endif // COLORTYPES_H
+404
View File
@@ -0,0 +1,404 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "ri.h"
#include "types.h"
#include "matrix.h"
#include "graph_state.h"
#include "stack.h"
#include "object.h"
#include "vars.h"
#include "engine.h"
#include "imageio.h"
#include "colortypes.h"
#include "render.h"
#include "bintree.h"
RtToken RI_P = NULL;
ri_var_t* V_P = NULL;
RtToken RI_PZ = NULL;
ri_var_t* V_PZ = NULL;
RtToken RI_PW = NULL;
ri_var_t* V_PW = NULL;
RtToken RI_N = NULL;
ri_var_t* V_N = NULL;
RtToken RI_NP = NULL;
ri_var_t* V_NP = NULL;
RtToken RI_CS = NULL;
ri_var_t* V_CS = NULL;
RtToken RI_OS = NULL;
ri_var_t* V_OS = NULL;
RtToken RI_S = NULL;
ri_var_t* V_S = NULL;
RtToken RI_T = NULL;
ri_var_t* V_T = NULL;
RtToken RI_ST = NULL;
ri_var_t* V_ST = NULL;
RtToken RI_WIDTH = NULL;
ri_var_t* V_WIDTH = NULL;
RtToken RI_CONSTANTWIDTH = NULL;
ri_var_t* V_CONSTANTWIDTH = NULL;
typedef struct _sjmalloc_t
{
UINT32 size;
void* pMem;
struct _sjmalloc_t *pPrev;
struct _sjmalloc_t *pNext;
} jmalloc_t;
linkedlist_t *pMallocs = NULL;
UINT32 nmallocs = 0;
// Renderer functions
void* jmalloc(UINT32 size)
{
void *pMem;
pMem = malloc(size);
// pMallocs = LinkedList_add(pMallocs, (UINT32)pMem, (void*)&size, sizeof(void*));
nmallocs++;
return pMem;
}
void jfree(void *pMem)
{
// pMallocs = LinkedList_del(LinkedList_find_by_id(pMallocs, (UINT32)pMem));
nmallocs--;
free(pMem);
}
void jstats()
{
linkedlist_t *pList;
UINT32 n= 0, size = 0;
pList = LinkedList_find_first(pMallocs);
while(pList)
{
size += (UINT32)pList->size;
pList = pList->pNext;
n++;
}
fprintf(stdout, "Memory leak : %d bytes, n=%d, count=%d\n", size, n, nmallocs);
}
void Tic(double *tic_start)
{
*tic_start = (double)clock();
}
double Toc(double *tic_start)
{
double finish = (double)clock();
return (finish - *tic_start) / CLOCKS_PER_SEC;
}
void Engine_init(engine_t *pObj, RtToken name)
{
ri_var_t *pVar;
pObj->pContext_stack = Stack_push(NULL, &pObj->pContext, 2*sizeof(context_t));
VarListInit(&pObj->varList);
pObj->pFile = stderr;
if (name != RI_NULL)
pObj->pFile = fopen(name, "wb");
// Init predefined variable
V_P = VarListDeclare(&pObj->varList, "P", "varying", "point");
RI_P = V_P->name;
V_PZ = VarListDeclare(&pObj->varList, "Pz", "varying", "float");
RI_PZ = V_PZ->name;
V_PW = VarListDeclare(&pObj->varList, "Pw", "varying", "float");
RI_PW = V_PW->name;
V_N = VarListDeclare(&pObj->varList, "N", "varying", "point");
RI_N = V_N->name;
V_NP = VarListDeclare(&pObj->varList, "Np", "uniform", "point");
RI_NP = V_NP->name;
V_CS = VarListDeclare(&pObj->varList, "Cs", "varying", "color");
RI_CS = V_CS->name;
V_OS = VarListDeclare(&pObj->varList, "Os", "varying", "color");
RI_OS = V_OS->name;
V_S = VarListDeclare(&pObj->varList, "s", "varying", "float");
RI_S = V_S->name;
V_T = VarListDeclare(&pObj->varList, "t", "varying", "float");
RI_T = V_T->name;
V_ST = VarListDeclare(&pObj->varList, "st", "varying", "float");
RI_ST = V_ST->name;
V_WIDTH = VarListDeclare(&pObj->varList, "width", "varying", "float");
RI_WIDTH = V_WIDTH->name;
V_CONSTANTWIDTH = VarListDeclare(&pObj->varList, "constantwidth", "constant", "float");
RI_CONSTANTWIDTH = V_CONSTANTWIDTH->name;
Engine_context_set(pObj, ENGINE_CONTEXT_INVALID, ENGINE_CONTEXT_LEVEL_MAIN);
}
void Engine_free(engine_t *pObj)
{
if (pObj->pContext[ENGINE_CONTEXT_LEVEL_MAIN] == ENGINE_CONTEXT_INVALID)
return;
pObj->pContext_stack = Stack_free(pObj->pContext_stack);
Engine_context_set(pObj, ENGINE_CONTEXT_INVALID, ENGINE_CONTEXT_LEVEL_MAIN);
VarListFree(&pObj->varList);
}
void Engine_context_set(engine_t *pObj, UINT32 context, UINT32 level)
{
pObj->pContext[level] = context;
}
UINT32 Engine_context_get(engine_t *pObj, UINT32 level)
{
if (!pObj->pContext)
return ENGINE_CONTEXT_INVALID;
return pObj->pContext[level];
}
void Engine_context_push(engine_t *pObj)
{
pObj->pContext_stack = Stack_push(pObj->pContext_stack, &pObj->pContext, 2*sizeof(context_t));
}
void Engine_context_pop(engine_t *pObj)
{
pObj->pContext_stack = Stack_pop(pObj->pContext_stack, &pObj->pContext);
}
void Engine_Render(engine_t *pObj)
{
render_t render;
graph_state_t *pG = &pObj->gs;
scene_t *pW = &pObj->geometry;
linkedlist_t *pObjList;
object_t *pMesh;
object_t *pObject;
RtFloat kf, fi;
UINT32 im_width, im_height, x, y, i, j;
image_t im;
rgba_t *pCs;
double tictoc;
Tic(&tictoc);
kf = (RtFloat)pG->pOpt->format.xres/(pG->pOpt->format.yres*pG->pOpt->format.ar_f);
im_width = (UINT32)pG->pOpt->format.xres;
im_height = (UINT32)(kf*pG->pOpt->format.yres + 0.5);
if (pG->pOpt->display.mode == RI_RGBA)
ImageInit(&im, im_width, im_height, 4);
else
ImageInit(&im, im_width, im_height, 3);
render.pG = pG;
render.pImage = &im;
render.pLightList = pObj->lights.pObjs;
render.ppPixel = (shaded_pixel_t**)malloc(im_width*sizeof(shaded_pixel_t*));
for (i=0; i < im_width; i++)
{
render.ppPixel[i] = (shaded_pixel_t*)malloc(im_height*sizeof(shaded_pixel_t));
memset(render.ppPixel[i], 0, im_height*sizeof(shaded_pixel_t));
for (j=0; j < im_height; j++)
render.ppPixel[i][j].z = +RI_INFINITY;
}
// Get screen width
render.screen_u = (RtFloat)fabs(pG->pOpt->screen[1]-pG->pOpt->screen[0]);
render.screen_v = (RtFloat)fabs(pG->pOpt->screen[3]-pG->pOpt->screen[2]);
// Get focal length
fi = (RtFloat)(1.0/tan(0.5*PI*pG->pOpt->projection.fov/180));
// Calculate screen factors
render.ku = (RtFloat)(fi/render.screen_u);
render.kv = (RtFloat)(fi/render.screen_v);
// ToDo: PixelAspectRatio, FrameAspectRatio
pObjList = LinkedList_find_first(pW->pObjs);
while(pObjList)
{
pObject = (object_t*)LinkedList_get(pObjList);
if (pObject->type == OBJECT_TYPE_POLYGON)
{
RenderWire_polygon(&render, pObject);
}
if (pObject->type == OBJECT_TYPE_PATCH)
{
Render_patch(&render, pObject);
}
if (pObject->type == OBJECT_TYPE_PATCHMESH)
{
Render_patchmesh(&render, pObject);
}
if (pObject->type == OBJECT_TYPE_SPHERE)
{
Render_sphere(&render, pObject);
}
if (pObject->type == OBJECT_TYPE_CURVE)
{
}
pObjList = pObjList->pNext;
}
for (x=0; x < im_width; x++)
{
for (y=0; y < im_height; y++)
{
if (render.ppPixel[x][y].z == +RI_INFINITY)
continue;
ImageSetPixel(&im, x, y, render.ppPixel[x][y].cs);
}
}
for (i=0; i < im_width; i++)
{
free(render.ppPixel[i]);
}
free(render.ppPixel);
// ImageSaveTiff(&im, pG->pOpt->display.name);
ImageFree(&im);
printf("Scene rendering time : %.2g s\n", Toc(&tictoc));
}
// --------------------------------------------------------------
void Engine_DeleteScene(engine_t *pObj)
{
object_t *pObject;
scene_t *pW = &pObj->geometry;
context_t main_context;
main_context = Engine_context_get(pObj, ENGINE_CONTEXT_LEVEL_MAIN);
pW->pObjs = LinkedList_find_first(pW->pObjs);
while (pW->pObjs)
{
pObject = (object_t*)LinkedList_get(pW->pObjs);
if (pObject->main_context != main_context)
{
if (pW->pObjs->pNext)
{
pW->pObjs = pW->pObjs->pNext;
continue;
}
else
break;
}
if (pObject->type == OBJECT_TYPE_POLYGON)
{
Polygon_free(pObject);
pW->stat.num_polygons--;
}
if (pObject->type == OBJECT_TYPE_PATCH)
{
Patch_free(pObject);
pW->stat.num_patches--;
}
if (pObject->type == OBJECT_TYPE_PATCHMESH)
{
Patchmesh_free(pObject);
pW->stat.num_patchmeshes--;
}
if (pObject->type == OBJECT_TYPE_CURVE)
{
Curve_free(pObject);
pW->stat.num_curves--;
}
if (pObject->type == OBJECT_TYPE_SPHERE)
{
Sphere_free(pObject);
pW->stat.num_spheres--;
}
pW->pObjs = LinkedList_del(pW->pObjs);
pW->num_objs--;
}
}
// --------------------------------------------------------------
void Engine_SceneInfo(engine_t *pObj)
{
UINT32 num_verts = 0, num_objects = 0;
linkedlist_t *pObjList;
polygon_t *pPoly;
object_t *pObject;
scene_t *pW = &pObj->geometry;
scene_t *pL = &pObj->lights;
pObjList = LinkedList_find_first(pW->pObjs);
while(pObjList)
{
pObject = (object_t*)LinkedList_get(pObjList);
if (pObject)
{
if (pObject->type == OBJECT_TYPE_POLYGON)
{
pPoly = (polygon_t*)pObject->pObjData;
num_verts += pPoly->num_verts;
}
printf("Time statistics for %s [%d]\n", pObject->type, pObject->id);
printf("Dicing : %.2g s\n", pObject->stat.time_dice);
printf("Displacing : %.2g s\n", pObject->stat.time_displace);
printf("Transform : %.2g s\n", pObject->stat.time_transform);
printf("Shading : %.2g s\n", pObject->stat.time_shade);
printf("Rendering : %.2g s\n", pObject->stat.time_render);
printf("Total : %.2g s\n", pObject->stat.time_dice + pObject->stat.time_displace + pObject->stat.time_transform + pObject->stat.time_shade + pObject->stat.time_render);
}
pObjList = pObjList->pNext;
}
printf("Frame # %d\n", pObj->gs.frame);
printf("Scene contains %d objects\n", pW->num_objs);
printf(" %d lights\n", pL->stat.num_lights);
printf(" %d polygons\n", pW->stat.num_polygons);
printf(" %d patches\n", pW->stat.num_patches);
printf(" %d patchmeshes\n", pW->stat.num_patchmeshes);
printf(" %d spheres\n", pW->stat.num_spheres);
printf(" %d curves\n", pW->stat.num_curves);
printf(" %d vertices\n", num_verts);
}
+69
View File
@@ -0,0 +1,69 @@
#ifndef ENGINE_H
#define ENGINE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENGINE_CONTEXT_LEVEL_MAIN 0
#define ENGINE_CONTEXT_LEVEL_SUB 1
#define ENGINE_CONTEXT_INVALID 0
#define ENGINE_CONTEXT_MAIN 1
#define ENGINE_CONTEXT_FRAME 2
#define ENGINE_CONTEXT_WORLD 3
#define ENGINE_CONTEXT_ATTRIBUTE 4
#define ENGINE_CONTEXT_TRANSFORM 5
#define ENGINE_CONTEXT_SOLID 6
#define ENGINE_CONTEXT_OBJECT 7
#define ENGINE_CONTEXT_MOTION 8
extern ri_var_t* V_P;
extern ri_var_t* V_PZ;
extern ri_var_t* V_PW;
extern ri_var_t* V_N;
extern ri_var_t* V_NP;
extern ri_var_t* V_CS;
extern ri_var_t* V_OS;
extern ri_var_t* V_S;
extern ri_var_t* V_T;
extern ri_var_t* V_ST;
extern ri_var_t* V_WIDTH;
extern ri_var_t* V_CONSTANTWIDTH;
typedef UINT32 context_t;
typedef struct _sengine_t
{
FILE *pFile;
RtToken name;
context_t *pContext;
stack_t *pContext_stack;
var_list_t varList;
scene_t geometry;
scene_t lights;
graph_state_t gs;
void *pErrorHandler;
} engine_t;
// JayMan functions
void Engine_init(engine_t *pObj, RtToken name);
void Engine_free(engine_t *pObj);
void Engine_context_set(engine_t *pObj, UINT32 context, UINT32 level);
UINT32 Engine_context_get(engine_t *pObj, UINT32 level);
void Engine_context_push(engine_t *pObj);
void Engine_context_pop(engine_t *pObj);
void Engine_Render(engine_t *pObj);
void Engine_DeleteScene(engine_t *pObj);
void Engine_SceneInfo(engine_t *pObj);
void* jmalloc(UINT32 size);
void jfree(void *pMem);
void jstats(void);
void Tic(double *tic_start);
double Toc(double *tic_start);
#endif // ENGINE_H
+102
View File
@@ -0,0 +1,102 @@
#include <stdlib.h>
#include <string.h>
#include "ri.h"
#include "types.h"
#include "stack.h"
#include "graph_state.h"
#include "object.h"
#include "matrix.h"
#include "vars.h"
#include "engine.h"
static char RI_DEFAULT_FILNAME[] = "ri.pic";
// Graphics state functions
void GS_init(graph_state_t *pObj)
{
UINT32 i;
pObj->pAttr_stack = Stack_push(NULL, &pObj->pAttr, sizeof(attr_t));
pObj->pOpt_stack = Stack_push(NULL, &pObj->pOpt, sizeof(opt_t));
pObj->pXform_stack = Stack_push(NULL, &pObj->pXform, sizeof(xform_t));
// Initialize coordinate systems
for (i=0; i < NUM_COORD_SYSTEMS; i++)
{
IdentityMatrix(&pObj->pXform->space[i]);
}
// Init options
memset(pObj->pOpt, 0, sizeof(opt_t));
pObj->pOpt->display.name = RI_DEFAULT_FILNAME;
pObj->pOpt->display.type = RI_FILE;
pObj->pOpt->display.mode = RI_RGBA;
pObj->pOpt->format.xres = 640;
pObj->pOpt->format.yres = 480;
pObj->pOpt->format.ar_f = (RtFloat)1;
pObj->pOpt->format.ar_p = (RtFloat)1;
pObj->pOpt->crop[0] = (RtFloat)0;
pObj->pOpt->crop[1] = (RtFloat)1;
pObj->pOpt->crop[2] = (RtFloat)0;
pObj->pOpt->crop[3] = (RtFloat)1;
pObj->pOpt->screen[0] = (RtFloat)-1.33;
pObj->pOpt->screen[1] = (RtFloat)+1.33;
pObj->pOpt->screen[2] = (RtFloat)-1.00;
pObj->pOpt->screen[3] = (RtFloat)+1.00;
pObj->pOpt->projection.type = RI_PERSPECTIVE;
pObj->pOpt->projection.fov = (RtFloat)90;
pObj->pAttr->cs[0] = 1.0;
pObj->pAttr->cs[1] = 1.0;
pObj->pAttr->cs[2] = 1.0;
pObj->pAttr->ubasis = (RtPointer)RiBezierBasis;
pObj->pAttr->vbasis = (RtPointer)RiBezierBasis;
pObj->pAttr->ustep = RI_BEZIERSTEP;
pObj->pAttr->vstep = RI_BEZIERSTEP;
pObj->pAttr->nsides = 1;
}
void GS_free(graph_state_t *pObj)
{
pObj->pAttr_stack = Stack_free(pObj->pAttr_stack);
pObj->pOpt_stack = Stack_free(pObj->pOpt_stack);
pObj->pXform_stack = Stack_free(pObj->pXform_stack);
}
void GS_opt_push(graph_state_t *pObj)
{
pObj->pOpt_stack = Stack_push(pObj->pOpt_stack, (void*)&pObj->pOpt, sizeof(opt_t));
}
void GS_opt_pop(graph_state_t *pObj)
{
pObj->pOpt_stack = Stack_pop(pObj->pOpt_stack, (void*)&pObj->pOpt);
}
void GS_attr_push(graph_state_t *pObj)
{
pObj->pAttr_stack = Stack_push(pObj->pAttr_stack, (void*)&pObj->pAttr, sizeof(attr_t));
}
void GS_attr_pop(graph_state_t *pObj)
{
pObj->pAttr_stack = Stack_pop(pObj->pAttr_stack, (void*)&pObj->pAttr);
}
void GS_xform_push(graph_state_t *pObj)
{
pObj->pXform_stack = Stack_push(pObj->pXform_stack, (void*)&pObj->pXform, sizeof(xform_t));
GS_xform_set(pObj, pObj->curr_xform);
}
void GS_xform_pop(graph_state_t *pObj)
{
pObj->pXform_stack = Stack_pop(pObj->pXform_stack, (void*)&pObj->pXform);
GS_xform_set(pObj, pObj->curr_xform);
}
void GS_xform_set(graph_state_t *pObj, UINT32 id)
{
pObj->curr_xform = id;
pObj->pXform_curr = &pObj->pXform->space[id];
}
+96
View File
@@ -0,0 +1,96 @@
/*************************************************************************/
/* Types.h
/*************************************************************************/
#ifndef GRAPH_STATE_H
#define GRAPH_STATE_H
#include "types.h"
#include "stack.h"
#define PI 3.1415926535897932384626433832795
#define NUM_COORD_SYSTEMS 6
typedef struct _sdisplay_t
{
char *name;
RtToken type;
RtToken mode;
} display_t;
typedef struct _sprojection_t
{
RtToken type;
RtFloat fov;
UINT32 flags;
} projection_t;
typedef struct _sformat_t
{
RtInt xres;
RtInt yres;
RtFloat ar_f;
RtFloat ar_p;
} format_t;
typedef struct _sopt_t
{
display_t display;
projection_t projection;
format_t format;
RtFloat crop[4];
RtFloat screen[4];
RtFloat clipping[2];
} opt_t;
typedef struct _sattr_t
{
RtColor cs;
RtFloat shadingrate;
RtPointer ubasis, vbasis;
RtInt ustep, vstep;
RtInt nsides;
} attr_t;
typedef struct _sxform_t
{
RtMatrix space[NUM_COORD_SYSTEMS];
} xform_t;
typedef struct _sgraph_state_t
{
UINT32 frame;
UINT32 context;
UINT32 curr_xform;
opt_t *pOpt;
attr_t *pAttr;
xform_t *pXform;
stack_t *pOpt_stack;
stack_t *pAttr_stack;
stack_t *pXform_stack;
RtMatrix *pXform_curr;
} graph_state_t;
enum eSpaces
{
object_space = 0, world_space, camera_space, screen_space, raster_space, ndc_space
};
void GS_init(graph_state_t *pObj);
void GS_free(graph_state_t *pObj);
void GS_opt_push(graph_state_t *pObj);
void GS_opt_pop(graph_state_t *pObj);
void GS_attr_push(graph_state_t *pObj);
void GS_attr_pop(graph_state_t *pObj);
void GS_xform_push(graph_state_t *pObj);
void GS_xform_pop(graph_state_t *pObj);
void GS_xform_set(graph_state_t *pObj, UINT32 id);
#endif // GRAPH_STATE_H
+412
View File
@@ -0,0 +1,412 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ri.h"
#include "types.h"
#include "matrix.h"
#include "linklist.h"
#include "vars.h"
#include "grid.h"
#include "graph_state.h"
#include "object.h"
void Grid_create(grid_t *pObj, UINT32 npu, UINT32 npv)
{
UINT32 i, j, k, nu, nv;
ri_var_t *pVar;
nu = npu + 1;
nv = npv + 1;
pObj->nu = nu;
pObj->nv = nv;
pObj->npoints = nu*nv;
VarListInit(&pObj->vars);
pObj->pPointPos = (pixelpos_t*)malloc(nu*nv*sizeof(pixelpos_t));
memset(pObj->pPointPos, 0, nu*nv*sizeof(pixelpos_t));
pObj->pPointState = (point_state_t*)malloc(nu*nv*sizeof(point_state_t));
memset(pObj->pPointState, 0, nu*nv*sizeof(point_state_t));
k=0;
for (i=0; i < nv; i++)
{
for (j=0; j < nu; j++)
{
pObj->pPointPos[k][0] = j;
pObj->pPointPos[k][1] = i;
k++;
}
}
for (i=0; i < (nv-1)*nu; i += nu)
{
for (j=0; j < nu-1; j++)
{
pObj->pPointState[i+j].has_face = 1;
}
}
for (i=0; i < pObj->npoints; i ++)
{
pObj->pPointState[i].visible = 1;
}
}
void Grid_free(grid_t *pObj)
{
if (pObj->pPointPos)
free(pObj->pPointPos);
if (pObj->pPointState)
free(pObj->pPointState);
// VarListDelByName(&pObj->vars, "P");
// VarListPrint(&pObj->vars);
VarListFree(&pObj->vars);
}
void Grid_CopyVarByName(grid_t *pObj, RtToken pDstName, RtToken pSrcName)
{
ri_var_t *pSrc, *pDst;
RtPointer pSrcData, pDstData;
pSrc = VarListFindByName(&pObj->vars, pSrcName);
pDst = VarListFindByName(&pObj->vars, pDstName);
if (pDst)
{
if (pSrc->etype != pDst->etype)
{
fprintf(stderr, "Grid_CopyVar(): Variables are not of same type!\n");
return;
}
if (pSrc->nvalues != pDst->nvalues)
{
fprintf(stderr, "Grid_CopyVar(): Variables are not of same size!\n");
return;
}
}
else
{
pDst = VarListDeclare(&pObj->vars, pDstName, pSrc->cls, pSrc->type);
VarAlloc(pDst, pSrc->nvalues);
}
pSrcData = VarGet(pSrc);
pDstData = VarGet(pDst);
memcpy(pDstData, pSrcData, pSrc->value_size*pSrc->nvalues);
}
void Grid_InitUV(grid_t *pObj)
{
UINT32 i, j, k, nu, nv;
RtFloat *pU, *pV, u, v, du, dv;
ri_var_t *pVar;
nu = pObj->nu;
nv = pObj->nv;
du = (RtFloat)1/(nu-1);
dv = (RtFloat)1/(nv-1);
pVar = VarListDeclare(&pObj->vars, "u", "varying", "float");
pU = (RtFloat*)VarAlloc(pVar, nu*nv);
pVar = VarListDeclare(&pObj->vars, "v", "varying", "float");
pV = (RtFloat*)VarAlloc(pVar, nu*nv);
pVar = VarListDeclare(&pObj->vars, "du", "uniform", "float");
VarSet(pVar, (RtPointer)&du, 1);
pVar = VarListDeclare(&pObj->vars, "dv", "uniform", "float");
VarSet(pVar, (RtPointer)&dv, 1);
// Init uv-coords
k = 0;
v = 0;
for (i=0; i < nv; i++)
{
u = 0;
for (j=0; j < nu; j++)
{
pU[k] = u;
pV[k] = v;
u += du;
k++;
}
v += dv;
}
}
void Grid_assign_derivs(grid_t *pObj)
{
UINT32 i, j, k, nu, nv;
RtPoint *pPoint, *pDPDU, *pDPDV, dpd2;
RtFloat du, dv, ik;
ri_var_t *pVar;
nu = pObj->nu;
nv = pObj->nv;
pPoint = (RtPoint*)VarGetByName(&pObj->vars, "P");
du = *((RtFloat*)VarGetByName(&pObj->vars, "du"));
dv = *((RtFloat*)VarGetByName(&pObj->vars, "dv"));
pDPDU = (RtPoint*)VarGetByName(&pObj->vars, "dpdu");
if (!pDPDU)
pDPDU = (RtPoint*)VarAlloc(VarListDeclare(&pObj->vars, "dpdu", "varying", "point"), pObj->npoints);
pDPDV = (RtPoint*)VarGetByName(&pObj->vars, "dpdv");
if (!pDPDV)
pDPDV = (RtPoint*)VarAlloc(VarListDeclare(&pObj->vars, "dpdv", "varying", "point"), pObj->npoints);
for (i=0; i < pObj->npoints; i += nu)
{
for (j=0; j < nu-1; j++)
{
ik = (RtFloat)1/du;
for (k=0; k < 3; k++)
{
pDPDU[i+j][k] = ik*(pPoint[i+j+1][k] - pPoint[i+j][k]);
}
}
for (k=0; k < 3; k++)
{
pDPDU[i+j][k] = 2*pDPDU[i+j-1][k] - pDPDU[i+j-2][k];
}
}
for (i=0; i < nu; i++)
{
for (j=0; j < (nv-1)*nu; j += nu)
{
ik = (RtFloat)1/dv;
for (k=0; k < 3; k++)
{
pDPDV[i+j][k] = ik*(pPoint[i+j+nu][k] - pPoint[i+j][k]);
}
}
for (k=0; k < 3; k++)
{
pDPDV[i+j][k] = 2*pDPDV[i+j-nu][k] - pDPDV[i+j-2*nu][k];
}
}
}
void Grid_calc_normal(grid_t *pObj, RtToken name)
{
UINT32 i;
RtFloat *a, *b;
RtPoint *pPoint, *pDPDU, *pDPDV, *pN;
pDPDU = (RtPoint*)VarGetByName(&pObj->vars, "dpdu");
pDPDV = (RtPoint*)VarGetByName(&pObj->vars, "dpdv");
pN = (RtPoint*)VarGetByName(&pObj->vars, name);
if (!pN)
pN = (RtPoint*)VarAlloc(VarListDeclare(&pObj->vars, name, "varying", "point"), pObj->npoints);
for (i=0; i < pObj->npoints; i++)
{
a = pDPDU[i];
b = pDPDV[i];
pN[i][0] = a[1]*b[2] - a[2]*b[1];
pN[i][1] = a[2]*b[0] - a[0]*b[2];
pN[i][2] = a[0]*b[1] - a[1]*b[0];
}
}
void Grid_displace(grid_t *pObj)
{
UINT32 i, j, k;
RtPoint *pP, *pDPDU, *pDPDV, *pN;
RtColor *pCs, *pCi;
RtFloat *pU, *pV, fd, kn, kd;
pP = (RtPoint*)VarGetByName(&pObj->vars, "P");
pU = (RtFloat*)VarGetByName(&pObj->vars, "u");
pV = (RtFloat*)VarGetByName(&pObj->vars, "v");
pDPDU = (RtPoint*)VarGetByName(&pObj->vars, "dpdu");
pDPDV = (RtPoint*)VarGetByName(&pObj->vars, "dpdv");
pN = (RtPoint*)VarGetByName(&pObj->vars, "Ng");
kd = 0.00;
for (i=0; i < pObj->npoints; i++)
{
if (!pObj->pPointState[i].visible)
continue;
kn = (RtFloat)1/Norm((RtFloat*)pN[i], 3);
fd = cos(10*3.141592654*pV[i]);
pP[i][0] += kd*fd*pN[i][0]*kn;
pP[i][1] += kd*fd*pN[i][1]*kn;
pP[i][2] += kd*fd*pN[i][2]*kn;
}
Grid_assign_derivs(pObj);
Grid_calc_normal(pObj, "Ng");
}
void Normalize(RtPoint N, RtPoint Nn)
{
RtFloat k;
k = (RtFloat)1/Norm((RtFloat*)N, 3);
Nn[0] = N[0]*k;
Nn[1] = N[1]*k;
Nn[2] = N[2]*k;
}
void Negate(RtPoint N, RtPoint Ni)
{
Ni[0] = -N[0];
Ni[1] = -N[1];
Ni[2] = -N[2];
}
RtFloat* Diffuse(linkedlist_t *pLightList, RtPoint N, RtPoint P)
{
static RtFloat result[3];
RtPoint L, Ln, Nn;
linkedlist_t *pLights;
object_t *pLight;
RtFloat k, kn;
pLights = LinkedList_find_first(pLightList);
memset(result, 0, sizeof(RtColor));
Normalize(N, Nn);
while(pLights)
{
pLight = (object_t*)LinkedList_get(pLights);
L[0] = (*pLight->pXform)[0][3] - P[0];
L[1] = (*pLight->pXform)[1][3] - P[1];
L[2] = (*pLight->pXform)[2][3] - P[2];
Normalize(L, Ln);
k = MAX(ScalarProduct(Ln, Nn, 3), 0);
result[0] += pLight->pAttr->cs[0] * k;
result[1] += pLight->pAttr->cs[1] * k;
result[2] += pLight->pAttr->cs[2] * k;
pLights = pLights->pNext;
}
return result;
}
RtFloat* Specular(linkedlist_t *pLightList, RtPoint N, RtPoint P, RtPoint I, RtFloat roughness)
{
static RtFloat result[3];
RtPoint L, H, Hn, Ln, Nn, In, Ini;
linkedlist_t *pLights;
object_t *pLight;
RtFloat k, kn;
pLights = LinkedList_find_first(pLightList);
memset(result, 0, sizeof(RtColor));
Normalize(N, Nn);
Normalize(I, In);
Negate(In, Ini);
while(pLights)
{
pLight = (object_t*)LinkedList_get(pLights);
L[0] = (*pLight->pXform)[0][3] - P[0];
L[1] = (*pLight->pXform)[1][3] - P[1];
L[2] = (*pLight->pXform)[2][3] - P[2];
Normalize(L, Ln);
H[0] = Ini[0] + Ln[0];
H[1] = Ini[1] + Ln[1];
H[2] = Ini[2] + Ln[2];
Normalize(H, Hn);
k = pow(MAX(ScalarProduct(Nn, Hn, 3), 0), (RtFloat)1/roughness);
result[0] += pLight->pAttr->cs[0] * k;
result[1] += pLight->pAttr->cs[1] * k;
result[2] += pLight->pAttr->cs[2] * k;
pLights = pLights->pNext;
}
return result;
}
void Grid_shade(grid_t *pObj, RtMatrix *pXform, linkedlist_t *pLights)
{
UINT32 i, j, k;
RtPoint *pP, *pDPDU, *pDPDV, *pN, *pI;
RtColor *pCs, *pCi;
RtFloat *pU, *pV, fd, kn, vt, p[4], d[4], *pCl_diff, *pCl_spec;
RtFloat kd, ks;
pP = (RtPoint*)VarGetByName(&pObj->vars, "P");
pU = (RtFloat*)VarGetByName(&pObj->vars, "u");
pV = (RtFloat*)VarGetByName(&pObj->vars, "v");
pCs = (RtColor*)VarGetByName(&pObj->vars, "Cs");
pDPDU = (RtPoint*)VarGetByName(&pObj->vars, "dpdu");
pDPDV = (RtPoint*)VarGetByName(&pObj->vars, "dpdv");
pN = (RtPoint*)VarGetByName(&pObj->vars, "Ng");
pI = (RtPoint*)VarGetByName(&pObj->vars, "I");
pCi = (RtColor*)VarAlloc(VarListDeclare(&pObj->vars, "Ci", "varying", "color"), pObj->npoints);
p[3] = (RtFloat)1.0;
kd = 1;
ks = 0.4;
for (i=0; i < pObj->npoints; i++)
{
if (!pObj->pPointState[i].visible)
continue;
pCl_diff = Diffuse(pLights, pN[i], pP[i]);
pCl_spec = Specular(pLights, pN[i], pP[i], pI[i], 0.01);
fd = (RtFloat)rand()/RAND_MAX;
pCi[i][0] = (*pCs)[0]*(kd*pCl_diff[0]) + ks*pCl_spec[0];
pCi[i][1] = (*pCs)[1]*(kd*pCl_diff[1]) + ks*pCl_spec[1];
pCi[i][2] = (*pCs)[2]*(kd*pCl_diff[2]) + ks*pCl_spec[2];
}
}
void Grid_shadeConstant(grid_t *pObj)
{
UINT32 i;
RtColor *pCs, *pCi;
pCs = (RtColor*)VarGetByName(&pObj->vars, "Cs");
pCi = (RtColor*)VarAlloc(VarListDeclare(&pObj->vars, "Ci", "varying", "color"), pObj->npoints);
for (i=0; i < pObj->npoints; i++)
{
if (!pObj->pPointState[i].visible)
continue;
pCi[i][0] = (*pCs)[0];
pCi[i][1] = (*pCs)[1];
pCi[i][2] = (*pCs)[2];
}
}
+36
View File
@@ -0,0 +1,36 @@
#ifndef GRID_H
#define GRID_H
typedef struct _spoint_state_t
{
UINT32 active;
UINT32 shaded;
UINT32 visible;
UINT32 has_face;
UINT32 has_normal;
} point_state_t;
typedef UINT32 pixelpos_t[2];
typedef struct _sgrid_t
{
UINT32 nu, nv, npoints;
var_list_t vars;
pixelpos_t *pPointPos;
point_state_t *pPointState;
} grid_t;
void Grid_init(void);
void Grid_free(grid_t *pObj);
void Grid_create(grid_t *pObj, UINT32 nu, UINT32 nv);
void Grid_assign_params(grid_t *pObj);
void Grid_assign_derivs(grid_t *pObj);
void Grid_calc_Ng(grid_t *pObj);
void Grid_calc_N(grid_t *pObj);
void Grid_assign_Ng2N(grid_t *pObj);
void Grid_displace(grid_t *pObj);
void Grid_shade(grid_t *pObj, RtMatrix *pXform, linkedlist_t *pLights);
void Grid_shadeConstant(grid_t *pObj);
#endif // GRID
+129
View File
@@ -0,0 +1,129 @@
// ------------------------------------------------------------
// imageio.c
//
// Reading writing tiff images
//
// 12.03.2005, J.Ahrensfeld
// ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "libsys.h"
#include "types.h"
#include "colortypes.h"
#include "imageio.h"
// ------------------------------------------------------------
double smin(double v1, double v2)
{
if (v1 < v2)
return v1;
return v2;
}
// ------------------------------------------------------------
double smax(double v1, double v2)
{
if (v1 > v2)
return v1;
return v2;
}
// ------------------------------------------------------------
void ImageInit(image_t *pObj, int im_width, int im_height, int num_ch)
{
pObj->im_height = im_height;
pObj->im_width = im_width;
pObj->num_ch = num_ch;
pObj->num_pixel = im_height*im_width;
volatile UINT32 *pVGA_ctrl = (UINT32*)SYS_VGA_CTRL;
volatile UINT32 *pVGA_front = (UINT32*)SYS_VGA_FB_FRONT;
volatile UINT32 *pVGA_back = (UINT32*)SYS_VGA_FB_BACK;
pObj->pColor_data = (UINT64*)jmalloc(pObj->num_pixel*sizeof(UINT32));
memset(pObj->pColor_data, 0, pObj->num_pixel*sizeof(UINT32));
printf("pPixelBuf : %8.8X\n", (UINT32)pObj->pColor_data);
*pVGA_front = (UINT32)pObj->pColor_data;
*pVGA_back = (UINT32)pObj->pColor_data;
*pVGA_ctrl |= SYS_VGA_BIT_MSTEN;
}
// ------------------------------------------------------------
void ImageFree(image_t *pObj)
{
pObj->im_height = 0;
pObj->im_width = 0;
pObj->num_pixel = 0;
pObj->num_ch = 0;
if (pObj->pColor_data)
jfree(pObj->pColor_data);
}
// ------------------------------------------------------------
void ImageCopy(image_t *pSrc, image_t *pDst)
{
pDst->im_height = pSrc->im_height;
pDst->im_width = pSrc->im_width;
pDst->num_pixel = pSrc->num_pixel;
pDst->num_ch = pSrc->num_ch;
if (!pDst->pColor_data)
pDst->pColor_data = (double*)jmalloc(pSrc->num_ch*pSrc->num_pixel*sizeof(double));
memcpy(pDst->pColor_data, pSrc->pColor_data, pSrc->num_ch*pSrc->num_pixel*sizeof(double));
}
// ------------------------------------------------------------
int ImageLoadTiff(image_t *pObj, char *filename)
{
return 0;
}
// ------------------------------------------------------------
int ImageSaveTiff(image_t *pObj, char *filename)
{
return 0;
}
void ImageSetPixel(image_t *pObj, UINT32 u, UINT32 v, rgba_t color)
{
UINT32 offset;
UINT32 *pRGB;
UINT32 *pRGBA;
pRGB = (UINT32*)pObj->pColor_data;
pRGBA = (UINT32*)pObj->pColor_data;
if (pObj->num_ch == 4)
{
if ((u < pObj->im_width) && (v < pObj->im_height))
{
offset = (UINT32)(pObj->im_width*v);
pRGBA[u+offset] = ((UINT32)(255*color[0]) & 0xFF) | (((UINT32)(255*color[1]) & 0xFF) << 8) | (((UINT32)(255*color[2]) & 0xFF) << 16);
}
}
else
{
if ((u < pObj->im_width) && (v < pObj->im_height))
{
offset = (UINT32)(pObj->im_width*v);
pRGB[u+offset] = ((UINT32)(255*color[0]) & 0xFF) | (((UINT32)(255*color[1]) & 0xFF) << 8) | (((UINT32)(255*color[2]) & 0xFF) << 16);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
// ------------------------------------------------------------
// imageio.h
//
// Reading writing tiff images
//
// 12.03.2005, J.Ahrensfeld
// ------------------------------------------------------------
#ifndef IMAGEIO_H
#define IMAGEIO_H
#include "libsys.h"
#include "types.h"
#include "colortypes.h"
// ------------------------------------------------------------
typedef struct _image_t
{
UINT32 im_width, im_height, num_pixel, num_ch;
UINT64 *pColor_data;
} image_t;
// ------------------------------------------------------------
void ImageInit(image_t *pObj, int im_width, int im_height, int num_ch);
void ImageFree(image_t *pObj);
void ImageCopy(image_t *pSrc, image_t *pDst);
int ImageLoadTiff(image_t *pObj, char *filename);
int ImageSaveTiff(image_t *pObj, char *filename);
void ImageSetPixel(image_t *pObj, UINT32 u, UINT32 v, rgba_t color);
// ------------------------------------------------------------
#endif // IMAGEIO_H
+238
View File
@@ -0,0 +1,238 @@
#include <stdlib.h>
#include <string.h>
#include "linklist.h"
linkedlist_t* LinkedList_free(linkedlist_t *pObj)
{
linkedlist_t *pCurr;
pCurr = LinkedList_find_first(pObj);
while(pCurr)
pCurr = LinkedList_del(pCurr);
return pCurr;
}
linkedlist_t* LinkedList_append(linkedlist_t *pObj, UINT32 num, UINT32 size)
{
UINT32 i;
linkedlist_t *pCurr, *pLast;
char *pData = NULL;
if (size)
pData = (void*)malloc(num*size);
pLast = pObj;
if (pObj == NULL)
{
pCurr = (linkedlist_t*)malloc(num*sizeof(linkedlist_t));
}
else
{
if (pObj->pNext)
pLast = LinkedList_find_last(pObj);
pCurr = (linkedlist_t*)malloc(num*sizeof(linkedlist_t));
}
memset(pCurr, 0, num*sizeof(linkedlist_t));
pLast = pCurr;
pLast->pPrev = pObj;
if (pObj)
pObj->pNext = pCurr;
pLast->size = size;
if (pData)
pLast->pData = pData;
pData += size;
for(i=1; i < num; i++)
{
pLast->pNext = &pCurr[i];
pLast = pLast->pNext;
pLast->pPrev = &pCurr[i-1];
pLast->size = size;
if (pData)
pLast->pData = pData;
pData += size;
}
return pCurr;
}
/*
linkedlist_t* LinkedList_append(linkedlist_t *pObj, UINT32 id, UINT32 size)
{
linkedlist_t *pCurr;
if (pObj == NULL)
{
pCurr = (linkedlist_t*)malloc(sizeof(linkedlist_t));
}
else
{
if (pObj->pNext)
pObj = LinkedList_find_last(pObj);
pObj->pNext = (linkedlist_t*)malloc(sizeof(linkedlist_t));
pCurr = pObj->pNext;
}
memset(pCurr, 0, sizeof(linkedlist_t));
if (size)
{
pCurr->pData = (void*)malloc(size);
}
pCurr->size = size;
pCurr->id = id;
pCurr->pPrev = pObj;
return pCurr;
}
*/
linkedlist_t* LinkedList_add(linkedlist_t *pObj, UINT32 num, void *pData, UINT32 size)
{
linkedlist_t *pCurr;
pCurr = LinkedList_append(pObj, num, size);
return LinkedList_put(pCurr, pData, size);
}
linkedlist_t* LinkedList_put(linkedlist_t *pObj, void *pData, UINT32 size)
{
linkedlist_t *pCurr;
pCurr = pObj;
if (size <= pCurr->size)
{
if (pData)
memcpy(pCurr->pData, pData, size);
pCurr->size = size;
}
return pCurr;
}
void* LinkedList_get(linkedlist_t *pObj)
{
return pObj->pData;
}
linkedlist_t* LinkedList_del(linkedlist_t *pObj)
{
linkedlist_t *pCurr;
if (pObj == NULL)
return NULL;
if (pObj->pPrev)
pObj->pPrev->pNext = pObj->pNext;
if (pObj->pNext)
pObj->pNext->pPrev = pObj->pPrev;
pCurr = NULL;
if (pObj->pNext)
{
pCurr = pObj->pNext;
}
else
{
pCurr = pObj->pPrev;
}
// if (pObj->pPrev)
// {
// if (pObj->pPrev != pCurr)
// pObj->pPrev->pNext = pCurr;
// else
// pObj->pPrev->pNext = NULL;
// }
if (pObj->pData)
free(pObj->pData);
free(pObj);
return pCurr;
}
linkedlist_t* LinkedList_ins(linkedlist_t *pObj, UINT32 num, void *pData, UINT32 size)
{
linkedlist_t *pCurr, *pNext;
if (pObj == NULL)
return NULL;
pNext = pObj->pNext;
pCurr = LinkedList_add(pObj, num, pData, size);
pCurr->pNext = pObj;
pCurr->pPrev = pObj->pPrev;
pObj->pPrev->pNext = pCurr;
pObj->pPrev = pCurr;
pObj->pNext = pNext;
return pCurr;
}
linkedlist_t* LinkedList_find_first(linkedlist_t *pObj)
{
linkedlist_t *pCurr;
if (pObj == NULL)
return NULL;
pCurr = pObj;
while(pCurr->pPrev)
pCurr = pCurr->pPrev;
return pCurr;
}
linkedlist_t* LinkedList_find_last(linkedlist_t *pObj)
{
linkedlist_t *pCurr;
if (pObj == NULL)
return NULL;
pCurr = pObj;
while(pCurr->pNext)
pCurr = pCurr->pNext;
return pCurr;
}
/*
linkedlist_t* LinkedList_find_by_id(linkedlist_t *pObj, UINT32 id)
{
linkedlist_t *pCurr;
if (pObj == NULL)
return NULL;
pCurr = LinkedList_find_first(pObj);
do
{
if (pCurr->id == id)
break;
pCurr = pCurr->pNext;
} while(pCurr->pNext);
if (pCurr->id == id)
return pCurr;
return NULL;
}
*/
+28
View File
@@ -0,0 +1,28 @@
#ifndef LINKLIST_H
#define LINKLIST_H
#include <string.h>
#include "types.h"
typedef struct _slinkedlist_t
{
UINT32 id;
UINT32 size;
void *pData;
struct _slinkedlist_t *pNext;
struct _slinkedlist_t *pPrev;
} linkedlist_t;
linkedlist_t* LinkedList_free(linkedlist_t *pObj);
linkedlist_t* LinkedList_append(linkedlist_t *pObj, UINT32 num, UINT32 size);
linkedlist_t* LinkedList_add(linkedlist_t *pObj, UINT32 num, void *pData, UINT32 size);
linkedlist_t* LinkedList_put(linkedlist_t *pObj, void *pData, UINT32 size);
linkedlist_t* LinkedList_ins(linkedlist_t *pObj, UINT32 num, void *pData, UINT32 size);
void* LinkedList_get(linkedlist_t *pObj);
linkedlist_t* LinkedList_del(linkedlist_t *pObj);
linkedlist_t* LinkedList_find_first(linkedlist_t *pObj);
linkedlist_t* LinkedList_find_last(linkedlist_t *pObj);
#endif // LINKLIST_H
+188
View File
@@ -0,0 +1,188 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 168 */
/* Listing 8.5 An improved boilerplate viewing program */
#include <stdio.h>
#include "ri.h"
#include "camset.h"
#define NFRAMES 20
#define SHADING_RATE 20
/* viewbasics.c: file compiling view options into a rendering shell. */
#define DATATYPE RI_RGBA /* Pixels have RGB and coverage */
#define PICXRES 800.0 /* Horizontal output resolution */
#define PICYRES 600.0 /* Vertical output resolution */
#define CROPMINX 0.0 /* RiCropWindow() parameters */
#define CROPMAXX 1.0
#define CROPMINY 0.0
#define CROPMAXY 1.0
#define CAMXFROM -0.0 /* Camera position */
#define CAMYFROM 1.0
#define CAMZFROM -4.0
#define CAMXTO 0.0 /* Camera direction */
#define CAMYTO -3.0
#define CAMZTO 10.0
#define CAMROLL 0.0 /* Camera roll */
#define CAMZOOM 1.0 /* Camera zoom rate */
//--------------------------------------------------------------------
#define PATCH 1
#define X0 -1
#define X1 -.33
#define X2 .33
#define X3 1
#define Y0 -.7
#define Y1 -.1
#define Y2 0.1
#define Y3 0.7
#define Z0 -1
#define Z1 -.33
#define Z2 .33
#define Z3 1
PatchExample(Patch)
RtPoint Patch[4][4];
{
RtPoint blpatch[2][2];
RtColor color = {1,1,0};
RtColor CpColors[16] = {{ .8, 0, 0 }, { 0, .8, 0 }, { 0, 0, .8 }, { .8, 0, .8 }, { .8, 0, 0 }, { 0, .8, 0 }, { 0, 0, .8 }, { .8, 0, .8 }, { .8, 0, 0 }, { 0, .8, 0 }, { 0, 0, .8 }, { .8, 0, .8 }, { .8, 0, 0 }, { 0, .8, 0 }, { 0, 0, .8 }, { .8, 0, .8 }};
int u, v;
#define MOVE_PT(d, s) {d[0]=s[0]; d[1]=s[1]; d[2]=s[2];}
#ifdef PATCH
RiColor(color);
// RiBasis(RiPowerBasis, RI_POWERSTEP, RiPowerBasis, RI_POWERSTEP);
// RiBasis(RiHermiteBasis, RI_HERMITESTEP, RiHermiteBasis, RI_HERMITESTEP);
// RiBasis(RiCatmullRomBasis, RI_CATMULLROMSTEP, RiCatmullRomBasis, RI_CATMULLROMSTEP);
// RiBasis(RiBSplineBasis, RI_BSPLINESTEP, RiBSplineBasis, RI_BSPLINESTEP);
RiPatch(RI_BICUBIC, RI_P, (RtPointer) Patch, "Cs", (RtPointer)CpColors, RI_NULL);
#endif
#ifdef HULL
for (v = 0; v < 3; v++) {
for (u = 0; u < 3; u++) {
MOVE_PT(blpatch[0][0], Patch[v][u])
MOVE_PT(blpatch[0][1], Patch[v][u+1])
MOVE_PT(blpatch[1][0], Patch[v+1][u])
MOVE_PT(blpatch[1][1], Patch[v+1][u+1])
color[0] = 1;
color[1] = 0.5*v;
color[2] = 0.5*u;
RiAttributeBegin();
RiColor(color);
RiPatch(RI_BILINEAR, RI_P, (RtPointer) blpatch, RI_NULL);
RiAttributeEnd();
}
}
#endif
}
Go()
{
static RtPoint Patch[16] = {
{ X0, Y0, Z0}, { X1, Y2, Z0}, { X2, Y1, Z0}, { X3, Y3, Z0},
{ X0, Y1, Z1}, { X1, Y2, Z1}, { X2, Y1, Z1}, { X3, Y2, Z1},
{ X0, Y1, Z2}, { X1, Y2, Z2}, { X2, Y1, Z2}, { X3, Y2, Z2},
{ X0, Y0, Z3}, { X1, Y2, Z3}, { X2, Y1, Z3}, { X3, Y3, Z3}};
PatchExample(Patch);
}
//--------------------------------------------------------------------
RtPoint CameraFrom = { CAMXFROM, CAMYFROM, CAMZFROM },
CameraTo = { CAMXTO, CAMYTO, CAMZTO };
static RtColor Color = { .6, .8, .2 };
RtPoint Square[4] = { {.5,.5,0}, {.5,-.5,0}, {-.5,-.9,0}, {-.1,.5,0} };
RtPoint Hexagon[6] = { {.2,.5,0}, {.5,0,0}, {.2,-.5,0}, {-.2,-.5,0}, {-.5,0,0}, {-.2,.5,0} };
RtColor VertColors[6] = {{ .8, 0, 0 }, { 0, .8, 0 }, { 0, 0, .8 }, { .8, 0, .8 }, { .8, .8, 0 }, { 0, .8, .8 }};
RtPoint hull[8] = { {.5,.5,0}, {.5,-.5,0}, {-.5,-.5,0}, {-.5,.5,0}, {.3,-.3,0}, {-.5,-.5,1}, {-.3,.1,-.5}, {.5,.5,0} };
typedef struct _testi
{
RtInt dummy;
RtPointer p;
} testi;
main()
{
RtInt frame, i, nverts[] = {4, 4};
RtToken my_var;
RtFloat width[4] = {0.005, 0.005, 0.03, 0.04};
RtLightHandle lighthnd;
RiBegin(RI_NULL); /* As always */
my_var = RiDeclare("Jens", "uniform string");
/* Output image characteristics */
RiShadingRate(SHADING_RATE);
RiFormat((RtInt)PICXRES, (RtInt)PICYRES, -1.0); /* Image resolution */
RiTransformBegin();
RiTranslate(10, 10, -5);
lighthnd = RiLightSource( "distantlight", RI_NULL);
RiTransformEnd();
RiIlluminate(lighthnd, RI_TRUE);
/* Region of image rendered */
RiCropWindow(CROPMINX, CROPMAXX, CROPMINY, CROPMAXY);
FrameCamera2(PICXRES*CAMZOOM, PICXRES, PICYRES);
RiClipping(1E-3, RI_INFINITY); /* Clipping planes*/
/* Now describe the world */
for (frame=0; frame < NFRAMES; frame++)
{
RiFrameBegin(frame);
RiDisplay(RI_NULL, RI_FRAMEBUFFER, DATATYPE, RI_NULL);
/* Camera position and orientation */
PlaceCamera(CameraFrom, CameraTo, CAMROLL);
// RiTranslate(1, 2, 3);
CameraFrom[0] += 2.0/NFRAMES;
RiWorldBegin();
RiBasis(RiBezierBasis, RI_BEZIERSTEP, RiBezierBasis, RI_BEZIERSTEP);
// RiBasis(RiBSplineBasis, RI_BSPLINESTEP, RiBSplineBasis, RI_BSPLINESTEP);
/* ...Your scene here... */
RiSurface("constant", RI_NULL);
RiRotate((360.0*frame)/NFRAMES, 0, 1, 0);
// RiCurves(RI_CUBIC, 1, nverts, RI_NONPERIODIC, RI_P, (RtPointer)hull, RI_WIDTH, (RtPointer)&width, RI_NULL);
RiAttributeBegin();
RiSides(2);
// RiTranslate(1, -0.5, 0);
Go();
RiAttributeEnd();
RiTransformBegin();
RiTransformBegin();
RiTranslate(0, -9.5, 0);
RiTransformEnd();
RiTranslate(0, -0.5, 0);
RiSurface("constant", RI_NULL);
RiPolygon(6, "P", (RtPointer)Hexagon, "Cs", (RtPointer)VertColors, RI_NULL);
RiTransformEnd();
RiTransformBegin();
RiTranslate(0, 0.5, 0);
// RiRotate(90, 1, 0, 0);
// RiRotate(45, 0, 1, 0);
RiColor(Color); /* Declare the color */
RiSides(2);
RiSphere(0.5, -0.5, 0.5, 360, RI_NULL);
RiTranslate(0, 0.5, 0);
RiTransformEnd();
RiWorldEnd();
RiFrameEnd();
}
RiEnd();
return 0;
}
+131
View File
@@ -0,0 +1,131 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 168 */
/* Listing 8.5 An improved boilerplate viewing program */
#include <stdio.h>
#include "ri.h"
#include "camset.h"
#include "patches.h"
#define NFRAMES 25
/* viewbasics.c: file compiling view options into a rendering shell. */
#define DATATYPE RI_RGBA /* Pixels have RGB and coverage */
#define SHADINGRATE 20.0
#define PICXRES 800.0 /* Horizontal output resolution */
#define PICYRES 600.0 /* Vertical output resolution */
#define CROPMINX 0.0 /* RiCropWindow() parameters */
#define CROPMAXX 1.0
#define CROPMINY 0.0
#define CROPMAXY 1.0
#define CAMXFROM 0.0 /* Camera position */
#define CAMYFROM 1.0
#define CAMZFROM -4.0
#define CAMXTO 0.0 /* Camera direction */
#define CAMYTO -2.0
#define CAMZTO 10.0
#define CAMROLL 0.0 /* Camera roll */
#define CAMZOOM 1.0 /* Camera zoom rate */
//--------------------------------------------------------------------
#define NPOINTS 10
Point2D points[NPOINTS] = {
{.0000,1.5000},
{.0703,1.5000},
{.1273,1.4293},
{.1273,1.3727},
{.1273,1.2300},
{.0899,1.1600},
{.0899,1.0000},
{.0899,0.7500},
{.4100,0.6780},
{.1250,0.0000},
};
RtColor color = {.9,.9,.5};
Go() {
RiColor(color);
RiRotate(-90.0, 1.0, 0.0, 0.0);
SurfOR(points, NPOINTS);
}
//--------------------------------------------------------------------
RtPoint CameraFrom = { CAMXFROM, CAMYFROM, CAMZFROM },
CameraTo = { CAMXTO, CAMYTO, CAMZTO };
static RtColor Color = { .6, .8, .2 };
RtPoint Square[4] = { {.5,.5,0}, {.5,-.5,0}, {-.5,-.5,0}, {-.5,.5,0} };
typedef struct _testi
{
RtInt dummy;
RtPointer p;
} testi;
main()
{
RtInt frame, i;
char filename[256];
RtToken my_var;
RtLightHandle lighthnd;
RiBegin(RI_NULL); /* As always */
my_var = RiDeclare("Jens", "uniform string");
/* Output image characteristics */
RiShadingRate(SHADINGRATE);
RiFormat((RtInt)PICXRES, (RtInt)PICYRES, -1.0); /* Image resolution */
RiTransformBegin();
lighthnd = RiLightSource( "distantlight", RI_NULL);
RiTransformEnd();
RiIlluminate(lighthnd, RI_TRUE);
/* Region of image rendered */
RiCropWindow(CROPMINX, CROPMAXX, CROPMINY, CROPMAXY);
FrameCamera2(PICXRES*CAMZOOM, PICXRES, PICYRES);
RiClipping(1E-3, RI_INFINITY); /* Clipping planes*/
/* Now describe the world */
for (frame=0; frame < NFRAMES; frame++)
{
RiFrameBegin(frame);
sprintf(filename, "R:\\ri%.3d.tif", frame);
RiDisplay(RI_NULL, RI_FRAMEBUFFER, DATATYPE, RI_NULL);
// Camera position and orientation
PlaceCamera(CameraFrom, CameraTo, CAMROLL);
// CameraFrom[0] += 2.0/NFRAMES;
RiWorldBegin();
// ...Your scene here...
RiSurface("matte", RI_NULL);
RiRotate((360.0*frame)/NFRAMES, 0, 1, 0);
Go();
// jstats();
RiColor(Color); // Declare the color
RiTransformBegin();
RiTranslate(0, 0.5, 0);
// RiRotate(90, 1, 0, 0);
// RiRotate(45, 0, 1, 0);
RiDisplacement("dented", RI_NULL);
RiSphere(0.5, -0.5, 0.5, 360, RI_NULL);
RiTransformEnd();
// RiPolygon(4, RI_P, (RtPointer)Square, RI_NULL);
RiWorldEnd();
RiFrameEnd();
}
RiEnd();
jstats();
return 0;
}
+160
View File
@@ -0,0 +1,160 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 168 */
/* Listing 8.5 An improved boilerplate viewing program */
#include <stdio.h>
#include "ri.h"
#include "camset.h"
#include "polys.h"
/* viewbasics.c: file compiling view options into a rendering shell. */
#define DATATYPE RI_RGBA /* Pixels have RGB and coverage */
#define SHADINGRATE 20.0
#define PICXRES 800.0 /* Horizontal output resolution */
#define PICYRES 600.0 /* Vertical output resolution */
#define CROPMINX 0.0 /* RiCropWindow() parameters */
#define CROPMAXX 1.0
#define CROPMINY 0.0
#define CROPMAXY 1.0
#define CAMXFROM 0.0 /* Camera position */
#define CAMYFROM 0.0
#define CAMZFROM -2.0
#define CAMXTO 0.5 /* Camera direction */
#define CAMYTO 0.0
#define CAMZTO 1.0
#define CAMROLL 0.0 /* Camera roll */
#define CAMZOOM 0.5 /* Camera zoom rate */
//--------------------------------------------------------------------
RtPoint CameraFrom = { CAMXFROM, CAMYFROM, CAMZFROM },
CameraTo = { CAMXTO, CAMYTO, CAMZTO };
RtPoint Square[4] = { {.5,.5,0}, {.5,-.5,0}, {-.5,-.5,0}, {-.5,.5,0} };
static RtColor Color = { .6, .8, .2 };
#define L -.5 /* For x: left side */
#define R .5 /* For x: right side */
#define D -.5 /* For y: down side */
#define U .5 /* For y: upper side */
#define F .5 /* For z: far side */
#define N -.5 /* For z: near side */
/* UnitCube(): define a cube in the graphics environment */
void UnitCube()
{
static RtPoint Cube[6][4] = {
{ {L,D,F}, {L,D,N}, {R,D,N}, {R,D,F} }, /* Bottom face */
{ {L,D,F}, {L,U,F}, {L,U,N}, {L,D,N} }, /* Left face */
{ {R,U,N}, {L,U,N}, {L,U,F}, {R,U,F} }, /* Top face */
{ {R,U,N}, {R,U,F}, {R,D,F}, {R,D,N} }, /* Right face */
{ {R,D,F}, {R,U,F}, {L,U,F}, {L,D,F} }, /* Far face */
{ {L,U,N}, {R,U,N}, {R,D,N}, {L,D,N} } /* Near face */
};
int i;
for( i = 0; i < 6; i++) /* Declare the cube */
RiPolygon( (RtInt) 4, RI_P, (RtPointer) Cube[i], RI_NULL);
}
//--------------------------------------------------------------------
#define NPOINTS 16
Point2D points[NPOINTS] = {
{1.5000,.0000},
{1.4600,.0900},
{1.3500,.1273},
{1.2625,.1203},
{1.1750,.1047},
{1.0875,.0935},
{1.0000,.0899},
{0.9375,.0982},
{0.8625,.1236},
{0.7250,.1851},
{0.5875,.2281},
{0.4500,.2383},
{0.3375,.2255},
{0.2250,.1953},
{0.0750,.1414},
{0.0000,.1125}
};
RtColor colors[NPOINTS] = {
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5},
{.9,.9,.5}
};
RtColor color = {.9,.9,.5};
void Go(void)
{
RiColor(color);
/*RiTranslate(0.0,-0.5,0.0);*/
RiRotate(-90.0, 1.0, 0.0, 0.0);
PolySurfOR(points, colors, NPOINTS);
}
main()
{
RiBegin(RI_NULL); /* As always */
/* Output image characteristics */
RiDisplay(RI_NULL, RI_FRAMEBUFFER, DATATYPE, RI_NULL);
RiShadingRate(SHADINGRATE);
RiFormat((RtInt)PICXRES, (RtInt)PICYRES, -1.0); /* Image resolution */
RiTransformBegin();
RiRotate(30, 1.0, 0.0, 0.0);
RiLightSource( "distantlight", RI_NULL);
RiTransformEnd();
/* Region of image rendered */
RiCropWindow(CROPMINX, CROPMAXX, CROPMINY, CROPMAXY);
FrameCamera2(PICXRES*CAMZOOM, PICXRES, PICYRES);
// RiFrameAspectRatio(1.83);
/* Camera position and orientation */
CameraTo[0] -= CameraFrom[0];
CameraTo[1] -= CameraFrom[1];
CameraTo[2] -= CameraFrom[2];
PlaceCamera(CameraFrom, CameraTo, CAMROLL);
RiClipping(1E-3, RI_INFINITY); /* Clipping planes*/
// RiRotate(-40, 3.0, 2.0, 1.0);
/* Now describe the world */
RiWorldBegin();
/* ...Your scene here... */
RiSurface("matte", RI_NULL);
RiColor(Color); /* Declare the color */
// RiRotate(10, 0.0, 1.0, 0.0);
RiTranslate(0, -1.0, 0.0);
// RiScale(2.0, 1.0, 1.0);
// RiPatch(RI_BICUBIC, RI_P, (RtPointer)Square, RI_NULL);
// RiPatch(RI_BILINEAR, RI_P, (RtPointer)Square, RI_NULL);
UnitCube();
// RiPointsPolygons(1, (RtInt*)&nverts, verts, RI_P, (RtPointer)Square, RI_NULL);
RiTranslate(0, 0.5, 0.0);
Go();
RiWorldEnd();
RiEnd();
return 0;
}
+415
View File
@@ -0,0 +1,415 @@
//
// Autor: Matthias Huether (huether@dfki.uni-sb.de)
//
// Beschreibung: Funktionen fuer Matrizenrechnung
//
//
// M sei eine Matrix, wie sie in den Animationsskripten vorkommt.
//
// M = TR => M^{-1} = (TR)^{-1} = R^{-1}T^{-1},
// wobei T: Translationsmatrix, R: Rotationsmatrix
// M hat folgende Gestalt:
// ( 1 0 0 x )
// M = ( 0 a -1 y )
// ( 0 1 b z )
// ( 0 0 0 1 )
// Die Translation steht in der 4. SPALTE der Matrix M, der Rest ist Rotation.
// Die Rotationsmatrix erhaelt man, indem man die Translation aus M
// ausschneidet, d.h. M[1][4]=M[2][4]=M[3][4]=0:
// ( 1 0 0 0 )
// R = ( 0 a -1 0 )
// ( 0 1 b 0 )
// ( 0 0 0 1 )
// Die Translationsmatrix erhaelt man, indem man in M die Rotation durch die
// Einheismatrix ersetzt:
// ( 1 0 0 x )
// T = ( 0 1 0 y )
// ( 0 0 1 z )
// ( 0 0 0 1 )
// Die inverse Rotationsmatrix Ri ergibt sich aus der Rotationsmatrix R durch
// Transponieren von R:
// ( 1 0 0 0 )
// Ri = ( 0 a 1 0 )
// ( 0 -1 b 0 )
// ( 0 0 0 1 )
// Die inverse Translationsmatrix Ti ist die negative Translation:
// ( 1 0 0 -x )
// Ti = ( 0 1 0 -y )
// ( 0 0 1 -z )
// ( 0 0 0 1 )
#include <stdio.h>
#include <math.h>
#include "ri.h"
#include "types.h"
#define min 1e-06 // Zahl<min -> Zahl=0
#define pi 3.14159265359
// Ausgabe einer nxn-Matrix
void PrintMatrix (RtMatrix M, UINT32 d)
{
UINT32 i,j;
printf("\n");
for (i=0;i<d;++i)
{
printf("\t( ");
for (j=0;j<d-1;++j)
printf("%9.6g\t",fabs(M[i][j])<min?0:M[i][j]);
printf("%9.6g )\n",fabs(M[i][d-1])<min?0:M[i][d-1]);
}
printf("\n");
}
// Ausgabe eines Vektors in Zeilenform
void PrintVector (RtFloat *u, UINT32 d)
{
UINT32 i;
printf("\n\t ( ");
for (i=0;i<d-1;++i)
printf("%g, ",fabs(u[i])<min?0:u[i]);
printf("%g )\n",fabs(u[d-1])<min?0:u[d-1]);
}
// Berechnung des Skalarprodukts <.,.> zwischen zwei Vektoren u, v;
// <u,v> = u[1]*v[1] + u[2]*v[2] + ... + u[n]*v[n]
RtFloat ScalarProduct (RtFloat *u, RtFloat *v, UINT32 d)
{
UINT32 i;
RtFloat s=0.0;
for (i=0;i<d;++i)
s += u[i]*v[i];
return s;
}
// Berechnung des Betrags eines Vektors
RtFloat Norm (RtFloat *u, UINT32 d)
{
RtFloat m;
m = (RtFloat)sqrt(ScalarProduct(u,u,d));
return m;
}
// Berechnung des Winkels zwischen zwei Vektoren u, v.
// Verfahren: cos(u,v) = <u,v>/(|u|*|v|), wobei <.,.> das Skalarprodukt
// zwischen zwei Vektoren und |.| der Betrag eines Vektors sind.
RtFloat AngleBetweenVectors (RtFloat *u, RtFloat *v, UINT32 d)
{
RtFloat p;
p = (RtFloat)acos(ScalarProduct(u,v,d)/(Norm(u,d)*Norm(v,d)));
printf("Winkel = %g\n",p*180.0/3.1415925359);
return p;
}
// Liefert die Einheitsmatrix in Id zurueck.
void IdentityMatrix (RtMatrix *Id)
{
UINT32 i,j;
for (i=0;i<4;++i)
for (j=0;j<4;++j)
(*Id)[i][j] = (RtFloat)(i==j);
}
// Multiplizieren zweier 4x4-Matrizen M, N mit dem Schulverfahren.
// Das Ergebnis der Multiplikation von M und N steht in der Matrix MN.
void MultiplyMatrices (RtMatrix M, RtMatrix N, RtMatrix *MN)
{
INT32 i,j;
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
(*MN)[i][j] = 0;
(*MN)[i][j] += M[i][0]*N[0][j];
(*MN)[i][j] += M[i][1]*N[1][j];
(*MN)[i][j] += M[i][2]*N[2][j];
(*MN)[i][j] += M[i][3]*N[3][j];
}
}
}
// Multiplikation einer 4x4-Matrix M und eines Spaltenvektors u
void MultiplyMatrixVector (RtMatrix M, RtFloat *u, RtFloat *Mu)
{
INT32 i;
for (i=0; i<4; i++)
{
Mu[i] = 0;
Mu[i] += M[i][0]*u[0];
Mu[i] += M[i][1]*u[1];
Mu[i] += M[i][2]*u[2];
Mu[i] += M[i][3]*u[3];
}
}
void MultiplyMatrixVector2 (RtMatrix M, RtFloat *u, RtFloat *Mu)
{
INT32 i;
RtFloat res[3];
for (i=0; i < 3; i++)
{
res[i] = M[i][3];
res[i] += M[i][2]*u[2];
res[i] += M[i][1]*u[1];
res[i] += M[i][0]*u[0];
}
memcpy(Mu, res, 3*sizeof(RtFloat));
}
// Multiplikation eines Zeilenvektors u und einer 4x4-Matrix M
void MultiplyVectorMatrix (RtFloat *u, RtMatrix M, RtFloat *uM)
{
INT32 i;
for (i=0;i<4;++i)
{
uM[i] = 0;
uM[i] += u[0]*M[0][i];
uM[i] += u[1]*M[1][i];
uM[i] += u[2]*M[2][i];
uM[i] += u[3]*M[3][i];
}
}
// Transponieren einer 4x4-Matrix M; Mt: transponierte Matrix M
void TransposeMatrix (RtMatrix M, RtMatrix *Mt)
{
INT32 i;
for (i=0; i<4; i++)
{
(*Mt)[i][0] = M[0][i];
(*Mt)[i][1] = M[1][i];
(*Mt)[i][2] = M[2][i];
(*Mt)[i][3] = M[3][i];
}
}
// erzeugt neue Translationsmatrix
// Argumente: Zeiger auf 3 Floatwerte der Translation,
// Zeiger auf RtMatrix fuer die Rueckgabe
// Ergebnis: Translationsmatrix T
void NewTranslationMatrix (RtFloat *tval, RtMatrix *T)
{
IdentityMatrix(T);
(*T)[0][3] = tval[0];
(*T)[1][3] = tval[1];
(*T)[2][3] = tval[2];
}
// Berechnung der Rotationsmatrix R aus M (Verfahren s. oben)
void RotationMatrix (RtMatrix M, RtMatrix *R)
{
UINT32 i;
for (i=0; i<4; i++)
{
(*R)[i][0] = M[i][0];
(*R)[i][1] = M[i][1];
(*R)[i][2] = M[i][2];
(*R)[i][3] = 0;
}
(*R)[3][3] = M[3][3];
}
// Berechnung der Translationsmatrix T aus M (Verfahren s. oben)
void TranslationMatrix (RtMatrix M, RtMatrix *T)
{
RtFloat tval[3]; // Werte fuer die Translation
tval[0] = M[0][3];
tval[1] = M[1][3];
tval[2] = M[2][3];
NewTranslationMatrix (tval,T);
}
// Berechnung der inversen Rotationsmatrix Ri aus der Rotationsmatrix R
// (Verfahren s. oben)
void InverseRotationMatrix (RtMatrix R, RtMatrix *Ri)
{
//printf("R = "); PrintMatrix(R);
TransposeMatrix (R,Ri);
}
// Berechnung der inversen Translationsmatrix Ti aus der Translationsmatrix T
// (Verfahren s. oben)
void InverseTranslationMatrix (RtMatrix T, RtMatrix *Ti)
{
UINT32 i;
//printf("T = "); PrintMatrix(T);
IdentityMatrix (Ti);
for (i=0;i<3;++i)
(*Ti)[i][3] = -T[i][3];
}
// Berechnung der inversen Matrix Mi zu M (Verfahren s. oben)
void InverseMatrix (RtMatrix M, RtMatrix *Mi)
{
RtMatrix R,Ri,T,Ti;
RotationMatrix (M,&R);
//printf ("# R = "); PrintMatrix(Ri);
InverseRotationMatrix (R,&Ri);
//printf("Ri = "); PrintMatrix(Ri);
TranslationMatrix (M,&T);
InverseTranslationMatrix (T,&Ti);
//printf("Ti = "); PrintMatrix(Ti);
MultiplyMatrices (Ri,Ti,Mi);
}
// erzeugt Transformationsmatrix anhand einer Rotationsachse und einem
// Rotationswinkel
// Argumente: zwei Punkte, die die Rotationsachse bestimmen (startpoint,
// endpoint)
// Rotationswinkel im Bogenmass (angle)
// Zeiger auf RtMatrix fuer Rueckgabewert
// Ergebnis: Transformationsmatrix M
// Verfahren aus: Steven Harrington; Computer Graphics, A Programming
// Approach; Second Edition, 1987; McGraw-Hill Book Company; Seite 256ff
void BuildTransformationMatrix ( RtFloat *startpoint, RtFloat *endpoint, RtFloat angle, RtMatrix *M)
{
// T: Translationsmatrix, Rx,Ry,Rz : Rotation um die x-,y-,z-Achse,
// H: Hilfsmatrix
// a,b,c: bestimmen Richtung der Rotationsachse
RtMatrix T,Ti,Rx,Rxi,Ry,Ryi,Rz,Rzi,H;
RtFloat a,b,c,v,l;
NewTranslationMatrix (startpoint,&T);
InverseTranslationMatrix (T,&Ti);
//angle = -angle;
a = endpoint[0]-startpoint[0];
b = endpoint[1]-startpoint[1];
c = endpoint[2]-startpoint[2];
v = sqrt(b*b+c*c);
l = sqrt(a*a+b*b+c*c);
if (c<0)
{
a = -a;
b = -b;
c = -c;
angle = -angle;
}
IdentityMatrix(&Rx);
Rx[1][1] = Rx[2][2] = (v!=0 ? c/v : 1); // cos
Rx[1][2] = (v!=0 ? b/v : 0); // sin
Rx[2][1] = -Rx[1][2];
TransposeMatrix (Rx,&Rxi);
IdentityMatrix(&Ry);
Ry[0][0] = Ry[2][2] = (l!=0 ? v/l : 1); // cos
Ry[0][2] = (l!=0 ? a/l : 0); // sin
Ry[2][0] = -Ry[0][2];
TransposeMatrix(Ry,&Ryi);
IdentityMatrix(&Rz);
Rz[0][0] = Rz[1][1] = cos(angle);
Rz[0][1] = -sin(angle);
Rz[1][0] = -Rz[0][1];
MultiplyMatrices (T,Rx,&H);
MultiplyMatrices (H,Ry,M);
MultiplyMatrices (*M,Rz,&H);
MultiplyMatrices (H,Ryi,M);
MultiplyMatrices (*M,Rxi,&H);
MultiplyMatrices (H,Ti,M);
}
void AimCamera(RtFloat *startpoint, RtFloat *endpoint, RtFloat roll, RtMatrix *M)
{
// T: Translationsmatrix, Rx,Ry,Rz : Rotation um die x-,y-,z-Achse,
// H: Hilfsmatrix
// a,b,c: bestimmen Richtung der Rotationsachse
RtMatrix T,Ti,Tti,R,Rx,Ry,Rz,Rzi,Rr,H;
RtFloat a,b,c,v,l,u,alpha,beta,angle;
NewTranslationMatrix (startpoint,&T);
InverseTranslationMatrix (T,&Ti);
TransposeMatrix(Ti,&Tti);
angle = roll*pi/180.0;
a = endpoint[0]-startpoint[0];
b = endpoint[1]-startpoint[1];
c = endpoint[2]-startpoint[2];
v = sqrt(b*b+c*c);
l = sqrt(a*a+b*b+c*c);
u = sqrt(a*a+c*c);
IdentityMatrix(&Rx);
if (b!=0)
{
Rx[1][1] = Rx[2][2] = (l!=0 ? u/l : 1); // cos v
Rx[1][2] = (l!=0 ? b/l : 0); // sin a
Rx[2][1] = -Rx[1][2];
}
IdentityMatrix(&Ry);
Ry[0][0] = Ry[2][2] = (u!=0 ? c/u : 1); // cos
Ry[0][2] = (u!=0 ? a/u : 0); // sin
Ry[2][0] = -Ry[0][2];
IdentityMatrix(&Rz);
Rz[0][0] = Rz[1][1] = cos(angle);
Rz[0][1] = -sin(angle);
Rz[1][0] = -Rz[0][1];
TransposeMatrix(Rz,&Rzi);
MultiplyMatrices (Ry,Rx,&R);
// BuildTransformationMatrix(startpoint,endpoint,angle,&H);
// RotationMatrix (H, &Rr,4);
MultiplyMatrices(R,Rzi,&H);
MultiplyMatrices (Tti,H,M);
}
void PointCopy(RtFloat *pDst, RtFloat *pSrc)
{
pDst[0] = pSrc[0];
pDst[1] = pSrc[1];
pDst[2] = pSrc[2];
}
void PointSwapXY(RtFloat *pSrcDst)
{
RtFloat tmp;
tmp = pSrcDst[0];
pSrcDst[0] = pSrcDst[1];
pSrcDst[1] = tmp;
}
void PointSwapXZ(RtFloat *pSrcDst)
{
RtFloat tmp;
tmp = pSrcDst[0];
pSrcDst[0] = pSrcDst[2];
pSrcDst[2] = tmp;
}
void PointSwapYZ(RtFloat *pSrcDst)
{
RtFloat tmp;
tmp = pSrcDst[1];
pSrcDst[1] = pSrcDst[2];
pSrcDst[2] = tmp;
}
+43
View File
@@ -0,0 +1,43 @@
//
// Autor: Matthias Huether (huether@dfki.uni-sb.de)
//
//
// Beschreibung: Funktionen fuer das Rechnen mit Transformationsmatrizen
//
// ergaenzendes Modul: matrix.cc
// letzte Änderung 01.09.199, Jens Ahrensfeld
#ifndef MATRIX_H
#define MATRIX_H
#include "ri.h"
extern void PrintMatrix(RtMatrix,short);
extern void PrintVector(RtFloat*,short);
extern RtFloat ScalarProduct(RtFloat*,RtFloat*,short);
extern RtFloat Norm(RtFloat*,short);
extern RtFloat AngleBetweenVectors(RtFloat*,RtFloat*,short);
extern void IdentityMatrix(RtMatrix*);
extern void MultiplyMatrices(RtMatrix,RtMatrix,RtMatrix*);
extern void MultiplyMatrixVector(RtMatrix,RtFloat*,RtFloat*);
extern void MultiplyMatrixVector2(RtMatrix M, RtFloat *u, RtFloat *Mu);
extern void MultiplyVectorMatrix(RtFloat*,RtMatrix,RtFloat*);
extern void TransposeMatrix(RtMatrix,RtMatrix*);
extern void NewTranslationMatrix(RtFloat*,RtMatrix*);
extern void RotationMatrix(RtMatrix,RtMatrix*,short);
extern void TranslationMatrix(RtMatrix,RtMatrix*);
extern void InverseRotationMatrix(RtMatrix,RtMatrix*);
extern void InverseTranslationMatrix(RtMatrix,RtMatrix*);
extern void InverseMatrix(RtMatrix,RtMatrix*);
extern void BuildTransformationMatrix(RtFloat*,RtFloat*,RtFloat,RtMatrix*);
// added 01.09.1999
extern void AimCamera(RtFloat*,RtFloat*,RtFloat,RtMatrix*);
void PointCopy(RtFloat *pDst, RtFloat *pSrc);
void PointSwapXY(RtFloat *pSrcDst);
void PointSwapXZ(RtFloat *pSrcDst);
void PointSwapYZ(RtFloat *pSrcDst);
#endif
+714
View File
@@ -0,0 +1,714 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ri.h"
#include "types.h"
#include "graph_state.h"
#include "matrix.h"
#include "object.h"
#include "vars.h"
#include "engine.h"
#include "grid.h"
extern engine_t engine;
// --------------------------------------------------------------
RtToken OBJECT_TYPE_VERTEX = "vertex";
RtToken OBJECT_TYPE_POLYGON = "polygon";
RtToken OBJECT_TYPE_PATCH = "patch";
RtToken OBJECT_TYPE_PATCHMESH = "patchmesh";
RtToken OBJECT_TYPE_LIGHT = "light";
RtToken OBJECT_TYPE_SPHERE = "sphere";
RtToken OBJECT_TYPE_CURVE = "curve";
// --------------------------------------------------------------
void Interpolate_linear_u(RtPoint P0, RtPoint P1, RtPoint* pP, RtFloat u)
{
RtPoint P;
RtFloat u1;
u1 = 1-u;
*pP[0] = u1*P0[0] + u*P1[0];
*pP[1] = u1*P0[1] + u*P1[1];
*pP[2] = u1*P0[2] + u*P1[2];
}
void Interpolate_linear(RtPoint P0, RtPoint P1, RtPoint *pP, UINT32 N)
{
UINT32 i;
RtFloat dx, dy, dz, fx, fy, fz;
fx = P0[0];
fy = P0[1];
fz = P0[2];
dx = (P1[0]-fx)/(N-1);
dy = (P1[1]-fy)/(N-1);
dz = (P1[2]-fz)/(N-1);
for (i=0; i < N; i++)
{
pP[i][0] = i*dx + fx;
pP[i][1] = i*dy + fy;
pP[i][2] = i*dz + fz;
}
}
RtInt Interpolate_cubic(RtPoint *pCP, UINT32 ncp, RtPoint *pIP, UINT32 nip, RtFloat umin, RtFloat umax, RtPointer Basis, RtInt step, RtInt dimcp)
{
UINT32 i, j, jj, k, d, n, np;
RtFloat Bi[3][4], p[4], ut, u, du;
RtFloat *B = (RtFloat*)Basis;
INT32 ii;
np = (ncp-4)/step + 1;
if ((pCP == NULL) || (pIP == NULL) || (Basis == NULL))
return nip*np;
du = (umax-umin)/(nip-1);
n = 0;
for(k=0; k < np; k += step)
{
for(j=0; j < 3; j++)
{
for (ii=0; ii < 4; ii++)
{
Bi[j][ii] = 0;
for (jj=0; jj < 4; jj++)
Bi[j][ii] += B[4*ii+jj]*pCP[k+jj*dimcp][j];
}
}
u = umin;
for(i=0; i < nip; i++)
{
for (j=0; j < 3; j++)
{
ut = u;
pIP[n][j] = Bi[j][3];
for (ii=2; ii >= 0; ii--)
{
pIP[n][j] += ut*Bi[j][ii];
ut *= u;
}
}
u += du;
n++;
}
}
return n;
}
// --------------------------------------------------------------
void Polygon_init(object_t *pObj, UINT32 id, UINT32 nvertices)
{
polygon_t *pObject;
ri_var_t *pVar;
memset(pObj, 0, sizeof(object_t));
pObj->type = OBJECT_TYPE_POLYGON;
pObj->pObjData = jmalloc(sizeof(patchmesh_t));
memset(pObj->pObjData, 0, sizeof(patchmesh_t));
pObject = (polygon_t*)pObj->pObjData;
// --------------------------------------------
pObject->id = id;
pObject->num_verts = nvertices;
VarListInit(&pObj->vars);
}
void Polygon_free(object_t *pObj)
{
if (pObj == NULL)
return;
VarListFree(&pObj->vars);
Object_free(pObj);
}
void Polygon_bound_update(object_t *pObj)
{
}
void Polygon_dice(object_t *pObj, RtInt npu, RtInt npv)
{
}
// --------------------------------------------------------------
void Curve_init(object_t *pObj, UINT32 id, RtToken degree, RtInt nverts, RtToken wrap)
{
curve_t *pObject;
memset(pObj, 0, sizeof(object_t));
pObj->type = OBJECT_TYPE_CURVE;
pObj->pObjData = jmalloc(sizeof(curve_t));
memset(pObj->pObjData, 0, sizeof(curve_t));
pObject = (curve_t*)pObj->pObjData;
// --------------------------------------------
pObject->id = id;
pObject->type = degree;
pObject->wrap = wrap;
pObject->nverts = nverts;
pObject->width[2] = (RtFloat)1.0;
VarListInit(&pObj->vars);
}
void Curve_free(object_t *pObj)
{
if (pObj == NULL)
return;
VarListFree(&pObj->vars);
Object_free(pObj);
}
void Curve_bound_update(object_t *pObj)
{
}
// --------------------------------------------------------------
void Patch_init(object_t *pObj, UINT32 id, RtToken type)
{
patch_t *pObject;
memset(pObj, 0, sizeof(object_t));
pObj->type = OBJECT_TYPE_PATCH;
pObj->pObjData = jmalloc(sizeof(patch_t));
memset(pObj->pObjData, 0, sizeof(patch_t));
pObject = (patch_t*)pObj->pObjData;
// --------------------------------------------
pObject->id = id;
pObject->type = type;
VarListInit(&pObj->vars);
}
void Patch_free(object_t *pObj)
{
patch_t *pObject;
if (pObj == NULL)
return;
VarListFree(&pObj->vars);
Object_free(pObj);
}
void Patch_dice(object_t *pObj, RtInt npu, RtInt npv)
{
RtInt i, j, n, nu, nv, nptu, nptv;
RtFloat u, v, du, dv;
patch_t *pObject;
var_list_t *pGridVars;
ri_var_t *pVar;
RtPoint *pPoints, *pMesh, *pP1, *pP2, *pPV;
RtColor *pColor;
double tictoc;
Tic(&tictoc);
pObj->pGrid = (grid_t*)jmalloc(sizeof(grid_t));
Grid_create(pObj->pGrid, npu, npv);
pGridVars = &pObj->pGrid->vars;
Grid_InitUV(pObj->pGrid);
nu = pObj->pGrid->nu;
nv = pObj->pGrid->nv;
pObject = (patch_t*)pObj->pObjData;
// Declare variable "P"
pVar = VarListDeclare(pGridVars, "P", "varying", "point");
pMesh = (RtPoint*)VarAlloc(pVar, nu*nv);
// Declare variable "Cs"
pVar = VarListDeclare(pGridVars, "Cs", "uniform", "color");
pColor = (RtColor*)VarAlloc(pVar, 1);
memcpy(pColor, pObj->pAttr->cs, sizeof(RtColor));
pPoints = (RtPoint*)Object_get_var(pObj, "P");
if (pObject->type == RI_BILINEAR)
{
pP1 = (RtPoint*)jmalloc(nu*sizeof(RtPoint));
pP2 = (RtPoint*)jmalloc(nu*sizeof(RtPoint));
// Interpolate 1 in X-direction
Interpolate_linear(pPoints[0], pPoints[1], pP1, nu);
// Interpolate 2 in X-direction
Interpolate_linear(pPoints[2], pPoints[3], pP2, nu);
// Interpolate in Z-direction
for (i=0; i < nv; i++)
{
Interpolate_linear(pP1[i], pP2[i], &pMesh[i*nu], nu);
}
jfree(pP1);
jfree(pP2);
}
else
{
if (pObj->pAttr == NULL)
return;
nptv = Interpolate_cubic(NULL, 4, NULL, nv, 0, 1, pObj->pAttr->vbasis, pObj->pAttr->vstep, 0);
nptu = Interpolate_cubic(NULL, 4, NULL, nu, 0, 1, pObj->pAttr->ubasis, pObj->pAttr->ustep, 0);
pPV = (RtPoint*)jmalloc(4*nptv*sizeof(RtPoint));
// Tensor product
j = 0;
for (i=0; i < 4*nv; i += nv)
{
Interpolate_cubic(&pPoints[j++], 4, &pPV[i], nv, 0, 1, pObj->pAttr->vbasis, pObj->pAttr->vstep, 4);
}
j = 0;
for (i=0; i < nv*nu; i += nu)
{
Interpolate_cubic(&pPV[j++], 4, &pMesh[i], nu, 0, 1, pObj->pAttr->ubasis, pObj->pAttr->ustep, nv);
}
jfree(pPV);
}
pObj->stat.time_dice = Toc(&tictoc);
Grid_assign_derivs(pObj->pGrid);
Grid_calc_normal(pObj->pGrid, "Ng");
}
void Patch_bound_update(object_t *pObj)
{
UINT32 i, j, npoints;
patch_t *pObject;
RtPoint *pPoints;
pObject = (patch_t*)pObj->pObjData;
pPoints = (RtPoint*)Object_get_var(pObj, "P");
pObj->bound[0] = +RI_INFINITY;
pObj->bound[1] = -RI_INFINITY;
pObj->bound[2] = +RI_INFINITY;
pObj->bound[3] = -RI_INFINITY;
pObj->bound[4] = +RI_INFINITY;
pObj->bound[5] = -RI_INFINITY;
npoints = 4;
if (pObject->type == RI_BICUBIC)
npoints = 16;
for(i=0; i < npoints; i++)
{
for (j=0; j < 3; j++)
{
pObj->bound[2*j+0] = MIN(pPoints[i][j], pObj->bound[2*j+0]);
pObj->bound[2*j+1] = MAX(pPoints[i][j], pObj->bound[2*j+1]);
}
}
}
// --------------------------------------------------------------
void Patchmesh_init(object_t *pObj, UINT32 id, RtToken type, RtInt nu, RtToken uwrap, RtInt nv, RtToken vwrap, RtPointer pVerts)
{
patchmesh_t *pObject;
ri_var_t *pVar;
memset(pObj, 0, sizeof(object_t));
pObj->type = OBJECT_TYPE_PATCHMESH;
pObj->pObjData = jmalloc(sizeof(patchmesh_t));
memset(pObj->pObjData, 0, sizeof(patchmesh_t));
pObject = (patchmesh_t*)pObj->pObjData;
// --------------------------------------------
pObject->id = id;
pObject->type = type;
pObject->uwrap = uwrap;
pObject->vwrap = vwrap;
pObject->nu = nu;
pObject->nv = nv;
VarListInit(&pObj->vars);
}
void Patchmesh_free(object_t *pObj)
{
if (pObj == NULL)
return;
VarListFree(&pObj->vars);
Object_free(pObj);
}
void Patchmesh_get_patch(object_t *pObj, object_t *pDst, RtInt ip)
{
UINT32 i, j, k, npu, npv;
RtInt x1, y1;
patch_t *pPatch;
ri_var_t *pVar;
RtPoint *pSrc, *pVerts;
patchmesh_t *pObject = (patchmesh_t*)pObj->pObjData;
Patch_init(pDst, 0, pObject->type);
pVar = VarListAdd(&pDst->vars, V_P);
pPatch = (patch_t*)pDst->pObjData;
pSrc = (RtPoint*)Object_get_var(pObj, "P");
Patchmesh_get_npatches(pObj, &npu, &npv);
if (pPatch->type == RI_BILINEAR)
{
pVerts = (RtPoint*)VarSet(pVar, NULL, 4);
x1 = (ip % npu);
y1 = (ip / npu);
PointCopy((RtFloat*)pVerts[0], (RtFloat*)&pSrc[((x1+0) % pObject->nu) + ((y1+0) % pObject->nv)*pObject->nu]);
PointCopy((RtFloat*)pVerts[1], (RtFloat*)&pSrc[((x1+1) % pObject->nu) + ((y1+0) % pObject->nv)*pObject->nu]);
PointCopy((RtFloat*)pVerts[2], (RtFloat*)&pSrc[((x1+0) % pObject->nu) + ((y1+1) % pObject->nv)*pObject->nu]);
PointCopy((RtFloat*)pVerts[3], (RtFloat*)&pSrc[((x1+1) % pObject->nu) + ((y1+1) % pObject->nv)*pObject->nu]);
}
else
{
pVerts = (RtPoint*)VarSet(pVar, NULL, 16);
x1 = pObj->pAttr->ustep*(ip % npu);
y1 = pObj->pAttr->vstep*(ip / npu);
k = 0;
for(i=0; i < 4; i++)
for(j=0; j < 4; j++)
PointCopy((RtFloat*)pVerts[k++], (RtFloat*)&pSrc[((x1+j) % pObject->nu) + ((y1+i) % pObject->nv)*pObject->nu]);
}
Object_set_attr(pDst, pObj->pAttr);
Object_set_xform(pDst, pObj->pXform);
}
RtInt Patchmesh_get_npatches(object_t *pObj, RtInt *nupatches, RtInt *nvpatches)
{
RtInt n, m;
patchmesh_t *pMesh = (patchmesh_t*)pObj->pObjData;
if (pMesh->type == RI_BICUBIC)
{
if (pMesh->uwrap == RI_PERIODIC)
m = (pMesh->nu)/pObj->pAttr->ustep;
else
m = (pMesh->nu-4)/pObj->pAttr->ustep + 1;
if (pMesh->vwrap == RI_PERIODIC)
n = (pMesh->nv)/pObj->pAttr->vstep;
else
n = (pMesh->nv-4)/pObj->pAttr->vstep + 1;
}
else
{
if (pMesh->uwrap == RI_PERIODIC)
m = pMesh->nu;
else
m = pMesh->nu-1;
if (pMesh->vwrap == RI_PERIODIC)
n = pMesh->nv;
else
n = pMesh->nv-1;
}
if (nupatches)
*nupatches = m;
if (nvpatches)
*nvpatches = n;
if (pMesh->uwrap == RI_NONPERIODIC)
m++;
if (pMesh->vwrap == RI_NONPERIODIC)
n++;
return m*n;
}
void Patchmesh_bound_update(object_t *pObj)
{
UINT32 i, j, m, n, N;
patchmesh_t *pObject;
RtPoint *pPoints;
object_t *pMesh;
object_t patch;
pObject = (patchmesh_t*)pObj->pObjData;
pPoints = (RtPoint*)Object_get_var(pObj, "P");
pObj->bound[0] = +RI_INFINITY;
pObj->bound[1] = -RI_INFINITY;
pObj->bound[2] = +RI_INFINITY;
pObj->bound[3] = -RI_INFINITY;
pObj->bound[4] = +RI_INFINITY;
pObj->bound[5] = -RI_INFINITY;
N = Patchmesh_get_npatches(pObj, NULL, NULL);
if (pObject->type == RI_BILINEAR)
{
for(i=0; i < N; i++)
{
for (j=0; j < 3; j++)
{
pObj->bound[2*j+0] = MIN(pPoints[i][j], pObj->bound[2*j+0]);
pObj->bound[2*j+1] = MAX(pPoints[i][j], pObj->bound[2*j+1]);
}
}
}
else
{
N = Patchmesh_get_npatches(pObj, &m, &n);
for(i=0; i < m*n; i++)
{
Patchmesh_get_patch(pObj, &patch, i);
Patch_bound_update(&patch);
for (j=0; j < 3; j++)
{
pObj->bound[2*j+0] = MIN(patch.bound[2*j+0], pObj->bound[2*j+0]);
pObj->bound[2*j+1] = MAX(patch.bound[2*j+1], pObj->bound[2*j+1]);
}
Patch_free(&patch);
}
}
}
// --------------------------------------------------------------
void Sphere_init(object_t *pObj, UINT32 id, RtFloat radius, RtFloat zmin, RtFloat zmax, RtFloat thetamax)
{
sphere_t *pObject;
memset(pObj, 0, sizeof(object_t));
pObj->type = OBJECT_TYPE_SPHERE;
pObj->pObjData = jmalloc(sizeof(sphere_t));
memset(pObj->pObjData, 0, sizeof(sphere_t));
pObject = (sphere_t*)pObj->pObjData;
// --------------------------------------------
pObject->id = id;
pObject->phi_min = (RtFloat)-PI/2;
if (zmin > -radius)
pObject->phi_min = (RtFloat)asin(zmin/radius);
pObject->phi_max = (RtFloat)PI/2;
if (zmax < radius)
pObject->phi_max = (RtFloat)asin(zmax/radius);
pObject->zmin = zmin;
pObject->zmax = zmax;
pObject->radius = radius;
pObject->theta_max = (RtFloat)(PI*thetamax/180);
pObject->is_full_sweep = (thetamax == 360);
}
void Sphere_bound_update(object_t *pObj)
{
sphere_t *pObject;
pObject = (sphere_t*)pObj->pObjData;
pObj->bound[0] = -pObject->radius;
pObj->bound[1] = pObject->radius;
pObj->bound[2] = -pObject->radius;
pObj->bound[3] = pObject->radius;
pObj->bound[4] = pObject->zmin;
pObj->bound[5] = pObject->zmax;
}
void Sphere_dice(object_t *pObj, RtInt npu, RtInt npv)
{
RtInt i, j, k, nu, nv;
sphere_t *pSphere;
RtPoint *pPoint;
RtColor *pColor;
var_list_t *pGridVars;
ri_var_t *pVar;
RtPoint *pPoints, p;
RtFloat u, v, du, dv, phi, p1[3], cosphi ,sinphi;
RtMatrix rot;
double tictoc;
Tic(&tictoc);
pSphere = (sphere_t*)pObj->pObjData;
pObj->pGrid = (grid_t*)jmalloc(sizeof(grid_t));
Grid_create(pObj->pGrid, npu, npv);
Grid_InitUV(pObj->pGrid);
pGridVars = &pObj->pGrid->vars;
nu = pObj->pGrid->nu;
nv = pObj->pGrid->nv;
du = (RtFloat)(1.0/(nu-1));
dv = (RtFloat)(1.0/(nv-1));
// Declare variable "P"
pVar = VarListDeclare(pGridVars, "P", "varying", "point");
pPoint = (RtPoint*)VarAlloc(pVar, nu*nv);
// Declare variable "Cs"
pVar = VarListDeclare(pGridVars, "Cs", "uniform", "color");
pColor = (RtColor*)VarAlloc(pVar, 1);
memcpy(pColor, pObj->pAttr->cs, sizeof(RtColor));
// Create sphere points in v-direction on XZ-plane
v = 0;
k = 0;
for (j=0; j < nv; j++)
{
IdentityMatrix(&rot);
phi = pSphere->phi_min + v*(pSphere->phi_max - pSphere->phi_min);
p1[0] = (RtFloat)cos(phi)*pSphere->radius;
p1[1] = 0;
p1[2] = (RtFloat)sin(phi)*pSphere->radius;
u = 0;
for (i=0; i < nu; i++)
{
phi = u*pSphere->theta_max;
cosphi = (RtFloat)cos(phi);
sinphi = (RtFloat)sin(phi);
rot[0][0] = cosphi;
rot[0][1] = cosphi;
rot[1][0] = sinphi;
rot[0][1] = -sinphi;
MultiplyMatrixVector2(rot, p1, pPoint[k]);
u += du;
k++;
}
v += dv;
}
pObj->stat.time_dice = Toc(&tictoc);
Grid_assign_derivs(pObj->pGrid);
Grid_calc_normal(pObj->pGrid, "Ng");
}
void Sphere_free(object_t *pObj)
{
if (pObj == NULL)
return;
VarListFree(&pObj->vars);
Object_free(pObj);
}
// --------------------------------------------------------------
void Light_init(object_t *pObj, UINT32 id)
{
light_t *pObject;
memset(pObj, 0, sizeof(object_t));
pObj->type = OBJECT_TYPE_LIGHT;
pObj->pObjData = jmalloc(sizeof(light_t));
memset(pObj->pObjData, 0, sizeof(light_t));
pObject = (light_t*)pObj->pObjData;
// --------------------------------------------
pObject->id = id;
pObject->onoff = RI_TRUE;
}
void Light_free(object_t *pObj)
{
if (pObj == NULL)
return;
VarListFree(&pObj->vars);
Object_free(pObj);
}
// --------------------------------------------------------------
void Object_set_xform(object_t *pObj, RtMatrix *pXform)
{
pObj->pXform = pXform;
}
void Object_set_attr(object_t *pObj, attr_t *pAttr)
{
pObj->pAttr = pAttr;
}
RtPointer Object_get_var(object_t *pObj, RtToken name)
{
return (RtPointer)VarGet(VarListFindByName(&pObj->vars, name));
}
void Object_init(object_t *pObj)
{
memset(pObj, 0, sizeof(object_t));
}
void Object_free(object_t *pObj)
{
if (pObj == NULL)
return;
if (pObj->pObjData)
jfree(pObj->pObjData);
if (pObj->pGrid)
Grid_free(pObj->pGrid);
pObj->pObjData = NULL;
}
// --------------------------------------------------------------
void Scene_Add(scene_t *pObj, object_t *pObject)
{
pObject->id = ++pObj->num_objs;
pObject->main_context = Engine_context_get(&engine, ENGINE_CONTEXT_LEVEL_MAIN);
pObj->pObjs = LinkedList_add(pObj->pObjs, 1, (void*)pObject, sizeof(object_t));
}
void Scene_Del(scene_t *pObj, object_t *pObject)
{
}
// --------------------------------------------------------------
+153
View File
@@ -0,0 +1,153 @@
#ifndef OBJECT_H
#define OBJECT_H
// --------------------------------------------------------------
#include "types.h"
#include "linklist.h"
#include "vars.h"
#include "grid.h"
// --------------------------------------------------------------
extern RtToken OBJECT_TYPE_VERTEX;
extern RtToken OBJECT_TYPE_POLYGON;
extern RtToken OBJECT_TYPE_PATCH;
extern RtToken OBJECT_TYPE_PATCHMESH;
extern RtToken OBJECT_TYPE_LIGHT;
extern RtToken OBJECT_TYPE_SPHERE;
extern RtToken OBJECT_TYPE_CURVE;
// --------------------------------------------------------------
typedef struct _sstat_t
{
UINT32 num_polygons;
UINT32 num_patches;
UINT32 num_patchmeshes;
UINT32 num_lights;
UINT32 num_spheres;
UINT32 num_curves;
} stat_t;
typedef struct _sscene_t
{
stat_t stat;
UINT32 num_objs;
linkedlist_t *pObjs;
} scene_t;
typedef struct _spolygon_t
{
UINT32 id;
UINT32 num_verts;
} polygon_t;
typedef struct _scurve_t
{
UINT32 id;
RtToken type;
RtToken wrap;
UINT32 nverts;
RtFloat width[2];
RtPointer pVerts;
} curve_t;
typedef struct _spatch_t
{
UINT32 id;
RtToken type;
} patch_t;
typedef struct _spatchmesh_t
{
UINT32 id;
RtInt nu, nv;
RtToken type;
RtToken uwrap, vwrap;
} patchmesh_t;
typedef struct _slight_t
{
UINT32 id;
RtBoolean onoff;
} light_t;
typedef struct _ssphere_t
{
UINT32 id, is_full_sweep;
RtFloat radius, zmin, zmax;
RtFloat phi_min, phi_max, theta_max;
} sphere_t;
typedef struct _sobj_stat_t
{
double time_dice;
double time_transform;
double time_displace;
double time_shade;
double time_render;
} obj_stat_t;
typedef struct _sobject_t
{
UINT32 id;
UINT32 main_context;
RtToken type;
void *pObjData;
RtMatrix *pXform;
attr_t *pAttr;
RtBound bound;
var_list_t vars;
grid_t *pGrid;
obj_stat_t stat;
} object_t;
void Polygon_init(object_t *pObj, UINT32 id, UINT32 nvertices);
void Polygon_free(object_t *pObj);
void Polygon_dice(object_t *pObj, RtInt npu, RtInt npv);
void Polygon_bound_update(object_t *pObj);
void Curve_init(object_t *pObj, UINT32 id, RtToken degree, RtInt nverts, RtToken wrap);
void Curve_free(object_t *pObj);
void Curve_bound_update(object_t *pObj);
void Patch_init(object_t *pObj, UINT32 id, RtToken type);
void Patch_free(object_t *pObj);
void Patch_dice(object_t *pObj, RtInt npu, RtInt npv);
void Patch_bound_update(object_t *pObj);
void Patchmesh_init(object_t *pObj, UINT32 id, RtToken type, RtInt nu, RtToken uwrap, RtInt nv, RtToken vwrap, RtPointer pVerts);
void Patchmesh_free(object_t *pObj);
void Patchmesh_get_patch(object_t *pObj, object_t *pDst, RtInt ip);
RtInt Patchmesh_get_npatches(object_t *pObj, RtInt *nupatches, RtInt *nvpatches);
void Patchmesh_bound_update(object_t *pObj);
void Sphere_init(object_t *pObj, UINT32 id, RtFloat radius, RtFloat zmin, RtFloat zmax, RtFloat thetamax);
void Sphere_free(object_t *pObj);
void Sphere_dice(object_t *pObj, RtInt npu, RtInt npv);
void Sphere_bound_update(object_t *pObj);
void Light_init(object_t *pObj, UINT32 id);
void Light_free(object_t *pObj);
void Scene_Add(scene_t *pObj, object_t *pObject);
void Scene_Del(scene_t *pObj, object_t *pObject);
void Object_set_xform(object_t *pObj, RtMatrix *pXform);
void Object_set_attr(object_t *pObj, attr_t *pAttr);
void Object_init(object_t *pObj);
void Object_free(object_t *pObj);
RtPointer Object_get_var(object_t *pObj, RtToken name);
RtPointer Object_add_var(object_t *pObj, RtToken name, RtPointer *pValues);
// --------------------------------------------------------------
#endif // OBJECT_H
+47
View File
@@ -0,0 +1,47 @@
#include "ri.h"
#include "patches.h"
#define NU 13
#define MAXNPTS 100
#define F .5522847
/* Listing 6.3 Version of SurfOR() taking profile points */
/* and producing a bicubic */
float coeff[NU][2] = {
{ 1, 0 }, { 1, F }, { F, 1 }, { 0, 1 }, {-F, 1 }, {-1, F },
{-1, 0 }, {-1,-F }, {-F,-1 }, { 0,-1 }, { F,-1 }, { 1,-F }, { 1, 0} };
RtPoint mesh[MAXNPTS][NU];
void SurfOR(Point2D points[], int npoints)
{
int u, v;
/* coeff holds a matrix of coefficients for sweeping a point on the XZ
* plane circularly around the Z axis, with the circle approximated by
* 4 Bezier curves
*/
for (v = 0; v < npoints; v++) {
/*
* Start at the upper left of the mesh. For each
* control point on the circle being swept out,
* rotate every point on the XZ curve into position
*/
for (u = 0; u < NU; u++) {
/* Here comes each point on the XZ curve */
mesh[v][u][0] = points[v].x * coeff[u][0];
mesh[v][u][1] = points[v].x * coeff[u][1];
mesh[v][u][2] = points[v].y;
}
}
// RiBasis(RiPowerBasis, RI_POWERSTEP, RiPowerBasis, RI_POWERSTEP);
// RiBasis(RiHermiteBasis, RI_HERMITESTEP, RiHermiteBasis, RI_HERMITESTEP);
// RiBasis(RiCatmullRomBasis, RI_CATMULLROMSTEP, RiCatmullRomBasis, RI_CATMULLROMSTEP);
// RiBasis(RiBSplineBasis, RI_BSPLINESTEP, RiBSplineBasis, RI_BSPLINESTEP);
RiBasis(RiBezierBasis, RI_BEZIERSTEP, RiBezierBasis, RI_BEZIERSTEP);
RiPatchMesh(RI_BICUBIC,
(RtInt) NU, RI_NONPERIODIC,
(RtInt) npoints, RI_NONPERIODIC,
RI_P, (RtPointer) mesh,
RI_NULL);
}
+15
View File
@@ -0,0 +1,15 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 81 */
/* Listing 5.3 Bowling pin with normals assigned to vertices, defined using RiPointsPolygons */
#include "ri.h"
typedef struct _sPoint2D
{
RtFloat x, y;
} Point2D;
void SurfOR(Point2D points[], int npoints);
+55
View File
@@ -0,0 +1,55 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 81 */
/* Listing 5.3 Bowling pin with normals assigned to vertices, defined using RiPointsPolygons */
#include "ri.h"
#include "patches.h"
#define MAXNPTS 100
#define BEZIERWIDTH 12
#define F .5522847
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 102 */
/* Listing 6.4 Defining a surface of revolution using a wrapped patch mesh */
RtPoint mesh[MAXNPTS][BEZIERWIDTH];
float coeff[BEZIERWIDTH][2] = {
{ 1, 0 }, { 1, F }, { F, 1 }, { 0, 1 }, {-F, 1 }, {-1, F },
{-1, 0 }, {-1,-F }, {-F,-1 }, { 0,-1 }, { F,-1 }, { 1,-F } };
void BezierSurfOR(Point2D points[], int npoints, RtPoint mesh[][BEZIERWIDTH])
{
int u, v;
/*
* coeff holds a matrix of coefficients for sweeping a point on the XZ
* plane circularly around the Z axis, with the circle approximated by
* 4 Bezier curves.
*/
for (v = 0; v < npoints; v++) {
/*
* Start at the upper left of the mesh.
* For each control point on the circle being swept out,
* rotate every point on the XZ curve into position.
*/
for (u = 0; u < BEZIERWIDTH; u++) {
/* Here comes each point on the XZ curve */
mesh[v][u][0] = points[v].x * coeff[u][0];
mesh[v][u][1] = points[v].x * coeff[u][1];
mesh[v][u][2] = points[v].y;
}
}
}
void SurfOR(Point2D points[], int npoints)
{
BezierSurfOR(points, npoints, mesh);
RiBasis(RiBezierBasis, RI_BEZIERSTEP,
RiBezierBasis, RI_BEZIERSTEP);
RiPatchMesh(RI_BICUBIC,
(RtInt) BEZIERWIDTH, RI_PERIODIC,
(RtInt) npoints, RI_NONPERIODIC,
RI_P, (RtPointer) mesh, RI_NULL);
}
+102
View File
@@ -0,0 +1,102 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 81 */
/* Listing 5.3 Bowling pin with normals assigned to vertices, defined using RiPointsPolygons */
#include "ri.h"
#include "polys.h"
void PolySurfOR(Point2D *points, RtColor *colors, RtInt npoints)
{
int pt;
RtPoint point1, point2;
RtFloat *pp1, *pp2, *tmp;
RtPoint normal1, normal2;
RtFloat *pm1, *pm2;
pp1 = point1;
pp2 = point2;
pm1 = normal1;
pm2 = normal2;
/*
* For each adjacent pair of points in the surface profile,
* draw a hyperboloid by sweeping the line segment defined by
* those points about the z axis.
*/
pp1[0] = points[0].y;
pp1[1] = 0;
pp1[2] = points[0].x;
pm1[0] = points[0].x - points[1].x;
pm1[1] = 0;
pm1[2] = points[1].y - points[0].y;
for (pt = 1; pt < npoints-1; pt++) {
pp2[0] = points[pt].y;
pp2[1] = 0;
pp2[2] = points[pt].x;
pm2[0] = points[pt-1].x - points[pt+1].x;
pm2[1] = 0;
pm2[2] = points[pt+1].y - points[pt-1].y;
RiColor(colors[pt]);
PolyBoid(pp1, pp2, pm1, pm2, NDIVS, pt-1);
tmp = pp1; pp1 = pp2; pp2 = tmp;
tmp = pm1; pm1 = pm2; pm2 = tmp;
}
pt = npoints-1;
pp2[0] = points[pt].y;
pp2[1] = 0;
pp2[2] = points[pt].x;
pm2[0] = points[pt-1].x - points[pt].x;
pm2[1] = 0;
pm2[2] = points[pt].y - points[pt-1].y;
RiColor(colors[pt]);
PolyBoid(pp1, pp2, pm1, pm2, NDIVS, pt-1);
}
#define PI 3.14159
void getnextpair(float offset, RtPoint *ptrnextpair, RtFloat *point0, RtFloat *point1, RtInt ndivs)
{ float r;
double sin(), cos();
r = 2*PI*offset/ndivs;
ptrnextpair[0][0] = point0[0]*sin(r);
ptrnextpair[0][1] = point0[0]*cos(r);
ptrnextpair[0][2] = point0[2];
r = 2*PI*(offset-.5)/ndivs;
ptrnextpair[1][0] = point1[0]*sin(r);
ptrnextpair[1][1] = point1[0]*cos(r);
ptrnextpair[1][2] = point1[2];
}
/* Listing 5.3 Bowling pin with normals assigned to vertices, */
/* defined using RiPointsPolygons() */
RtPoint vertexstrip[MAXVERTS][2];
RtPoint normalstrip[MAXVERTS][2];
RtInt nverts[MAXVERTS][2], indices[MAXVERTS][2][3];
/*
* PolyBoid(): declare a polygonal "hyperboloid" using RiPointsPolygons()
*/
void PolyBoid(RtFloat *point0, RtFloat *point1, RtFloat *normal0, RtFloat *normal1, RtInt ndivs, RtInt parity)
{
int i;
for (i = 0; i <= ndivs; i++) {
getnextpair(i+parity/2.0, vertexstrip[i], point0, point1, ndivs);
getnextpair(i+parity/2.0, normalstrip[i], normal0, normal1, ndivs);
}
for (i = 0; i < ndivs; i++) {
nverts[i][0] = nverts[i][1] = 3;
indices[i][0][0] = i*2;
indices[i][0][1] = i*2+1;
indices[i][0][2] = (i+1)*2+1;
indices[i][1][0] = (i+1)*2+1;
indices[i][1][1] = (i+1)*2;
indices[i][1][2] = i*2;
}
RiPointsPolygons( (RtInt) (ndivs*2), (RtInt *) nverts, (RtInt *) indices,
RI_P, (RtPointer) vertexstrip,
RI_N, (RtPointer) normalstrip,
RI_NULL);
}
+19
View File
@@ -0,0 +1,19 @@
/* Copyrighted Pixar 1989 */
/* From the RenderMan Companion p. 81 */
/* Listing 5.3 Bowling pin with normals assigned to vertices, defined using RiPointsPolygons */
#include "ri.h"
typedef struct _sPoint2D
{
RtFloat x, y;
} Point2D;
void PolyBoid(RtFloat *point0, RtFloat *point1, RtFloat *normal0, RtFloat *normal1, RtInt ndivs, RtInt parity);
void PolySurfOR(Point2D *points, RtColor *colors, RtInt npoints);
void getnextpair(float offset, RtPoint *ptrnextpair, RtFloat *point0, RtFloat *point1, RtInt ndivs);
#define NDIVS 24
#define MAXVERTS 1000
+1002
View File
File diff suppressed because it is too large Load Diff
+40
View File
@@ -0,0 +1,40 @@
#ifndef RENDER_H
#define RENDER_H
#include "ri.h"
#include "types.h"
#include "matrix.h"
#include "graph_state.h"
#include "stack.h"
#include "object.h"
#include "vars.h"
#include "engine.h"
#include "imageio.h"
#include "colortypes.h"
#include "bintree.h"
typedef struct _sshaded_pixel_t
{
rgba_t cs, os;
double z;
} shaded_pixel_t;
typedef struct _srender_t
{
image_t *pImage;
RtFloat ku, kv, screen_u, screen_v;
graph_state_t *pG;
linkedlist_t *pLightList;
shaded_pixel_t **ppPixel;
} render_t;
void RenderWire_polygon(render_t *pObj, object_t *pObject);
void RenderWire_patch(render_t *pObj, object_t *pObject);
void RenderPoints(render_t *pObj, object_t *pObject);
void Render_patchmesh(render_t *pObj, object_t *pObject);
void Render_sphere(render_t *pObj, object_t *pObject);
void Render_patch(render_t *pObj, object_t *pObject);
#endif // RENDER_H
+1038
View File
File diff suppressed because it is too large Load Diff
+404
View File
@@ -0,0 +1,404 @@
/*************************************************************************
* ri.h - header file for the Blue Moon Rendering Tools (BMRT) *
*************************************************************************/
#ifndef RI_H
#define RI_H 1
#ifdef __cplusplus
extern "C" {
#endif
typedef short RtBoolean;
typedef int RtInt;
typedef float RtFloat;
typedef char *RtToken;
#ifndef NCOMPS
#define NCOMPS 3
#endif
typedef RtFloat RtColor[NCOMPS];
typedef RtFloat RtPoint[3];
typedef RtFloat RtVector[3];
typedef RtFloat RtNormal[3];
typedef RtFloat RtHpoint[3];
typedef RtFloat RtMatrix[4][4];
typedef RtFloat RtBasis[4][4];
typedef RtFloat RtBound[6];
typedef char *RtString;
typedef char *RtPointer;
#define RtVoid void
typedef RtFloat (*RtFilterFunc)(RtFloat, RtFloat, RtFloat, RtFloat);
typedef RtVoid (*RtErrorHandler)(RtInt code, RtInt severity, char *msg);
typedef RtVoid (*RtProcSubdivFunc)(RtPointer, RtFloat);
typedef RtVoid (*RtProcFreeFunc)(RtPointer);
typedef RtVoid (*RtArchiveCallback)(RtToken, char *, ...);
typedef RtVoid (*RtFunc)();
typedef RtPointer RtObjectHandle;
typedef RtPointer RtLightHandle;
typedef RtPointer RtContextHandle;
#define RI_FALSE 0
#define RI_TRUE 1
#define RI_INFINITY (RtFloat)1.0e38
#define RI_EPSILON (RtFloat)1.0e-10
#ifndef NULL
#define NULL 0
#endif
#define RI_NULL NULL
extern RtToken RI_FRAMEBUFFER, RI_FILE;
extern RtToken RI_RGB, RI_RGBA, RI_RGBZ, RI_RGBAZ, RI_A, RI_Z, RI_AZ;
extern RtToken RI_PERSPECTIVE, RI_ORTHOGRAPHIC;
extern RtToken RI_HIDDEN, RI_PAINT;
extern RtToken RI_CONSTANT, RI_SMOOTH;
extern RtToken RI_FLATNESS, RI_FOV;
extern RtToken RI_AMBIENTLIGHT, RI_POINTLIGHT, RI_DISTANTLIGHT, RI_SPOTLIGHT;
extern RtToken RI_INTENSITY, RI_LIGHTCOLOR, RI_FROM, RI_TO, RI_CONEANGLE,
RI_CONEDELTAANGLE, RI_BEAMDISTRIBUTION;
extern RtToken RI_MATTE, RI_METAL, RI_SHINYMETAL,
RI_PLASTIC, RI_PAINTEDPLASTIC;
extern RtToken RI_KA, RI_KD, RI_KS, RI_ROUGHNESS, RI_KR,
RI_TEXTURENAME, RI_SPECULARCOLOR;
extern RtToken RI_DEPTHCUE, RI_FOG, RI_BUMPY;
extern RtToken RI_MINDISTANCE, RI_MAXDISTANCE, RI_BACKGROUND,
RI_DISTANCE, RI_AMPLITUDE;
extern RtToken RI_INSIDE, RI_OUTSIDE, RI_LH, RI_RH;
extern RtToken RI_P, RI_PZ, RI_PW, RI_N, RI_NP, RI_CS,
RI_OS, RI_S, RI_T, RI_ST;
extern RtToken RI_BILINEAR, RI_BICUBIC;
extern RtToken RI_PRIMITIVE, RI_INTERSECTION, RI_UNION, RI_DIFFERENCE;
extern RtToken RI_PERIODIC, RI_NONPERIODIC, RI_CLAMP, RI_BLACK;
extern RtToken RI_IGNORE, RI_PRINT, RI_ABORT, RI_HANDLER;
extern RtToken RI_ORIGIN, RI_IDENTIFIER, RI_NAME;
extern RtToken RI_COMMENT, RI_STRUCTURE, RI_VERBATIM;
extern RtToken RI_LINEAR, RI_CUBIC, RI_WIDTH, RI_CONSTANTWIDTH;
extern RtToken RI_CURRENT, RI_WORLD, RI_OBJECT, RI_SHADER;
extern RtToken RI_RASTER, RI_NDC, RI_SCREEN, RI_CAMERA, RI_EYE;
extern RtToken RI_HOLE, RI_CREASE, RI_CORNER, RI_INTERPOLATEBOUNDARY;
extern RtBasis RiBezierBasis, RiBSplineBasis, RiCatmullRomBasis,
RiHermiteBasis, RiPowerBasis;
#define RI_BEZIERSTEP ((RtInt)3)
#define RI_BSPLINESTEP ((RtInt)1)
#define RI_CATMULLROMSTEP ((RtInt)1)
#define RI_HERMITESTEP ((RtInt)2)
#define RI_POWERSTEP ((RtInt)4)
extern RtInt RiLastError;
/* Basic control flow, scoping, stacks */
RtVoid RiBegin (RtToken name);
RtVoid RiEnd (void);
RtContextHandle RiGetContext (void);
RtVoid RiContext (RtContextHandle handle);
RtVoid RiFrameBegin (RtInt number);
RtVoid RiFrameEnd (void);
RtVoid RiWorldBegin (void);
RtVoid RiWorldEnd (void);
RtVoid RiAttributeBegin (void);
RtVoid RiAttributeEnd (void);
RtVoid RiTransformBegin (void);
RtVoid RiTransformEnd (void);
RtVoid RiSolidBegin (RtToken type);
RtVoid RiSolidEnd(void);
RtVoid RiMotionBegin (RtInt N, ...);
RtVoid RiMotionBeginV (RtInt N, RtFloat times[]);
RtVoid RiMotionEnd (void);
RtToken RiDeclare (char *name, char *declaration);
/* Options */
RtVoid RiClipping (RtFloat hither, RtFloat yon);
RtVoid RiColorSamples (RtInt N, RtFloat *nRGB, RtFloat *RGBn);
RtVoid RiCropWindow (RtFloat xmin, RtFloat xmax, RtFloat ymin, RtFloat ymax);
RtVoid RiDepthOfField (RtFloat fstop, RtFloat focallength, RtFloat focaldistance);
RtVoid RiDisplay (char *name, RtToken type, RtToken mode, ...);
RtVoid RiDisplayV (char *name, RtToken type, RtToken mode,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiExposure (RtFloat gain, RtFloat gamma);
RtVoid RiFormat (RtInt xres, RtInt yres, RtFloat aspect);
RtVoid RiFrameAspectRatio (RtFloat aspect);
RtVoid RiHider (RtToken type, ...);
RtVoid RiHiderV (RtToken type, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiImager (char *name, ...);
RtVoid RiImagerV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiOption (char *name, ...);
RtVoid RiOptionV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPixelFilter (RtFilterFunc function, RtFloat xwidth, RtFloat ywidth);
RtVoid RiPixelSamples (RtFloat xsamples, RtFloat ysamples);
RtVoid RiPixelVariance (RtFloat variation);
RtVoid RiProjection (char *name, ...);
RtVoid RiProjectionV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiQuantize (RtToken type, RtInt one, RtInt qmin, RtInt qmax, RtFloat ampl);
RtVoid RiRelativeDetail (RtFloat relativedetail);
RtVoid RiScreenWindow (RtFloat left, RtFloat right, RtFloat bot, RtFloat top);
RtVoid RiShutter (RtFloat smin, RtFloat smax);
/* Filters */
RtFloat RiBoxFilter (RtFloat x, RtFloat y, RtFloat xwidth, RtFloat ywidth);
RtFloat RiCatmullRomFilter (RtFloat x, RtFloat y, RtFloat xwidth, RtFloat ywidth);
RtFloat RiGaussianFilter (RtFloat x, RtFloat y, RtFloat xwidth, RtFloat ywidth);
RtFloat RiSincFilter (RtFloat x, RtFloat y, RtFloat xwidth, RtFloat ywidth);
RtFloat RiTriangleFilter (RtFloat x, RtFloat y, RtFloat xwidth, RtFloat ywidth);
/* Attributes */
RtVoid RiAttribute (char *name, ...);
RtVoid RiAttributeV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiBound (RtBound bound);
RtVoid RiColor (RtColor Cs);
RtVoid RiDetail (RtBound bound);
RtVoid RiDetailRange (RtFloat minvis, RtFloat lowtran,
RtFloat uptran, RtFloat maxvis);
RtVoid RiGeometricApproximation (RtToken type, RtFloat value);
RtVoid RiGeometricRepresentation (RtToken type);
RtVoid RiIlluminate (RtLightHandle light, RtBoolean onoff);
RtVoid RiMatte (RtBoolean onoff);
RtVoid RiOpacity (RtColor Cs);
RtVoid RiOrientation (RtToken orientation);
RtVoid RiReverseOrientation (void);
RtVoid RiShadingInterpolation (RtToken type);
RtVoid RiShadingRate (RtFloat size);
RtVoid RiSides (RtInt nsides);
RtVoid RiTextureCoordinates (RtFloat s1, RtFloat t1, RtFloat s2, RtFloat t2,
RtFloat s3, RtFloat t3, RtFloat s4, RtFloat t4);
/* Shaders */
RtLightHandle RiAreaLightSource (char *name, ...);
RtLightHandle RiAreaLightSourceV (char *name, RtInt n,
RtToken tokens[], RtPointer params[]);
RtVoid RiAtmosphere (char *name, ...);
RtVoid RiAtmosphereV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiDisplacement (char *name, ...);
RtVoid RiDisplacementV (char *name, RtInt n,
RtToken tokens[], RtPointer params[]);
RtVoid RiExterior (char *name, ...);
RtVoid RiExteriorV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiInterior (char *name, ...);
RtVoid RiInteriorV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
RtLightHandle RiLightSource (char *name, ...);
RtLightHandle RiLightSourceV (char *name, RtInt n,
RtToken tokens[], RtPointer params[]);
RtVoid RiSurface (char *name, ...);
RtVoid RiSurfaceV (char *name, RtInt n, RtToken tokens[], RtPointer params[]);
/* Transformations */
RtVoid RiConcatTransform (RtMatrix transform);
RtVoid RiCoordinateSystem (RtToken space);
RtVoid RiCoordSysTransform (RtToken space);
RtVoid RiIdentity (void);
RtVoid RiPerspective (RtFloat fov);
RtVoid RiRotate (RtFloat angle, RtFloat dx, RtFloat dy, RtFloat dz);
RtVoid RiScale (RtFloat dx, RtFloat dy, RtFloat dz);
RtVoid RiSkew (RtFloat angle, RtFloat dx1, RtFloat dy1, RtFloat dz1,
RtFloat dx2, RtFloat dy2, RtFloat dz2);
RtVoid RiTransform (RtMatrix transform);
RtVoid RiTranslate (RtFloat dx, RtFloat dy, RtFloat dz);
RtPoint *RiTransformPoints (RtToken fromspace, RtToken tospace,
RtInt npoints, RtPoint *points);
/* Geometric Primitives (and a couple gprim-specific attributes) */
RtVoid RiBasis (RtBasis ubasis, RtInt ustep, RtBasis vbasis, RtInt vstep);
RtVoid RiBlobby (RtInt nleaf, RtInt ncode, RtInt code[],
RtInt nflt, RtFloat flt[], RtInt nstr, RtString str[], ...);
RtVoid RiBlobbyV (RtInt nleaf, RtInt ncode, RtInt code[],
RtInt nflt, RtFloat flt[], RtInt nstr, RtString str[],
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiCone (RtFloat height, RtFloat radius, RtFloat thetamax, ...);
RtVoid RiConeV (RtFloat height, RtFloat radius, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiCurves (RtToken degree, RtInt ncurves,
RtInt nverts[], RtToken wrap, ...);
RtVoid RiCurvesV (RtToken degree, RtInt ncurves, RtInt nverts[], RtToken wrap,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiCylinder (RtFloat radius, RtFloat zmin, RtFloat zmax,
RtFloat thetamax, ...);
RtVoid RiCylinderV (RtFloat radius, RtFloat zmin, RtFloat zmax, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiDisk (RtFloat height, RtFloat radius, RtFloat thetamax, ...);
RtVoid RiDiskV (RtFloat height, RtFloat radius, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiGeneralPolygon (RtInt nloops, RtInt *nverts, ...);
RtVoid RiGeneralPolygonV (RtInt nloops, RtInt *nverts,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiGeometry (RtToken type, ...);
RtVoid RiGeometryV (RtToken type, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiHyperboloid (RtPoint point1, RtPoint point2, RtFloat thetamax, ...);
RtVoid RiHyperboloidV (RtPoint point1, RtPoint point2, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiNuPatch (RtInt nu, RtInt uorder, RtFloat *uknot,
RtFloat umin, RtFloat umax, RtInt nv, RtInt vorder,
RtFloat *vknot, RtFloat vmin, RtFloat vmax, ...);
RtVoid RiNuPatchV (RtInt nu, RtInt uorder, RtFloat *uknot,
RtFloat umin, RtFloat umax, RtInt nv, RtInt vorder,
RtFloat *vknot, RtFloat vmin, RtFloat vmax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiParaboloid (RtFloat rmax, RtFloat zmin,
RtFloat zmax, RtFloat thetamax, ...);
RtVoid RiParaboloidV (RtFloat rmax, RtFloat zmin, RtFloat zmax, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPatch (RtToken type, ...);
RtVoid RiPatchV (RtToken type, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPatchMesh (RtToken type, RtInt nu, RtToken uwrap,
RtInt nv, RtToken vwrap, ...);
RtVoid RiPatchMeshV (RtToken type, RtInt nu, RtToken uwrap, RtInt nv,
RtToken vwrap, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPoints (RtInt npts,...);
RtVoid RiPointsV (RtInt npts, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPointsGeneralPolygons (RtInt npolys, RtInt *nloops,
RtInt *nverts, RtInt *verts, ...);
RtVoid RiPointsGeneralPolygonsV (RtInt npolys, RtInt *nloops,
RtInt *nverts, RtInt *verts,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPointsPolygons (RtInt npolys, RtInt *nverts, RtInt *verts, ...);
RtVoid RiPointsPolygonsV (RtInt npolys, RtInt *nverts, RtInt *verts,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiPolygon (RtInt nvertices, ...);
RtVoid RiPolygonV (RtInt nvertices, RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiSphere (RtFloat radius, RtFloat zmin,
RtFloat zmax, RtFloat thetamax, ...);
RtVoid RiSphereV (RtFloat radius, RtFloat zmin, RtFloat zmax, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiSubdivisionMesh (RtToken scheme, RtInt nfaces,
RtInt nvertices[], RtInt vertices[],
RtInt ntags, RtToken tags[], RtInt nargs[],
RtInt intargs[], RtFloat floatargs[], ...);
RtVoid RiSubdivisionMeshV (RtToken scheme, RtInt nfaces,
RtInt nvertices[], RtInt vertices[],
RtInt ntags, RtToken tags[], RtInt nargs[],
RtInt intargs[], RtFloat floatargs[],
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiTorus (RtFloat majorrad, RtFloat minorrad, RtFloat phimin,
RtFloat phimax, RtFloat thetamax, ...);
RtVoid RiTorusV (RtFloat majorrad, RtFloat minorrad, RtFloat phimin,
RtFloat phimax, RtFloat thetamax,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiTrimCurve (RtInt nloops, RtInt *ncurves, RtInt *order,
RtFloat *knot, RtFloat *amin, RtFloat *amax,
RtInt *n, RtFloat *u, RtFloat *v, RtFloat *w);
/* Procedural primitives */
RtVoid RiProcedural (RtPointer data, RtBound bound,
RtVoid (*subdivfunc) (RtPointer, RtFloat),
RtVoid (*freefunc) (RtPointer));
RtVoid RiProcDelayedReadArchive (RtPointer data, RtFloat detail);
RtVoid RiProcRunProgram (RtPointer data, RtFloat detail);
RtVoid RiProcDynamicLoad (RtPointer data, RtFloat detail);
/* Object Instancing */
RtObjectHandle RiObjectBegin (void);
RtVoid RiObjectEnd (void);
RtVoid RiObjectInstance (RtObjectHandle handle);
/* Texture map creation */
RtVoid RiMakeCubeFaceEnvironment (char *px, char *nx, char *py,
char *ny, char *pz, char *nz,
char *tex, RtFloat fov, RtFilterFunc filterfunc,
RtFloat swidth, RtFloat twidth, ...);
RtVoid RiMakeCubeFaceEnvironmentV (char *px, char *nx, char *py,
char *ny, char *pz, char *nz,
char *tex, RtFloat fov, RtFilterFunc filterfunc,
RtFloat swidth, RtFloat twidth,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiMakeLatLongEnvironment (char *pic, char *tex, RtFilterFunc filterfunc,
RtFloat swidth, RtFloat twidth, ...);
RtVoid RiMakeLatLongEnvironmentV (char *pic, char *tex, RtFilterFunc filterfunc,
RtFloat swidth, RtFloat twidth,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiMakeShadow (char *pic, char *tex, ...);
RtVoid RiMakeShadowV (char *pic, char *tex,
RtInt n, RtToken tokens[], RtPointer params[]);
RtVoid RiMakeTexture (char *pic, char *tex, RtToken swrap, RtToken twrap,
RtFilterFunc filterfunc, RtFloat swidth, RtFloat twidth, ...);
RtVoid RiMakeTextureV (char *pic, char *tex, RtToken swrap, RtToken twrap,
RtFilterFunc filterfunc, RtFloat swidth, RtFloat twidth,
RtInt n, RtToken tokens[], RtPointer params[]);
/* Error Handlers */
RtVoid RiErrorHandler (RtErrorHandler handler);
RtVoid RiErrorAbort (RtInt code, RtInt severity, char *message);
RtVoid RiErrorIgnore (RtInt code, RtInt severity, char *message);
RtVoid RiErrorPrint (RtInt code, RtInt severity, char *message);
/* Reading and writing archive files */
RtVoid RiArchiveRecord (RtToken type, char *format, ...);
RtVoid RiReadArchive (RtString filename, RtArchiveCallback callback, ...);
RtVoid RiReadArchiveV (RtString filename, RtArchiveCallback callback,
int n, RtToken tokens[], RtPointer params[]);
/*
Error codes
1 - 10 System and File errors
11 - 20 Program Limitations
21 - 40 State Errors
41 - 60 Parameter and Protocol Errors
61 - 80 Execution Errors
*/
#define RIE_NOERROR 0
#define RIE_NOMEM 1 /* Out of memory */
#define RIE_SYSTEM 2 /* Miscellaneous system error */
#define RIE_NOFILE 3 /* File nonexistant */
#define RIE_BADFILE 4 /* Bad file format */
#define RIE_VERSION 5 /* File version mismatch */
#define RIE_DISKFULL 6 /* Target disk is full */
#define RIE_INCAPABLE 11 /* Optional RI feature */
#define RIE_OPTIONAL 11 /* Optional RI feature */
#define RIE_UNIMPLEMENT 12 /* Unimplemented feature */
#define RIE_LIMIT 13 /* Arbitrary program limit */
#define RIE_BUG 14 /* Probably a bug in renderer */
#define RIE_NOTSTARTED 23 /* RiBegin not called */
#define RIE_NESTING 24 /* Bad begin-end nesting */
#define RIE_NOTOPTIONS 25 /* Invalid state for options */
#define RIE_NOTATTRIBS 26 /* Invalid state for attributes */
#define RIE_NOTPRIMS 27 /* Invalid state for primitives */
#define RIE_ILLSTATE 28 /* Other invalid state */
#define RIE_BADMOTION 29 /* Badly formed motion block */
#define RIE_BADSOLID 30 /* Badly formed solid block */
#define RIE_BADTOKEN 41 /* Invalid token for request */
#define RIE_RANGE 42 /* Parameter out of range */
#define RIE_CONSISTENCY 43 /* Parameters inconsistent */
#define RIE_BADHANDLE 44 /* Bad object/light handle */
#define RIE_NOSHADER 45 /* Can't load requested shader */
#define RIE_MISSINGDATA 46 /* Required parameters not provided */
#define RIE_SYNTAX 47 /* Declare type syntax error */
#define RIE_MATH 61 /* Zerodivide, noninvert matrix, etc. */
/* Error severity levels */
#define RIE_INFO 0 /* Rendering stats & other info */
#define RIE_WARNING 1 /* Something seems wrong, maybe okay */
#define RIE_ERROR 2 /* Problem. Results may be wrong */
#define RIE_SEVERE 3 /* So bad you should probably abort */
#ifdef __cplusplus
}
#endif
#endif /* RI_H */
+619
View File
@@ -0,0 +1,619 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "ri.h"
#include "types.h"
#include "matrix.h"
#include "graph_state.h"
#include "object.h"
#include "vars.h"
#include "engine.h"
#include "ribgen.h"
// --------------------------------------------------------------
void RibgenPrintVars(engine_t *pObj, ri_var_t *pVar, RtPointer arg)
{
UINT32 i, j;
RtPoint *pPoint;
RtColor *pColor;
RtFloat *pFloat;
switch (pVar->etype)
{
case VT_POINT:
case VT_NORMAL:
case VT_VECTOR:
pPoint = (RtPoint*)arg;
fprintf(pObj->pFile, " \"%s\" [", pVar->name);
for (i=0; i < pVar->nvalues; i++)
{
fprintf(pObj->pFile, " %g %g %g", pPoint[i][0], pPoint[i][1], pPoint[i][2]);
}
fprintf(pObj->pFile, " ]\n");
break;
case VT_COLOR:
pColor = (RtColor*)arg;
fprintf(pObj->pFile, " \"%s\" [", pVar->name);
for (i=0; i < pVar->nvalues; i++)
{
fprintf(pObj->pFile, " %g %g %g", pColor[i][0], pColor[i][1], pColor[i][2]);
}
fprintf(pObj->pFile, " ]\n");
break;
case VT_FLOAT:
pFloat = (RtFloat*)arg;
fprintf(pObj->pFile, " \"%s\" [", pVar->name);
for (i=0; i < pVar->nvalues; i++)
{
fprintf(pObj->pFile, " %g", pFloat[i]);
}
fprintf(pObj->pFile, " ]\n");
break;
default:
fprintf(stderr, "RibgenPrintArgs(): Type handling of \"%s\" not implemented!\n", pVar->type);
break;
}
}
// --------------------------------------------------------------
void Ribgen_FrameBegin(engine_t *pObj, RtInt number)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "FrameBegin %d\n", number);
}
// --------------------------------------------------------------
void Ribgen_FrameEnd(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "FrameEnd\n");
}
// --------------------------------------------------------------
void Ribgen_WorldBegin(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "WorldBegin\n");
}
// --------------------------------------------------------------
void Ribgen_WorldEnd(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "WorldEnd\n");
}
// --------------------------------------------------------------
void Ribgen_AttributeBegin(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "AttributeBegin\n");
}
// --------------------------------------------------------------
void Ribgen_AttributeEnd(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "AttributeEnd\n");
}
// --------------------------------------------------------------
void Ribgen_TransformBegin(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "TransformBegin\n");
}
// --------------------------------------------------------------
void Ribgen_TransformEnd(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "TransformEnd\n");
}
// --------------------------------------------------------------
void Ribgen_ConcatTransform(engine_t *pObj, RtMatrix transform)
{
UINT32 i, j;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "ConcatTransform [");
for (i=0; i < 4; i++)
for (j=0; j < 4; j++)
fprintf(pObj->pFile, "%g", transform[i][j]);
fprintf(pObj->pFile, "]\n");
}
// --------------------------------------------------------------
void Ribgen_Identity(engine_t *pObj)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Identity\n");
}
// --------------------------------------------------------------
void Ribgen_Perspective(engine_t *pObj, RtFloat fov)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Perspective [%g]\n", fov);
}
// --------------------------------------------------------------
void Ribgen_Rotate(engine_t *pObj, RtFloat angle, RtFloat dx, RtFloat dy, RtFloat dz)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Rotate %g %g %g %g\n", angle, dx, dy, dz);
}
// --------------------------------------------------------------
void Ribgen_Scale(engine_t *pObj, RtFloat dx, RtFloat dy, RtFloat dz)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Scale %g %g %g\n", dx, dy, dz);
}
// --------------------------------------------------------------
void Ribgen_Transform(engine_t *pObj, RtMatrix transform)
{
UINT32 i, j;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Transform [");
for (i=0; i < 4; i++)
for (j=0; j < 4; j++)
fprintf(pObj->pFile, "%g", transform[i][j]);
fprintf(pObj->pFile, "]\n");
}
// --------------------------------------------------------------
void Ribgen_Translate(engine_t *pObj, RtFloat dx, RtFloat dy, RtFloat dz)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Translate %g %g %g\n", dx, dy, dz);
}
// --------------------------------------------------------------
void Ribgen_CropWindow(engine_t *pObj, RtFloat xmin, RtFloat xmax, RtFloat ymin, RtFloat ymax)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "CropWindow %g %g %g %g\n", xmin, xmax, ymin, ymax);
}
// --------------------------------------------------------------
void Ribgen_Display(engine_t *pObj, char *name, RtToken type, RtToken mode, va_list args)
{
RtPointer token, arg;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Display \"%s\" \"%s\" \"%s\"", name, type, mode);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
fprintf(pObj->pFile, "\"%s\" \"%s\"", token, arg);
}
fprintf(pObj->pFile, "\n");
}
// --------------------------------------------------------------
void Ribgen_Format(engine_t *pObj, RtInt xres, RtInt yres, RtFloat aspect)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Format %d %d %g\n", xres, yres, aspect);
}
// --------------------------------------------------------------
void Ribgen_FrameAspectRatio(engine_t *pObj, RtFloat aspect)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "FrameAspectRatio %g\n", aspect);
}
// --------------------------------------------------------------
void Ribgen_Projection(engine_t *pObj, char *name, va_list args)
{
RtToken token;
RtPointer arg;
RtFloat fov;
graph_state_t *pG = &pObj->gs;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Projection \"%s\"", name);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
if (token == RI_FOV)
{
fprintf(pObj->pFile, " \"%s\"", token);
arg = va_arg(args, RtPointer);
fov = *((RtFloat*)arg);
fprintf(pObj->pFile, " [%g]", fov);
}
}
fprintf(pObj->pFile, "\n");
}
// --------------------------------------------------------------
void Ribgen_ScreenWindow(engine_t *pObj, RtFloat left, RtFloat right, RtFloat bot, RtFloat top)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "ScreenWindow %g %g %g %g\n", left, right, bot, top);
}
// --------------------------------------------------------------
void Ribgen_Clipping(engine_t *pObj, RtFloat hither, RtFloat yon)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Clipping %g %g\n", hither, yon);
}
// --------------------------------------------------------------
void Ribgen_Color(engine_t *pObj, RtColor Cs)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Color [%g %g %g]\n", Cs[0], Cs[1], Cs[2]);
}
// --------------------------------------------------------------
void Ribgen_ShadingRate(engine_t *pObj, RtFloat size)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "ShadingRate %g\n", size);
}
// --------------------------------------------------------------
void Ribgen_Sides(engine_t *pObj, RtInt nsides)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Sides %d\n", nsides);
}
// --------------------------------------------------------------
void Ribgen_Illuminate (engine_t *pObj, RtInt id, RtBoolean onoff)
{
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Illuminate %d %d\n", id, onoff);
}
// --------------------------------------------------------------
void Ribgen_PointsPolygons(engine_t *pObj, RtInt npolys, RtInt *nverts, RtInt *verts, va_list args)
{
UINT32 j, v, n, num_verts = 0;
RtToken token;
RtPoint *pPoint;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "PointsPolygons");
while((token = va_arg(args, RtToken)) != RI_NULL)
{
if (token == RI_P)
{
pPoint = (RtPoint*)va_arg(args, RtPointer);
fprintf(pObj->pFile, " [");
for (n=0; n < (UINT32)npolys; n++)
fprintf(pObj->pFile, " %d", nverts[n]);
fprintf(pObj->pFile, " ] [");
v = 0;
for (n=0; n < (UINT32)npolys; n++)
{
for (j=0; j < (UINT32)nverts[n]; j++)
{
if ((UINT32)verts[v] > num_verts)
num_verts = verts[v];
fprintf(pObj->pFile, " %d", verts[v]);
v++;
}
}
num_verts++;
fprintf(pObj->pFile, " ]\n");
fprintf(pObj->pFile, " \"%s\" [", token);
for (j=0; j < num_verts; j++)
{
fprintf(pObj->pFile, " %g %g %g", pPoint[j][0], pPoint[j][1], pPoint[j][2]);
}
fprintf(pObj->pFile, " ]\n");
}
if (token == RI_N)
{
pPoint = (RtPoint*)va_arg(args, RtPointer);
fprintf(pObj->pFile, " \"%s\" [", token);
for (j=0; j < num_verts; j++)
{
fprintf(pObj->pFile, " %g %g %g", -pPoint[j][0], -pPoint[j][1], -pPoint[j][2]);
}
fprintf(pObj->pFile, " ]\n");
}
}
}
// --------------------------------------------------------------
void Ribgen_Polygon(engine_t *pObj, object_t *pObject, va_list args)
{
RtToken token;
RtPoint *pPoint;
ri_var_t *pVar;
RtPointer arg;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Polygon");
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
pVar = VarListFindByName(&pObject->vars, token);
RibgenPrintVars(pObj, pVar, arg);
}
}
// --------------------------------------------------------------
void Ribgen_Basis (engine_t *pObj, RtBasis ubasis, RtInt ustep, RtBasis vbasis, RtInt vstep)
{
RtToken uname, vname;
RtToken name_bezier = "bezier";
RtToken name_bspline = "b-spline";
RtToken name_catmullrom = "catmull-rom";
RtToken name_hermite = "hermite";
RtToken name_power = "power";
if (!pObj->pFile)
return;
uname = name_bezier;
if (ubasis == RiBSplineBasis)
uname = name_bspline;
if (ubasis == RiCatmullRomBasis)
uname = name_catmullrom;
if (ubasis == RiHermiteBasis)
uname = name_hermite;
if (ubasis == RiPowerBasis)
uname = name_power;
vname = name_bezier;
if (vbasis == RiBSplineBasis)
vname = name_bspline;
if (vbasis == RiCatmullRomBasis)
vname = name_catmullrom;
if (vbasis == RiHermiteBasis)
vname = name_hermite;
if (vbasis == RiPowerBasis)
vname = name_power;
fprintf(pObj->pFile, "Basis \"%s\" %d \"%s\" %d\n", uname, ustep, vname, vstep);
}
// --------------------------------------------------------------
void Ribgen_Curves(engine_t *pObj, object_t *pObject, RtToken type, RtInt ncurves, RtInt nverts[], RtToken wrap, va_list args)
{
UINT32 i, n, num_verts = 0;
RtToken token;
RtPointer arg;
ri_var_t *pVar;
if (!pObj->pFile)
return;
num_verts = 0;
fprintf(pObj->pFile, "Curves \"%s\" [", type);
for (n=0; n < ncurves; n++)
{
fprintf(pObj->pFile, " %d", nverts[n]);
num_verts += nverts[n];
}
fprintf(pObj->pFile, " ] \"%s\"", wrap);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
pVar = VarListFindByName(&pObject->vars, token);
RibgenPrintVars(pObj, pVar, arg);
}
}
// --------------------------------------------------------------
void Ribgen_Patch(engine_t *pObj, object_t *pObject, RtToken type, va_list args)
{
UINT32 i, npoints;
RtToken token;
RtPointer arg;
ri_var_t *pVar;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Patch \"%s\"", type);
npoints = 4;
if (type == RI_BICUBIC)
npoints = 16;
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
pVar = VarListFindByName(&pObject->vars, token);
RibgenPrintVars(pObj, pVar, arg);
}
}
// --------------------------------------------------------------
void Ribgen_Patchmesh(engine_t *pObj, object_t *pObject, RtToken type, RtInt nu, RtToken uwrap, RtInt nv, RtToken vwrap, va_list args)
{
UINT32 i;
RtToken token;
RtPointer arg;
ri_var_t *pVar;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "PatchMesh \"%s\" %d \"%s\" %d \"%s\"", type, nu, uwrap, nv, vwrap);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
pVar = VarListFindByName(&pObject->vars, token);
RibgenPrintVars(pObj, pVar, arg);
}
}
// --------------------------------------------------------------
void Ribgen_Sphere(engine_t *pObj, object_t *pObject, RtFloat radius, RtFloat zmin, RtFloat zmax, RtFloat thetamax, va_list args)
{
RtToken token;
RtPointer arg;
ri_var_t *pVar;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Sphere %g %g %g %g\n", radius, zmin, zmax, thetamax);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
pVar = VarListFindByName(&pObject->vars, token);
RibgenPrintVars(pObj, pVar, arg);
}
}
// --------------------------------------------------------------
void Ribgen_LightSource(engine_t *pObj, object_t *pObject, char *name, UINT32 id, va_list args)
{
RtToken token;
RtPointer arg;
ri_var_t *pVar;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "LightSource \"%s\" %d\n", name, id);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
pVar = VarListFindByName(&pObject->vars, token);
RibgenPrintVars(pObj, pVar, arg);
}
}
// --------------------------------------------------------------
void Ribgen_Surface(engine_t *pObj, char *name, va_list args)
{
RtToken token;
RtPointer arg;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Surface \"%s\"\n", name);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
}
}
// --------------------------------------------------------------
void Ribgen_Displacement(engine_t *pObj, char *name, va_list args)
{
RtToken token;
RtPointer arg;
if (!pObj->pFile)
return;
fprintf(pObj->pFile, "Displacement \"%s\"\n", name);
while((token = va_arg(args, RtToken)) != RI_NULL)
{
arg = va_arg(args, RtPointer);
}
}
// --------------------------------------------------------------
// --------------------------------------------------------------
// --------------------------------------------------------------
+43
View File
@@ -0,0 +1,43 @@
#ifndef RIBGEN_H
#define RIBGEN_H
// --------------------------------------------------------------
void Ribgen_FrameBegin(engine_t *pObj, RtInt number);
void Ribgen_FrameEnd(engine_t *pObj);
void Ribgen_WorldBegin(engine_t *pObj);
void Ribgen_WorldEnd(engine_t *pObj);
void Ribgen_AttributeBegin(engine_t *pObj);
void Ribgen_AttributeEnd(engine_t *pObj);
void Ribgen_TransformBegin(engine_t *pObj);
void Ribgen_TransformEnd(engine_t *pObj);
void Ribgen_ConcatTransform(engine_t *pObj, RtMatrix transform);
void Ribgen_Identity(engine_t *pObj);
void Ribgen_Perspective(engine_t *pObj, RtFloat fov);
void Ribgen_Rotate(engine_t *pObj, RtFloat angle, RtFloat dx, RtFloat dy, RtFloat dz);
void Ribgen_Scale(engine_t *pObj, RtFloat dx, RtFloat dy, RtFloat dz);
void Ribgen_Transform(engine_t *pObj, RtMatrix transform);
void Ribgen_Translate(engine_t *pObj, RtFloat dx, RtFloat dy, RtFloat dz);
void Ribgen_CropWindow(engine_t *pObj, RtFloat xmin, RtFloat xmax, RtFloat ymin, RtFloat ymax);
void Ribgen_Display(engine_t *pObj, char *name, RtToken type, RtToken mode, va_list args);
void Ribgen_Format(engine_t *pObj, RtInt xres, RtInt yres, RtFloat aspect);
void Ribgen_FrameAspectRatio(engine_t *pObj, RtFloat aspect);
void Ribgen_Projection(engine_t *pObj, char *name, va_list args);
void Ribgen_ScreenWindow(engine_t *pObj, RtFloat left, RtFloat right, RtFloat bot, RtFloat top);
void Ribgen_Basis (engine_t *pObj, RtBasis ubasis, RtInt ustep, RtBasis vbasis, RtInt vstep);
void Ribgen_Clipping(engine_t *pObj, RtFloat hither, RtFloat yon);
void Ribgen_Color(engine_t *pObj, RtColor Cs);
void Ribgen_ShadingRate(engine_t *pObj, RtFloat size);
void Ribgen_Sides(engine_t *pObj, RtInt nsides);
void Ribgen_Illuminate(engine_t *pObj, RtInt id, RtBoolean onoff);
void Ribgen_PointsPolygons(engine_t *pObj, RtInt npolys, RtInt *nverts, RtInt *verts, va_list args);
void Ribgen_Polygon(engine_t *pObj, object_t *pObject, va_list args);
void Ribgen_Curves(engine_t *pObj, object_t *pObject, RtToken type, RtInt ncurves, RtInt nverts[], RtToken wrap, va_list args);
void Ribgen_Patch(engine_t *pObj, object_t *pObject, RtToken type, va_list args);
void Ribgen_Patchmesh(engine_t *pObj, object_t *pObject, RtToken type, RtInt nu, RtToken uwrap, RtInt nv, RtToken vwrap, va_list args);
void Ribgen_Sphere(engine_t *pObj, object_t *pObject, RtFloat radius, RtFloat zmin, RtFloat zmax, RtFloat thetamax, va_list args);
void Ribgen_LightSource(engine_t *pObj, object_t *pObject, char *name, UINT32 id, va_list args);
void Ribgen_Surface(engine_t *pObj, char *name, va_list args);
void Ribgen_Displacement(engine_t *pObj, char *name, va_list args);
// --------------------------------------------------------------
#endif // RIBGEN_H
+98
View File
@@ -0,0 +1,98 @@
#include <stdlib.h>
#include <string.h>
#include "stack.h"
stack_t* Stack_free_top(stack_t *pObj)
{
stack_t *pCurr, *pNext;
if (pObj == NULL)
return pObj;
pCurr = pObj->pNext;
while(pCurr)
{
if (pCurr->pData)
jfree(pCurr->pData);
pNext = pCurr->pNext;
jfree(pCurr);
pCurr = pNext;
}
return pObj;
}
stack_t* Stack_free(stack_t *pObj)
{
stack_t *pCurr;
if (pObj == NULL)
return pObj;
while(pObj->pNext)
pObj = pObj->pNext;
while(pObj)
{
pCurr = pObj->pPrev;
if (pObj->pData)
jfree(pObj->pData);
jfree(pObj);
pObj = pCurr;
}
return pObj;
}
stack_t* Stack_push(stack_t *pObj, void **ppData, UINT32 size)
{
stack_t *pCurr;
if (!pObj)
{
pCurr = (stack_t*)jmalloc(sizeof(stack_t));
memset(pCurr, 0, sizeof(stack_t));
}
else
{
pCurr = pObj->pNext;
if (!pCurr)
{
pCurr = (stack_t*)jmalloc(sizeof(stack_t));
memset(pCurr, 0, sizeof(stack_t));
}
}
pCurr->pPrev = pObj;
if (size)
{
if (!pCurr->pData)
pCurr->pData = (void*)jmalloc(size);
if (pCurr->pPrev)
{
if (pCurr->pPrev->size == size)
memcpy(pCurr->pData, pCurr->pPrev->pData, size);
}
else
memset(pCurr->pData, 0, size);
}
pCurr->size = size;
*ppData = pCurr->pData;
return pCurr;
}
stack_t* Stack_pop(stack_t *pObj, void **ppData)
{
stack_t *pCurr;
if (pObj == NULL)
return pObj;
if (pObj->pPrev == NULL)
return pObj->pPrev;
pCurr = pObj->pPrev;
*ppData = pCurr->pData;
return pCurr;
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef STACK_H
#define STACK_H
#include "types.h"
typedef struct _sstack_t
{
UINT32 size;
void *pData;
struct _sstack_t *pNext;
struct _sstack_t *pPrev;
} stack_t;
stack_t* Stack_free_top(stack_t *pObj);
stack_t* Stack_free(stack_t *pObj);
stack_t* Stack_push(stack_t *pObj, void **ppData, UINT32 size);
stack_t* Stack_pop(stack_t *pObj, void **ppData);
#endif // STACK_H
+40
View File
@@ -0,0 +1,40 @@
/*************************************************************************/
/* Types.h */
/*************************************************************************/
#ifndef TYPES_H
#define TYPES_H
#ifndef UINT32
#define UINT32 unsigned int
#endif
#ifndef UINT16
#define UINT16 unsigned short
#endif
#ifndef UINT8
#define UINT8 unsigned char
#endif
#ifndef INT32
#define INT32 signed int
#endif
#ifndef INT16
#define INT16 signed short
#endif
#ifndef INT8
#define INT8 signed char
#endif
#define ERROR_BASE 0x80000000
#ifndef IS_ERROR
#define IS_ERROR(e) ((e & ERROR_BASE) == ERROR_BASE)
#endif
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
+269
View File
@@ -0,0 +1,269 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ri.h"
#include "types.h"
#include "matrix.h"
#include "graph_state.h"
#include "stack.h"
#include "object.h"
#include "imageio.h"
#include "colortypes.h"
#include "vars.h"
RtToken VAR_CLASSES[] = {"vertex", "uniform", "varying", "constant", NULL};
RtToken VAR_TYPES[] = {"float", "point", "color", "string", "integer", "vector", "normal", "matrix", "hpoint", NULL};
RtInt VAR_SIZES[] = {sizeof(RtFloat), sizeof(RtPoint), sizeof(RtColor), 1, sizeof(RtInt), sizeof(RtVector), sizeof(RtNormal), sizeof(RtMatrix), sizeof(RtHpoint), 0};
RtInt VAR_ECLASSES[] = {VC_VERTEX, VC_UNIFORM, VC_VARYING, VC_CONSTANT, 0};
RtInt VAR_ETYPES[] = {VT_FLOAT, VT_POINT, VT_COLOR, VT_STRING, VT_INTEGER, VT_VECTOR, VT_NORMAL, VT_MATRIX, VT_HPOINT, 0};
void VarListInit(var_list_t *pObj)
{
pObj->pVarList = NULL;
}
// Destroy list of variables
void VarListFree(var_list_t *pObj)
{
linkedlist_t *pVarList = pObj->pVarList;
ri_var_t *pVar;
pVarList = LinkedList_find_first(pVarList);
while(pVarList)
{
pVar = (ri_var_t*)pVarList->pData;
if (pVar->pValue)
{
jfree(pVar->pValue);
}
pVarList = pVarList->pNext;
}
LinkedList_free(pObj->pVarList);
pObj->pVarList = NULL;
}
// finds variable by its name
ri_var_t* VarListFindByName(var_list_t *pObj, UINT8 *name)
{
linkedlist_t *pFirst, *pVarList;
ri_var_t *pVar;
pFirst = LinkedList_find_first(pObj->pVarList);
// Find by comparing pointers
pVarList = pFirst;
while(pVarList)
{
pVar = (ri_var_t*)pVarList->pData;
if (name == pVar->name)
{
return pVar;
}
pVarList = pVarList->pNext;
}
// Find by comparing whole strings
pVarList = pFirst;
while(pVarList)
{
pVar = (ri_var_t*)pVarList->pData;
if (!stricmp(name, pVar->name))
{
return pVar;
}
pVarList = pVarList->pNext;
}
return NULL;
}
// Print variables
void VarListPrint(var_list_t *pObj)
{
linkedlist_t *pFirst, *pVarList;
ri_var_t *pVar;
pFirst = LinkedList_find_first(pObj->pVarList);
// Find by comparing pointers
pVarList = pFirst;
while(pVarList)
{
pVar = (ri_var_t*)pVarList->pData;
fprintf(stdout, "\nVariable \"%s\":\n", pVar->name);
fprintf(stdout, "\tClass: \"%s\"\n", pVar->cls);
fprintf(stdout, "\tType: \"%s\"\n", pVar->type);
fprintf(stdout, "\tValue size: %d\n", pVar->value_size);
fprintf(stdout, "\tNum. values: %d\n", pVar->nvalues);
pVarList = pVarList->pNext;
}
}
// Declare empty variable and add it to list of variables
ri_var_t* VarListDeclare(var_list_t *pObj, UINT8 *name, UINT8 *cls, UINT8 *type)
{
RtToken *pToken;
RtInt *pSize;
UINT32 *pEtypes;
UINT32 *pEclasses;
ri_var_t var;
if (VarListFindByName(pObj, name))
{
fprintf(stderr, "Variable \"%s\" is already declared!", name);
return NULL;
}
memset(&var, 0, sizeof(ri_var_t));
var.name = (char*)jmalloc(strlen(name)+1);
memcpy(var.name, name, strlen(name)+1);
pToken = VAR_CLASSES;
while(*pToken)
{
if (!stricmp(cls, *pToken))
{
var.cls = *pToken;
break;
}
pToken++;
}
if (*pToken == NULL)
{
fprintf(stderr, "Unknown class \"%s\" for variable!", cls);
return NULL;
}
pToken = VAR_TYPES;
pSize = VAR_SIZES;
pEtypes = VAR_ETYPES;
pEclasses = VAR_ECLASSES;
while(*pToken)
{
if (!stricmp(type, *pToken))
{
var.type = *pToken;
var.value_size = *pSize;
var.etype = *pEtypes;
var.ecls = *pEclasses;
break;
}
pToken++;
pSize++;
pEtypes++;
pEclasses++;
}
if (*pToken == NULL)
{
fprintf(stderr, "Unknown type \"%s\" for variable!", type);
return NULL;
}
pObj->pVarList = LinkedList_add(pObj->pVarList, 1, (void*)&var, sizeof(ri_var_t));
return (ri_var_t*)pObj->pVarList->pData;
}
// Add empty variable to list of variables
ri_var_t* VarListAdd(var_list_t *pObj, ri_var_t *pVar)
{
if (pVar->pValue != NULL)
fprintf(stderr, "VarListAdd(): Variable \"%s\" is not empty!\n", pVar->name);
pObj->pVarList = LinkedList_add(pObj->pVarList, 1, (void*)pVar, sizeof(ri_var_t));
return (ri_var_t*)pObj->pVarList->pData;
}
void VarListDelByName(var_list_t *pObj, UINT8 *name)
{
VarListDel(pObj, VarListFindByName(pObj, name));
}
void VarListDel(var_list_t *pObj, ri_var_t *pVar)
{
linkedlist_t *pFirst, *pVarList;
pFirst = LinkedList_find_first(pObj->pVarList);
// Find by comparing pointers
pVarList = pFirst;
while(pVarList)
{
if (((ri_var_t*)pVarList->pData)->name == pVar->name)
{
if (pVar->pValue)
{
jfree(pVar->pValue);
pVar->pValue = NULL;
}
LinkedList_del(pVarList);
break;
}
pVarList = pVarList->pNext;
}
}
// Allocs empty variable
RtPointer VarAlloc(ri_var_t *pObj, UINT32 nvalues)
{
if (!nvalues)
return NULL;
if (nvalues != pObj->nvalues)
{
if (pObj->pValue)
{
jfree(pObj->pValue);
pObj->pValue = NULL;
}
pObj->pValue = (RtPointer)jmalloc(nvalues*pObj->value_size);
pObj->nvalues = nvalues;
}
return pObj->pValue;
}
// Allocs empty variable and copy data into it
RtPointer VarSet(ri_var_t *pObj, RtPointer pValue, UINT32 nvalues)
{
RtPointer pData;
pData = VarAlloc(pObj, nvalues);
if (!pData)
return pData;
if (pValue)
memcpy(pData, pValue, nvalues*pObj->value_size);
else
memset(pData, 0, nvalues*pObj->value_size);
return pData;
}
RtPointer VarGetByName(var_list_t *pObj, RtToken name)
{
return VarGet(VarListFindByName(pObj, name));
}
// Return pointer to values of variable
RtPointer VarGet(ri_var_t *pObj)
{
if (!pObj)
return (RtPointer)pObj;
return (RtPointer)pObj->pValue;
}
+43
View File
@@ -0,0 +1,43 @@
#ifndef VARS_H
#define VARS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _sri_var_t
{
UINT32 etype;
UINT32 ecls;
RtToken name;
RtToken type;
RtToken cls;
RtPointer pValue;
UINT32 value_size, nvalues;
} ri_var_t;
typedef struct _svar_list_t
{
linkedlist_t *pVarList;
} var_list_t;
enum _eVarType {VT_FLOAT, VT_POINT, VT_COLOR, VT_STRING, VT_INTEGER, VT_VECTOR, VT_NORMAL, VT_MATRIX, VT_HPOINT};
enum _vVarClass {VC_VERTEX, VC_UNIFORM, VC_VARYING, VC_CONSTANT};
void VarListInit(var_list_t *pObj);
void VarListFree(var_list_t *pObj);
ri_var_t* VarListFindByName(var_list_t *pObj, UINT8 *name);
ri_var_t* VarListDeclare(var_list_t *pObj, UINT8 *name, UINT8 *cls, UINT8 *type);
ri_var_t* VarListAdd(var_list_t *pObj, ri_var_t *pVar);
void VarListDelByName(var_list_t *pObj, UINT8 *name);
void VarListDel(var_list_t *pObj, ri_var_t *pVar);
ri_var_t* VarListDeleteByName(var_list_t *pObj, UINT8 *name);
RtPointer VarAlloc(ri_var_t *pObj, UINT32 nvalues);
RtPointer VarSet(ri_var_t *pObj, RtPointer pValue, UINT32 nvalues);
RtPointer VarGet(ri_var_t *pObj);
void VarListPrint(var_list_t *pObj);
#endif // VARS_H