home *** CD-ROM | disk | FTP | other *** search
- /****************************/
- /* MESH.C */
- /****************************/
-
-
- /****************************/
- /* EXTERNALS */
- /****************************/
-
- #include <qd3d.h>
- #include <QD3DGeometry.h>
- #include <QD3DSet.h>
- #include <QD3DView.h>
- #include <QD3DShader.h>
- #include <QD3DGroup.h>
- #include <QD3DRenderer.h>
- #include <QD3DAcceleration.h>
- #include <QD3DDrawContext.h>
- #include <QD3DTransform.h>
-
- extern TQ3ViewObject gMyViewObject;
- extern TQ3StyleObject gMyStyleObject_Interpolation;
- extern TQ3StyleObject gMyStyleObject_Backfacing;
- extern TQ3StyleObject gMyStyleObject_Fillstyle;
- extern TQ3ShaderObject gMyShaderObject_IlluminationShader;
-
-
-
- /****************************/
- /* PROTOTYPES */
- /****************************/
-
- extern void DoFatalAlert(Str255 s);
-
- void DrawMyObjects(void);
- void CreateMyMesh(void);
- TQ3GroupObject MakeMyRotationGroup(float x, float y, float z);
-
-
- /****************************/
- /* CONSTANTS */
- /****************************/
-
-
- /*********************/
- /* VARIABLES */
- /*********************/
-
- TQ3GroupObject gMyMeshGroup;
-
-
- /****************** CREATE MY MESH ******************/
- //
- // Creates a mesh geometry object.
- //
- // INPUT : none
- // OUTPUT : none
- //
-
- void CreateMyMesh(void)
- {
- long i,faceNum,vertexNum;
- float ambient = 1.0;
- TQ3ColorRGB color;
- TQ3AttributeSet vertexAttribs;
- TQ3Vertex3D vertex;
- TQ3MeshFace face;
- TQ3MeshVertex meshVertexIDList[16];
- TQ3MeshVertex faceVerts[4];
- TQ3GeometryObject meshObj;
- TQ3GroupObject groupObject;
- TQ3GroupPosition myGroupPosition;
-
- TQ3Point3D meshVertexCoords[16] =
- {
- -10,0,-10, -5,0,-10, 5,0,-10, 10,0,-10,
- -10,0,-5, -5,0,-5, 5,0,-5, 10,0,-5,
- -10,0,5, -5,0,5, 5,0,5, 10,0,5,
- -10,0,10, -5,0,10, 5,0,10, 10,0,10
- };
-
- long meshFaceVertices[9*4] =
- {
- 0,4,5,1, 1,5,6,2, 2,6,7,3,
- 4,8,9,5, 5,9,10,6, 6,10,11,7,
- 8,12,13,9, 9,13,14,10, 10,14,15,11
- };
-
-
- /* CREATE NEW MESH OBJECT */
-
- meshObj = Q3Mesh_New();
- if (meshObj == nil)
- DoFatalAlert("\pError making new mesh!");
-
-
- /***********************************/
- /* CREATE THE VERTICES OF THE MESH */
- /***********************************/
-
- for (vertexNum = 0; vertexNum < 16; vertexNum++)
- {
- /* MAKE ATTRIBUTES FOR THIS VERTEX */
-
- vertexAttribs = Q3AttributeSet_New(); // make attribs
- if (vertexAttribs == nil)
- DoFatalAlert("\pError making new vertex attribs.");
-
- color.r = (float)1/((Random()&0xf)+1); // make random RGB color
- color.g = (float)1/((Random()&0xf)+1); // for each vertex
- color.b = (float)1/((Random()&0xf)+1);
-
- Q3AttributeSet_Add(vertexAttribs, // set color
- kQ3AttributeTypeDiffuseColor, &color);
-
- Q3AttributeSet_Add(vertexAttribs, // set ambient
- kQ3AttributeTypeAmbientCoefficient, &ambient);
-
-
- /* SET COORDS & ATTRIBS FOR VERTEX */
-
- vertex.point.x = meshVertexCoords[vertexNum].x; // copy x,y,z coords
- vertex.point.y = meshVertexCoords[vertexNum].y;
- vertex.point.z = meshVertexCoords[vertexNum].z;
- vertex.attributeSet = vertexAttribs; // attach attribs
-
-
- /* CREATE A NEW VERTEX & KEEP THE RETURNED VERTEX ID */
-
- meshVertexIDList[vertexNum] = Q3Mesh_VertexNew(meshObj, &vertex);
-
-
- /* DISPOSE OF OUR REFERENCE TO THE ATTRIBUTE SET */
-
- Q3Object_Dispose(vertexAttribs);
- }
-
- /*****************************/
- /* CREATE MESH POLYGON FACES */
- /*****************************/
-
- for (i = 0, faceNum=0; faceNum < 9; faceNum++)
- {
- /* GET VERTEX ID’S FROM TABLE */
-
- faceVerts[0] = meshVertexIDList[meshFaceVertices[i++]]; // copy vertex
- faceVerts[1] = meshVertexIDList[meshFaceVertices[i++]]; // ID’S into local
- faceVerts[2] = meshVertexIDList[meshFaceVertices[i++]]; // array for easy
- faceVerts[3] = meshVertexIDList[meshFaceVertices[i++]]; // use.
-
-
- /* MAKE FACE WITH NO ATTRIBUTES */
-
- face = Q3Mesh_FaceNew(meshObj,4,&faceVerts[0],nil); // make the face
- if (face == nil)
- DoFatalAlert("\pError adding face to mesh object!");
- }
-
- /*********************/
- /* DO THE ROTATION */
- /*********************/
-
-
- /* CREATE A GROUP WITH ROTATION TRANSFORMS */
-
- groupObject = MakeMyRotationGroup(0.1,0.1,-0.4);
-
-
- /* ADD MESH TO THE GROUP */
-
- myGroupPosition = Q3Group_AddObject(groupObject, meshObj);
- if ( myGroupPosition == nil )
- DoFatalAlert("\pQ3Group_AddObject failed!");
-
-
- /* DISPOSE OF OUR REFERENCE TO THE MESH OBJECT */
-
- Q3Object_Dispose(meshObj);
-
- gMyMeshGroup = groupObject;
- }
-
-
- /***************** MAKE MY ROTATION GROUP ********************/
- //
- // Creates a rotate transform object for all three axes, then combines them
- // into a single rotation group object which is returned to the caller.
- //
- // INPUT: x,y,z = radian degrees to rotate on each axis
- //
-
- TQ3GroupObject MakeMyRotationGroup(float x, float y, float z)
- {
- TQ3GroupObject rotGroup;
- TQ3RotateTransformData xformData;
- TQ3TransformObject rotObject;
- TQ3GroupPosition myGroupPosition;
-
- /* CREATE A GROUP */
-
- rotGroup= Q3OrderedDisplayGroup_New();
- if ( rotGroup== nil )
- DoFatalAlert("\pCouldnt make rotation group!");
-
-
- /* CREATE AN X-AXIS ROTATE TRANSFORMATION */
-
- xformData.axis = kQ3AxisX;
- xformData.radians = x;
- rotObject = Q3RotateTransform_New(&xformData); // create new xform object
-
- myGroupPosition = Q3Group_AddObject(rotGroup, rotObject); // add to group
- if ( myGroupPosition == 0 )
- DoFatalAlert("\pError adding x-axis rotation object to rot group!");
- Q3Object_Dispose(rotObject); // dispose our reference
-
-
- /* CREATE A Y-AXIS ROTATE XFORM */
-
- xformData.axis = kQ3AxisY;
- xformData.radians = y;
- rotObject = Q3RotateTransform_New(&xformData); // create new xform object
-
- myGroupPosition = Q3Group_AddObject(rotGroup, rotObject); // add to group
- if ( myGroupPosition == 0 )
- DoFatalAlert("\pError adding y-axis rotation object to rot group!");
- Q3Object_Dispose(rotObject); // dispose our reference
-
-
- /* CREATE A Z-AXIS ROTATE XFORM */
-
- xformData.axis = kQ3AxisZ;
- xformData.radians = z;
- rotObject = Q3RotateTransform_New(&xformData); // create new xform object
-
- myGroupPosition = Q3Group_AddObject(rotGroup, rotObject); // add to group
- if ( myGroupPosition == 0 )
- DoFatalAlert("\pError adding z-axis rotation object to rot group!");
- Q3Object_Dispose(rotObject); // dispose our reference
-
-
- /* RETURN THE GROUP */
-
- return(rotGroup);
- }
-
-
- /******************* DRAW MY OBJECTS *********************/
- //
- // Draws a frame of animation for the FlyThru window.
- //
-
- void DrawMyObjects(void)
- {
- TQ3Status myStatus;
- TQ3ViewStatus myViewStatus;
-
- /* START RENDERING */
-
- myStatus = Q3View_StartRendering(gMyViewObject); // start rendering
- if ( myStatus == kQ3Failure )
- DoFatalAlert("\p Q3View_StartRendering Failed!");
-
- do
- {
- /* SET INTERPOLATION STYLE */
-
- myStatus = Q3Style_Submit(gMyStyleObject_Interpolation,gMyViewObject);
- if ( myStatus == kQ3Failure )
- DoFatalAlert("\p Q3Style_Submit Failed!");
-
- /* SET BACKFACING STYLE */
-
- myStatus = Q3Style_Submit(gMyStyleObject_Backfacing,gMyViewObject);
- if ( myStatus == kQ3Failure )
- DoFatalAlert("\p Q3Style_Submit Failed!");
-
-
- /* SET FILL STYLE */
-
- myStatus = Q3Style_Submit(gMyStyleObject_Fillstyle, gMyViewObject);
- if ( myStatus == kQ3Failure )
- DoFatalAlert("\p Q3Style_Submit Failed!");
-
-
- /* SET SHADER TO USE */
-
- myStatus = Q3Shader_Submit(gMyShaderObject_IlluminationShader, gMyViewObject);
- if ( myStatus == kQ3Failure )
- DoFatalAlert("\p Q3Shader_Submit Failed!");
-
-
- /* DRAW THE MESH GROUP OBJECT */
-
- myStatus = Q3DisplayGroup_Submit(gMyMeshGroup,gMyViewObject);
- if ( myStatus == kQ3Failure )
- DoFatalAlert("\p Drawing Mesh Failed!");
-
-
- /* SEE IF DONE */
-
- myViewStatus = Q3View_EndRendering(gMyViewObject);
-
- } while ( myViewStatus == kQ3ViewStatusRetraverse );
- }
-
-
-
-