Skip to content

Commit

Permalink
Mesh Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
songxuan6970 committed Apr 24, 2013
1 parent 320becf commit 72eff13
Show file tree
Hide file tree
Showing 79 changed files with 20,215 additions and 0 deletions.
32 changes: 32 additions & 0 deletions HW5_modified/Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Application.cpp: implementation of the Application class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CS580HW.h"
#include "Application.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Application::Application()
{
m_pDisplay = NULL; // the display
m_pRender = NULL; // the renderer
m_pUserInput = NULL;
m_pFrameBuffer = NULL;
}

Application::~Application()
{
if(m_pFrameBuffer != NULL)
delete m_pFrameBuffer;
}

33 changes: 33 additions & 0 deletions HW5_modified/Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Application.h: interface for the Application class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_APPLICATION_H__3387B79A_B69F_491D_B782_81D9CAFAAB0F__INCLUDED_)
#define AFX_APPLICATION_H__3387B79A_B69F_491D_B782_81D9CAFAAB0F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Gz.h"
#include "disp.h"
#include "rend.h"

class Application
{
public:
Application();
virtual ~Application();

public:
GzDisplay* m_pDisplay; // the display
GzRender* m_pRender; // the renderer
GzInput* m_pUserInput;
char* m_pFrameBuffer; // Frame Buffer
int m_nWidth; // width of Frame Buffer
int m_nHeight; // height of Frame Buffer

virtual int Render()=0; // Pass user input data and call renderer
};

#endif // !defined(AFX_APPLICATION_H__3387B79A_B69F_491D_B782_81D9CAFAAB0F__INCLUDED_)
321 changes: 321 additions & 0 deletions HW5_modified/Application5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
// Application5.cpp: implementation of the Application5 class.
//
//////////////////////////////////////////////////////////////////////

/*
* application test code for homework assignment #5
*/

#include "stdafx.h"
#include "CS580HW.h"
#include "Application5.h"
#include "Gz.h"
#include "disp.h"
#include "rend.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define INFILE "WorldSpaceProjMesh.txt"//"ppot.asc"
#define OUTFILE "output.ppm"


extern int tex_fun(float u, float v, GzColor color); /* image texture function */
extern int ptex_fun(float u, float v, GzColor color); /* procedural texture function */

void shade(GzCoord norm, GzCoord color);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Application5::Application5()
{

}

Application5::~Application5()
{

}

int Application5::Initialize()
{
GzCamera camera;
int xRes, yRes, dispClass; /* display parameters */

GzToken nameListShader[9]; /* shader attribute names */
GzPointer valueListShader[9]; /* shader attribute pointers */
GzToken nameListLights[10]; /* light info */
GzPointer valueListLights[10];
int shaderType, interpStyle;
float specpower;
int status;

status = 0;

/*
* Allocate memory for user input
*/
m_pUserInput = new GzInput;

/*
* initialize the display and the renderer
*/
m_nWidth = 256; // frame buffer and display width
m_nHeight = 256; // frame buffer and display height

status |= GzNewFrameBuffer(&m_pFrameBuffer, m_nWidth, m_nHeight);

status |= GzNewDisplay(&m_pDisplay, GZ_RGBAZ_DISPLAY, m_nWidth, m_nHeight);

status |= GzGetDisplayParams(m_pDisplay, &xRes, &yRes, &dispClass);

status |= GzInitDisplay(m_pDisplay);

status |= GzNewRender(&m_pRender, GZ_Z_BUFFER_RENDER, m_pDisplay);

/* Translation matrix */
GzMatrix scale =
{
3.25, 0.0, 0.0, 0.0,
0.0, 3.25, 0.0, -3.25,
0.0, 0.0, 3.25, 3.5,
0.0, 0.0, 0.0, 1.0
};

GzMatrix rotateX =
{
1.0, 0.0, 0.0, 0.0,
0.0, .7071, .7071, 0.0,
0.0, -.7071, .7071, 0.0,
0.0, 0.0, 0.0, 1.0
};

GzMatrix rotateY =
{
.866, 0.0, -0.5, 0.0,
0.0, 1.0, 0.0, 0.0,
0.5, 0.0, .866, 0.0,
0.0, 0.0, 0.0, 1.0
};

#if 1 /* set up app-defined camera if desired, else use camera defaults */
camera.position[X] = -3;
camera.position[Y] = -25;
camera.position[Z] = -4;

camera.lookat[X] = 7.8;
camera.lookat[Y] = 0.7;
camera.lookat[Z] = 6.5;

camera.worldup[X] = -0.2;
camera.worldup[Y] = 1.0;
camera.worldup[Z] = 0.0;

camera.FOV = 63.7; /* degrees * /* degrees */

status |= GzPutCamera(m_pRender, &camera);
#endif

/* Start Renderer */
status |= GzBeginRender(m_pRender);

/* Light */
GzLight light1 = { {-0.7071, 0.7071, 0}, {0.5, 0.5, 0.9} };
GzLight light2 = { {0, -0.7071, -0.7071}, {0.9, 0.2, 0.3} };
GzLight light3 = { {0.7071, 0.0, -0.7071}, {0.2, 0.7, 0.3} };
GzLight ambientlight = { {0, 0, 0}, {0.3, 0.3, 0.3} };

/* Material property */
GzColor specularCoefficient = { 0.3, 0.3, 0.3 };
GzColor ambientCoefficient = { 0.1, 0.1, 0.1 };
GzColor diffuseCoefficient = {0.7, 0.7, 0.7};

/*
renderer is ready for frame --- define lights and shader at start of frame
*/

/*
* Tokens associated with light parameters
*/
nameListLights[0] = GZ_DIRECTIONAL_LIGHT;
valueListLights[0] = (GzPointer)&light1;
nameListLights[1] = GZ_DIRECTIONAL_LIGHT;
valueListLights[1] = (GzPointer)&light2;
nameListLights[2] = GZ_DIRECTIONAL_LIGHT;
valueListLights[2] = (GzPointer)&light3;
status |= GzPutAttribute(m_pRender, 3, nameListLights, valueListLights);

nameListLights[0] = GZ_AMBIENT_LIGHT;
valueListLights[0] = (GzPointer)&ambientlight;
status |= GzPutAttribute(m_pRender, 1, nameListLights, valueListLights);

/*
* Tokens associated with shading
*/
nameListShader[0] = GZ_DIFFUSE_COEFFICIENT;
valueListShader[0] = (GzPointer)diffuseCoefficient;

/*
* Select either GZ_COLOR or GZ_NORMALS as interpolation mode
*/
nameListShader[1] = GZ_INTERPOLATE;
interpStyle = GZ_NORMALS; /* Phong shading */
valueListShader[1] = (GzPointer)&interpStyle;

nameListShader[2] = GZ_AMBIENT_COEFFICIENT;
valueListShader[2] = (GzPointer)ambientCoefficient;
nameListShader[3] = GZ_SPECULAR_COEFFICIENT;
valueListShader[3] = (GzPointer)specularCoefficient;
nameListShader[4] = GZ_DISTRIBUTION_COEFFICIENT;
specpower = 32;
valueListShader[4] = (GzPointer)&specpower;

nameListShader[5] = GZ_TEXTURE_MAP;
#if 0 /* set up null texture function or valid pointer */
valueListShader[5] = (GzPointer)0;
#else
valueListShader[5] = (GzPointer)(tex_fun); /* or use ptex_fun */
#endif
status |= GzPutAttribute(m_pRender, 6, nameListShader, valueListShader);


status |= GzPushMatrix(m_pRender, scale);

GzMatrix Xwm; //hw4
memcpy(Xwm,scale,sizeof(GzMatrix));//hw4
ToUnitaryRotation(Xwm);//hw4
GzPushMatrixToXnorm(m_pRender, Xwm);//hw4

status |= GzPushMatrix(m_pRender, rotateY);

memcpy(Xwm,rotateY,sizeof(GzMatrix));//hw4
ToUnitaryRotation(Xwm);//hw4
GzPushMatrixToXnorm(m_pRender, Xwm);//hw4

status |= GzPushMatrix(m_pRender, rotateX);

memcpy(Xwm,rotateX,sizeof(GzMatrix));//hw4
ToUnitaryRotation(Xwm);//hw4
GzPushMatrixToXnorm(m_pRender, Xwm);//hw4

if (status) exit(GZ_FAILURE);

if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}

int Application5::Render()
{
GzToken nameListTriangle[3]; /* vertex attribute names */
GzPointer valueListTriangle[3]; /* vertex attribute pointers */
GzCoord vertexList[3]; /* vertex position coordinates */
GzCoord normalList[3]; /* vertex normals */
GzTextureIndex uvList[3]; /* vertex texture map indices */
char dummy[256];
int status;


/* Initialize Display */
status |= GzInitDisplay(m_pDisplay);

/*
* Tokens associated with triangle vertex values
*/
nameListTriangle[0] = GZ_POSITION;
nameListTriangle[1] = GZ_NORMAL;
nameListTriangle[2] = GZ_TEXTURE_INDEX;

// I/O File open
FILE *infile;
if( (infile = fopen( INFILE , "r" )) == NULL )
{
AfxMessageBox( "The input file was not opened\n" );
return GZ_FAILURE;
}

FILE *outfile;
if( (outfile = fopen( OUTFILE , "wb" )) == NULL )
{
AfxMessageBox( "The output file was not opened\n" );
return GZ_FAILURE;
}

/*
* Walk through the list of triangles, set color
* and render each triangle
*/
while( fscanf(infile, "%s", dummy) == 1) { /* read in tri word */
fscanf(infile, "%f %f %f %f %f %f %f %f",
&(vertexList[0][0]), &(vertexList[0][1]),
&(vertexList[0][2]),
&(normalList[0][0]), &(normalList[0][1]),
&(normalList[0][2]),
&(uvList[0][0]), &(uvList[0][1]) );
fscanf(infile, "%f %f %f %f %f %f %f %f",
&(vertexList[1][0]), &(vertexList[1][1]),
&(vertexList[1][2]),
&(normalList[1][0]), &(normalList[1][1]),
&(normalList[1][2]),
&(uvList[1][0]), &(uvList[1][1]) );
fscanf(infile, "%f %f %f %f %f %f %f %f",
&(vertexList[2][0]), &(vertexList[2][1]),
&(vertexList[2][2]),
&(normalList[2][0]), &(normalList[2][1]),
&(normalList[2][2]),
&(uvList[2][0]), &(uvList[2][1]) );

/*
* Set the value pointers to the first vertex of the
* triangle, then feed it to the renderer
* NOTE: this sequence matches the nameList token sequence
*/
valueListTriangle[0] = (GzPointer)vertexList;
valueListTriangle[1] = (GzPointer)normalList;
valueListTriangle[2] = (GzPointer)uvList;
GzPutTriangle(m_pRender, 3, nameListTriangle, valueListTriangle);
}

GzFlushDisplay2File(outfile, m_pDisplay); /* write out or update display to file*/
GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_pDisplay); // write out or update display to frame buffer

/*
* Close file
*/

if( fclose( infile ) )
AfxMessageBox( "The input file was not closed\n" );

if( fclose( outfile ) )
AfxMessageBox( "The output file was not closed\n" );

if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}

int Application5::Clean()
{
/*
* Clean up and exit
*/
int status = 0;

status |= GzFreeRender(m_pRender);
status |= GzFreeDisplay(m_pDisplay);

if (status)
return(GZ_FAILURE);
else
return(GZ_SUCCESS);
}



Loading

0 comments on commit 72eff13

Please sign in to comment.