int GzBeginRender(GzRender	*render)
{
    /*
    - set up for start of each frame - init frame buffer
    */

    // Check if the render is NULL and return failure
    if (render == NULL)
    {
        return GZ_FAILURE;
    }

    // Initialize the display to default values
    GzInitDisplay(render->display);

    //Return failure if the initialized values are not proper.
    if (NULL != render->display)
    {
        render->flatcolor[Red]   = 0;
        render->flatcolor[Green] = 0;
        render->flatcolor[Blue]  = 0;
    }
    else
    {
        return GZ_FAILURE;
    }

    return GZ_SUCCESS;
}
示例#2
0
文件: rend.cpp 项目: Baltiless/github
int GzBeginRender(GzRender *render)
{
/*  
- setup for start of each frame - init frame buffer color,alpha,z
- compute Xiw and projection xform Xpi from camera definition 
- init Ximage - put Xsp at base of stack, push on Xpi and Xiw 
- now stack contains Xsw and app can push model Xforms when needed 
*/ 
	if(render == NULL){
		errorCall("GzBeginRender","render is null");
		return GZ_FAILURE;
	}
	//setup for start of each frame, only one frame
	GzInitDisplay(render->display);

	//init matlevel
	render->matlevel = -1;
	
	//Compute Xsp
	computeXsp(render);
	
	//compute Xiw
	computeXiw(render);
	
	//compute Xpi
	computeXpi(render);
	
	//push Xsp Xpi Xiw
	GzPushMatrix(render,render->Xsp);
	GzPushMatrix(render,render->camera.Xpi);
	GzPushMatrix(render,render->camera.Xiw);
	
	return GZ_SUCCESS;
}
示例#3
0
//------------------------------------------------------------------------------
int GzBeginRender(GzRender *render) {
     
    if(NULL == render)
    {
        return GZ_FAILURE;
    }
    
    render->display->open = 1;
    
    //GzInitDisplay verifies that display is not NULL before using it.    
    GzInitDisplay(render->display);
    
    //Prepare Xpc    
    prepareXpc(&(render->camera));
    GzPushMatrix(render, render->camera.Xpi, false);
    
    //Prepare Xcw    
    prepareXcw(&(render->camera));    
    GzPushMatrix(render, render->camera.Xiw, true);    

    
    return GZ_SUCCESS;	
}
int GzBeginRender(GzRender *render)
{
/*  
- set up for start of each frame - clear frame buffer 
- compute Xiw and projection xform Xpi from camera definition 
- init Ximage - put Xsp at base of stack, push on Xpi and Xiw 
- now stack contains Xsw and app can push model Xforms if it want to. 
*/ 
for(int i=0;i<3;i++)
	render->flatcolor[i]=0;
	
render->matlevel=-1;
GzInitDisplay(render->display);
	
	setXsp(render);
	setXpi(render);
	setXiw(render);
	
	GzPushMatrix(render,render->Xsp);
	GzPushMatrix(render,render->camera.Xpi);
	GzPushMatrix(render,render->camera.Xiw);
	
	return GZ_SUCCESS;
}
示例#5
0
int Application4::Initialize()
{
    /* to be filled in by the app if it sets camera params */

    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 = 512;		// frame buffer and display width
    m_nHeight = 512;    // 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] = 13.2;
    camera.position[Y] = -8.7;
    camera.position[Z] = -14.8;

    camera.lookat[X] = 0.8;
    camera.lookat[Y] = 0.7;
    camera.lookat[Z] = 4.5;

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

    camera.FOV = 53.7;              /* 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;
#if 0
    interpStyle = GZ_COLOR;         /* Gouraud shading */
#else
    interpStyle = GZ_NORMALS;       /* Phong shading */
#endif

    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;

    status |= GzPutAttribute(m_pRender, 5, nameListShader, valueListShader);

    status |= GzPushMatrix(m_pRender, scale);
    status |= GzPushMatrix(m_pRender, rotateY);
    status |= GzPushMatrix(m_pRender, rotateX);

    if (status) exit(GZ_FAILURE);

    if (status)
        return(GZ_FAILURE);
    else
        return(GZ_SUCCESS);
}
示例#6
0
int Application1::Render() 
{
	int		i, j; 
	int		xRes, yRes, dispClass;	/* display parameters */ 
	int		status; 
 	 
	status = 0; 
 
	/* 
	 * initialize the display and the renderer 
	 */ 

	m_nWidth = 512;		// frame buffer and display width
	m_nHeight = 512;    // 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);  /* init for new frame */
 
	if (status) exit(GZ_FAILURE); 
 
	// I/O File open

	FILE *infile;
	if( (infile = fopen( INFILE1 , "r" )) == NULL )
	{
         AfxMessageBox( "The input file was not opened\n" );
		 return GZ_FAILURE;
	}

	FILE *outfile;
	if( (outfile = fopen( OUTFILE1 , "wb" )) == NULL )
	{
         AfxMessageBox( "The output file was not opened\n" );
		 return GZ_FAILURE;
	}
	
	int	ulx, uly, lrx, lry, r, g, b;
	while( fscanf(infile, "%d %d %d %d %d %d %d", 
			&ulx, &uly, &lrx, &lry, &r, &g, &b) == 7) { 
		for (j = uly; j <= lry; j++) {
		  for (i = ulx; i <= lrx; i++) {
		    GzPutDisplay(m_pDisplay, i, j, r, g, b, 1, 0);
		  }
		}
	} 
	
	GzFlushDisplay2File(outfile, m_pDisplay); 	/* write out or update display to file*/
	GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_pDisplay);	// write out or update display to frame buffer
 
	/* 
	 * Clean up and exit 
	 */ 

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

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

	status |= GzFreeDisplay(m_pDisplay); 
 
	if (status) 
		return(GZ_FAILURE); 
	else 
		return(GZ_SUCCESS); 

}
示例#7
0
int GzBeginRender(GzRender *render)
{
    /*
    - setup for start of each frame - init frame buffer color,alpha,z
    - compute Xiw and projection xform Xpi from camera definition
    - init Ximage - put Xsp at base of stack, push on Xpi and Xiw
    - now stack contains Xsw and app can push model Xforms when needed
    */
    if (render == NULL)
        return GZ_FAILURE;

    GzInitDisplay(render->display);
    if (render->display == NULL)
        return GZ_FAILURE;
    //compute Xiw
    GzCoord cl, camZaxis;
    cl[X] = render->camera.lookat[X] - render->camera.position[X];
    cl[Y] = render->camera.lookat[Y] - render->camera.position[Y];
    cl[Z] = render->camera.lookat[Z] - render->camera.position[Z];

    float normcl;
    normcl = sqrt(cl[X] * cl[X] + cl[Y] * cl[Y] + cl[Z] * cl[Z]);
    camZaxis[X] = cl[X] / normcl;
    camZaxis[Y] = cl[Y] / normcl;
    camZaxis[Z] = cl[Z] / normcl;

    GzCoord  camUp, camYaxis;
    float zdotup = ((render->camera.worldup[X]) * (camZaxis[X])
                    + (render->camera.worldup[Y]) * (camZaxis[Y])
                    + (render->camera.worldup[Z]) * (camZaxis[Z]));
    camUp[X] = render->camera.worldup[X] - zdotup * camZaxis[X];
    camUp[Y] = render->camera.worldup[Y] - zdotup * camZaxis[Y];
    camUp[Z] = render->camera.worldup[Z] - zdotup * camZaxis[Z];

    float normup;
    normup = sqrt(camUp[X] * camUp[X] + camUp[Y] * camUp[Y] + camUp[Z] * camUp[Z]);
    camYaxis[X] = camUp[X] / normup;
    camYaxis[Y] = camUp[Y] / normup;
    camYaxis[Z] = camUp[Z] / normup;

    GzCoord camXaxis;

    camXaxis[X] = camYaxis[Y] * camZaxis[Z] - camYaxis[Z] * camZaxis[Y];
    camXaxis[Y] = camYaxis[Z] * camZaxis[X] - camYaxis[X] * camZaxis[Z];
    camXaxis[Z] = camYaxis[X] * camZaxis[Y] - camYaxis[Y] * camZaxis[X];

    float xc, yc, zc;
    xc = (camXaxis[X] * render->camera.position[X]) +
         (camXaxis[Y] * render->camera.position[Y]) + (camXaxis[Z] * render->camera.position[Z]);
    yc = (camYaxis[X] * render->camera.position[X]) +
         (camYaxis[Y] * render->camera.position[Y]) + (camYaxis[Z] * render->camera.position[Z]);
    zc = (camZaxis[X] * render->camera.position[X]) +
         (camZaxis[Y] * render->camera.position[Y]) + (camZaxis[Z] * render->camera.position[Z]);

    render->camera.Xiw[0][0] = camXaxis[X];
    render->camera.Xiw[0][1] = camXaxis[Y];
    render->camera.Xiw[0][2] = camXaxis[Z];
    render->camera.Xiw[0][3] = -(xc);
    render->camera.Xiw[1][0] = camYaxis[X];
    render->camera.Xiw[1][1] = camYaxis[Y];
    render->camera.Xiw[1][2] = camYaxis[Z];
    render->camera.Xiw[1][3] = -(yc);
    render->camera.Xiw[2][0] = camZaxis[X];
    render->camera.Xiw[2][1] = camZaxis[Y];
    render->camera.Xiw[2][2] = camZaxis[Z];
    render->camera.Xiw[2][3] = -(zc);
    render->camera.Xiw[3][0] = 0;
    render->camera.Xiw[3][1] = 0;
    render->camera.Xiw[3][2] = 0;
    render->camera.Xiw[3][3] = 1;

    //Xpi
    float d;
    d = 1 / (tan((render->camera.FOV / (float)2.0) * (Pi / (float)180.0)));
    render->camera.Xpi[0][0] = 1;
    render->camera.Xpi[0][1] = 0;
    render->camera.Xpi[0][2] = 0;
    render->camera.Xpi[0][3] = 0;

    render->camera.Xpi[1][0] = 0;
    render->camera.Xpi[1][1] = 1;
    render->camera.Xpi[1][2] = 0;
    render->camera.Xpi[1][3] = 0;

    render->camera.Xpi[2][0] = 0;
    render->camera.Xpi[2][1] = 0;
    render->camera.Xpi[2][2] = 1/d;
    render->camera.Xpi[2][3] = 0;

    render->camera.Xpi[3][0] = 0;
    render->camera.Xpi[3][1] = 0;
    render->camera.Xpi[3][2] = 1/d;
    render->camera.Xpi[3][3] = 1;

    //init Ximage - put Xsp at base of stack, push on Xpi and Xiw
    GzPushMatrix(render, render->Xsp);
    GzPushMatrix(render, render->camera.Xpi);
    GzPushMatrix(render, render->camera.Xiw);

    return GZ_SUCCESS;
}
int Application3::Render()
{
	GzCamera	camera;
	GzToken		nameListTriangle[3]; 	/* vertex attribute names */
	GzPointer	valueListTriangle[3]; 	/* vertex attribute pointers */
	GzToken		nameListColor[3];		/* color type names */
	GzPointer	valueListColor[3];	/* color type rgb pointers */
	GzColor		color;
	GzCoord		vertexList[3];	/* vertex position coordinates */
	GzCoord		normalList[3];	/* vertex normals */
	GzTextureIndex  	uvList[3];		/* vertex texture map indices */
	char		dummy[256];
	int			i, j;
	int			xRes, yRes, dispClass;	/* display parameters */
	int			status;

	status = 0;

	status |= GzInitDisplay(m_pDisplay);

	/*
	* Tokens associated with triangle vertex values
	*/
	nameListTriangle[0] = GZ_POSITION;

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

	FILE *outfile;
	if ((outfile = fopen(OUTFILE3, "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
	*/
	i = 0;
	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 up shading attributes for each triangle
		*/
		shade(normalList[0], color);/* shade based on the norm of vert0 */
		valueListColor[0] = (GzPointer)color;
		nameListColor[0] = GZ_RGB_COLOR;
		GzPutAttribute(m_pRender, 1, nameListColor, valueListColor);

		/*
		* Set the value pointers to the first vertex of the
		* triangle, then feed it to the renderer
		*/
		valueListTriangle[0] = (GzPointer)vertexList;
		GzPutTriangle(m_pRender, 1, 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::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 */
	for (int i = 0; i < AAKERNEL_SIZE; i++)
	{
		status |= GzInitDisplay(m_pDisplay[i]); 
	}
	/* 
	* 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; 
		 for (int i = 0; i < AAKERNEL_SIZE; i++)
		 {
			 GzPutTriangle(m_pRender[i], 3, nameListTriangle, valueListTriangle);
		 } 
	} 
	combine2One();
	GzFlushDisplay2File(outfile, m_finalDisplay); 	/* write out or update display to file*/
	GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_finalDisplay);	// 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::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 */
	/////for (int i = 0; i < AAKERNEL_SIZE; i++) 
	////status |= GzInitDisplay(m_pDisplay); 
	
	/////for (int i = 0; i < AAKERNEL_SIZE; i++) {

		

		/////GzNewDisplay(&AArender->display, GZ_RGBAZ_DISPLAY, m_pRender->display->xres, m_pRender->display->yres);
	/////}
	/* 
	* 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(_T( "The input file was not opened\n" ));
		 return GZ_FAILURE;
	}

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

	/* 
	* Walk through the list of triangles, set color 
	* and render each triangle 
	*/ 
	////m_pAADisplays = (GzDisplay **)malloc(6 * sizeof(GzDisplay*));
	////m_pAADisplays = new GzDisplay*[6];
	////m_pAARenders = (GzRender **)malloc(6 * sizeof(GzRender*));
	////m_pAARenders = new GzRender*[6];
	for (int i = 0; i < AAKERNEL_SIZE; i++) {
		/////status |= GzNewRender(&m_pAARenders[i], m_pAADisplays[i]);
		m_pAARenders[i] = new GzRender();
		memcpy(m_pAARenders[i], m_pRender, sizeof(GzRender));
		status |= GzNewDisplay(&m_pAADisplays[i], m_nWidth, m_nHeight);
		status |= GzInitDisplay(m_pAADisplays[i]);
		m_pAARenders[i]->display = m_pAADisplays[i];
		m_pAARenders[i]->Xshift = AAFilter[i][0];
		m_pAARenders[i]->Yshift = AAFilter[i][1];
		m_pAARenders[i]->Weight = AAFilter[i][2];
	}

	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);
		//#if ANTIALIASING
			for (int i = 0; i < AAKERNEL_SIZE; i++) 				
				GzPutTriangle(m_pAARenders[i], 3, nameListTriangle, valueListTriangle);
		//#endif	
	}

	GzInitDisplay(m_pRender->display);
	for (int y = 0; y < m_pRender->display->yres; y++) {
		for (int x = 0; x < m_pRender->display->xres; x++) {
			
			GzDisplay* display = m_pRender->display;
			int index = ARRAY(x, y);
			
			for (int i = 0; i < AAKERNEL_SIZE; i++) {
				m_pRender->display->fbuf[index].red += m_pAARenders[i]->display->fbuf[index].red	* m_pAARenders[i]->Weight;
				m_pRender->display->fbuf[index].green += m_pAARenders[i]->display->fbuf[index].green * m_pAARenders[i]->Weight;
				m_pRender->display->fbuf[index].blue += m_pAARenders[i]->display->fbuf[index].blue	* m_pAARenders[i]->Weight;
			}
		}
	}
	

	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(_T("The input file was not closed\n"));

	if( fclose( outfile ) )
		AfxMessageBox(_T("The output file was not closed\n"));
 
	if (status) 
		return(GZ_FAILURE); 
	else 
		return(GZ_SUCCESS); 
}
示例#11
0
int Project::Render() 
{
	GzToken		nameListTriangle[2]; 	/* vertex attribute names */
	GzPointer	valueListTriangle[2]; 	/* vertex attribute pointers */
	GzCoord		vertexList[3];	/* vertex position coordinates */ 
	GzCoord		normalList[3];	/* vertex normals */
	int			status; 

	objParser.getNumOfVertices();


	/* Initialize Display */
	status |= GzInitDisplay(m_pDisplay); 
	
	/* 
	* Tokens associated with triangle vertex values 
	*/ 
	nameListTriangle[0] = GZ_POSITION; 
	nameListTriangle[1] = GZ_NORMAL; 
	 
	// I/O File open

	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 
	*/ 
	for (int i = 0; i < objParser.getNumOfFaces(); ++i)
	{
		Face face = objParser.getFace(i + 1);
		vector<Face::FaceVertex> faceVertices = face.faceVertices;
		for (int j = 0; j < faceVertices.size(); ++j)
		{
			Face::FaceVertex faceVertex = faceVertices[j];
			Vertex vertex = objParser.getVertex(faceVertex.v);
			VertexNormal vertexNormal = objParser.getVertexNormal(faceVertex.vn);
			vertexList[j][0] = vertex.x;
			vertexList[j][1] = vertex.y;
			vertexList[j][2] = vertex.z;
			normalList[j][0] = vertexNormal.x;
			normalList[j][1] = vertexNormal.y;
			normalList[j][2] = vertexNormal.z;
		}
		valueListTriangle[0] = (GzPointer)vertexList;
		valueListTriangle[1] = (GzPointer)normalList;
		GzPutTriangle(m_pRender, 2, 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

	switch(currRenderFunc)
	{
	case 1:
		{
			CreateDepthMap(m_pDisplay, m_pDepthMap);
			BlurDepthMap(m_pDepthMap, m_pDepthMapBlurred);
			CopyToFrameBuffer(m_pFrameBuffer, m_pDepthMapBlurred);
			break;
		}

	case 2:
		{
			CreateDepthMap(m_pDisplay, m_pDepthMap);
			BlurDepthMap(m_pDepthMap, m_pDepthMapBlurred);
			GetEdgeMap(m_pDepthMapBlurred, m_pEdgeMap, m_pFF1_x, m_pFF1_y, m_pGradient);
			CopyToFrameBuffer(m_pFrameBuffer, m_pEdgeMap);
			break;
		}

	case 3:
		{
			ApplyGaussianBlur(m_pFrameBuffer, m_p3DImageBlurred, m_ifilterLength);
			CopyToFrameBuffer(m_pFrameBuffer, m_p3DImageBlurred);
			break;
		}

	case 4:
		{
			ApplyGaussianBlur(m_pFrameBuffer, m_p3DImageBlurred, m_ifilterLength);
			MultiplyImageWithTexture(m_p3DImageBlurred, m_pProceduralTexture, m_pTextureImageMult);
			CopyToFrameBuffer(m_pFrameBuffer, m_pTextureImageMult);
			break;
		}

	case 5:
		{
			ApplyGaussianBlur(m_pFrameBuffer, m_p3DImageBlurred, m_ifilterLength);
			MultiplyImageWithTexture(m_p3DImageBlurred, m_pImageTexture, m_pTextureImageMult);
			CopyToFrameBuffer(m_pFrameBuffer, m_pTextureImageMult);
			break;
		}

	case 6:
		{
			CreateDepthMap(m_pDisplay, m_pDepthMap);
			BlurDepthMap(m_pDepthMap, m_pDepthMapBlurred);
			GetEdgeMap(m_pDepthMapBlurred, m_pEdgeMap, m_pFF1_x, m_pFF1_y, m_pGradient);
			FindForceField(m_pFF1_x, m_pFF1_y, m_pGradient, m_pFF_x, m_pFF_y);
			unsigned char* output;
			GzNewFrameBuffer(&output, m_nWidth, m_nHeight);
			DrawLooseStrokes(m_pEdgeMap, m_pFF_x, m_pFF_y, output);
			CopyToFrameBuffer(m_pFrameBuffer, output);
			break;
		}

	case 7:
		{
			CreateDepthMap(m_pDisplay, m_pDepthMap);
			BlurDepthMap(m_pDepthMap, m_pDepthMapBlurred);
			GetEdgeMap(m_pDepthMapBlurred, m_pEdgeMap, m_pFF1_x, m_pFF1_y, m_pGradient);
			ApplyGaussianBlur(m_pFrameBuffer, m_p3DImageBlurred, m_ifilterLength);
			if (lastTexture == 1)
				CopyToFrameBuffer(m_pTexture, m_pImageTexture);
			else
				CopyToFrameBuffer(m_pTexture, m_pProceduralTexture);
			MultiplyImageWithTexture(m_p3DImageBlurred, m_pTexture, m_pTextureImageMult);
			FindForceField(m_pFF1_x, m_pFF1_y, m_pGradient, m_pFF_x, m_pFF_y);
			DrawLooseStrokes(m_pEdgeMap, m_pFF_x, m_pFF_y, m_pTextureImageMult);
			CopyToFrameBuffer(m_pFrameBuffer, m_pTextureImageMult);
			break;
		}
	default:
		break;
	}


	//CreateDepthMap(m_pDisplay, m_pDepthMap);
	//BlurDepthMap(m_pDepthMap, m_pDepthMapBlurred);
	//GetEdgeMap(m_pDepthMapBlurred, m_pEdgeMap, m_pFF1_x, m_pFF1_y, m_pGradient);
	//ApplyGaussianBlur(m_pFrameBuffer, m_p3DImageBlurred, 9);
	//MultiplyImageWithTexture(m_p3DImageBlurred, m_pPaperTexture, m_pTextureImageMult);
	//
	//FindForceField(m_pFF1_x, m_pFF1_y, m_pGradient, m_pFF_x, m_pFF_y);
	//unsigned char* output;
	//GzNewFrameBuffer(&output, m_nWidth, m_nHeight);
	////DrawLine(line, 1, 230, 200, 210, 0, 0, 0);
	////DrawForceField(m_pDepthMap, m_pFF_x, m_pFF_y);
	//DrawLooseStrokes(m_pEdgeMap, m_pFF_x, m_pFF_y, m_pTextureImageMult);
	//CopyToFrameBuffer(m_pFrameBuffer, m_pTextureImageMult);

	/* 
	 * Close file
	 */

	if( fclose( outfile ) )
      AfxMessageBox( "The output file was not closed\n" );
 
	if (status) 
		return(GZ_FAILURE); 
	else 
		return(GZ_SUCCESS); 
}
示例#12
0
int Application6::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 */
	for (int i = 0; i < AAKERNEL_SIZE; i++)
	{
		status |= GzInitDisplay(m_pDisplay[i]);
	}
	
	/* 
	* 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]) );

	/* Generate two quads:
		-One to the left of the z axis
		-One to the right of the z axis
		
	*/
	int numPolys = 5;
	float numPolysHalved = float(numPolys) / 2.0f;
	for (float i = -numPolysHalved; i < numPolysHalved; i++){
		for (int j = 0; j < numPolys; j++){
			//First triangle of the quad
			vertexList[0][0] = SIZE * i; vertexList[0][1] = 0; vertexList[0][2] = SIZE * j;
			vertexList[1][0] = SIZE * i; vertexList[1][1] = 0; vertexList[1][2] = SIZE * j + SIZE;
			vertexList[2][0] = SIZE * i + SIZE; vertexList[2][1] = 0; vertexList[2][2] = SIZE * j + SIZE;

			normalList[0][0] = 0; normalList[0][1] = 1; normalList[0][2] = 0;
			normalList[1][0] = 0; normalList[1][1] = 1; normalList[1][2] = 0;
			normalList[2][0] = 0; normalList[2][1] = 1; normalList[2][2] = 0;

			uvList[0][0] = 0; uvList[0][1] = 1;
			uvList[1][0] = 0; uvList[1][1] = 0;
			uvList[2][0] = 1; uvList[2][1] = 0;
			/*
				* 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;
			for (int k = 0; k < AAKERNEL_SIZE; k++){
				GzPutTriangle(m_pRender[k], 3, nameListTriangle, valueListTriangle);
			}
			//Second triangle of the quad
			vertexList[0][0] = SIZE * i; vertexList[0][1] = 0; vertexList[0][2] = SIZE * j;
			vertexList[1][0] = SIZE * i + SIZE; vertexList[1][1] = 0; vertexList[1][2] = SIZE * j;
			vertexList[2][0] = SIZE * i + SIZE; vertexList[2][1] = 0; vertexList[2][2] = SIZE * j + SIZE;

			normalList[0][0] = 0; normalList[0][1] = 1; normalList[0][2] = 0;
			normalList[1][0] = 0; normalList[1][1] = 1; normalList[1][2] = 0;
			normalList[2][0] = 0; normalList[2][1] = 1; normalList[2][2] = 0;

			uvList[0][0] = 0; uvList[0][1] = 1;
			uvList[1][0] = 1; uvList[1][1] = 1;
			uvList[2][0] = 1; uvList[2][1] = 0;
			/*
			* 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;
			for (int k = 0; k < AAKERNEL_SIZE; k++){
				GzPutTriangle(m_pRender[k], 3, nameListTriangle, valueListTriangle);
			}
		}
	}

	//Combine the displays
	for (int i = 0; i < AAKERNEL_SIZE; i++){
		if (!m_pDisplay[i]){
			return GZ_FAILURE;
		}
	}
	if (!m_pFinalDisplay){
		return GZ_FAILURE;
	}
	for (int i = 0; i < m_pFinalDisplay->xres; i++){
		for (int j = 0; j < m_pFinalDisplay->yres; j++){
			int index = (i + (j * m_pFinalDisplay->xres));
			m_pFinalDisplay->fbuf[index].red = 0;
			m_pFinalDisplay->fbuf[index].green = 0;
			m_pFinalDisplay->fbuf[index].blue = 0;
			m_pFinalDisplay->fbuf[index].alpha = 0;
			m_pFinalDisplay->fbuf[index].z = 0;
			for (int k = 0; k < AAKERNEL_SIZE; k++){
				m_pFinalDisplay->fbuf[index].red += m_pDisplay[k]->fbuf[index].red * AAFilter[k][WEIGHT];
				m_pFinalDisplay->fbuf[index].green += m_pDisplay[k]->fbuf[index].green * AAFilter[k][WEIGHT];
				m_pFinalDisplay->fbuf[index].blue += m_pDisplay[k]->fbuf[index].blue * AAFilter[k][WEIGHT];
				m_pFinalDisplay->fbuf[index].alpha += m_pDisplay[k]->fbuf[index].alpha * AAFilter[k][WEIGHT];
				m_pFinalDisplay->fbuf[index].z += m_pDisplay[k]->fbuf[index].z * AAFilter[k][WEIGHT];
			}
		}
	}

	GzFlushDisplay2File(outfile, m_pFinalDisplay); 	/* write out or update display to file*/
	GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_pFinalDisplay);	// 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); 
}
示例#13
0
文件: rend.cpp 项目: mayang/cs580
int GzBeginRender(GzRender *render)
{
/*  
- set up for start of each frame - clear frame buffer 
- compute Xiw and projection xform Xpi from camera definition 
- init Ximage - put Xsp at base of stack, push on Xpi and Xiw 
- now stack contains Xsw and app can push model Xforms if it want to. 
*/ 
	if (render == NULL) {
		return GZ_FAILURE;
	}

	// clear frame buffer
	GzInitDisplay(render->display);
	if (render->display == NULL) {
		return GZ_FAILURE;
	}

	// computer Xiw
	// camera z axis
	GzCoord cl, camZ;
	cl[X] = render->camera.lookat[X] - render->camera.position[X];
	cl[Y] = render->camera.lookat[Y] - render->camera.position[Y];
	cl[Z] = render->camera.lookat[Z] - render->camera.position[Z];
	normalizeVector(cl);
	camZ[X] = cl[X];
	camZ[Y] = cl[Y];
	camZ[Z] = cl[Z];
	normalizeVector(camZ);
	
	// camera y axis
	GzCoord camUp, camY;
	float upDotZ = render->camera.worldup[X]*camZ[X] + render->camera.worldup[Y]*camZ[Y] +
		render->camera.worldup[Z]*camZ[Z];
	camUp[X] = render->camera.worldup[X] - upDotZ*camZ[X];
	camUp[Y] = render->camera.worldup[Y] - upDotZ*camZ[Y];
	camUp[Z] = render->camera.worldup[Z] - upDotZ*camZ[Z];
	normalizeVector(camUp);
	camY[X] = camUp[X];
	camY[Y] = camUp[Y];
	camY[Z] = camUp[Z];
	normalizeVector(camY);

	// camera x axis
	GzCoord camX;
	camX[X] = camY[Y]*camZ[Z] - camY[Z]*camZ[Y];
	camX[Y] = camY[Z]*camZ[X] - camY[X]*camZ[Z];
	camX[Z] = camY[X]*camZ[Y] - camY[Y]*camZ[X];
	normalizeVector(camX);

	// Build Xiw
	render->camera.Xiw[0][0] = camX[X];
	render->camera.Xiw[0][1] = camX[Y];
	render->camera.Xiw[0][2] = camX[Z];
	render->camera.Xiw[0][3] = -(camX[X]*render->camera.position[X] 
								+ camX[Y]*render->camera.position[Y]
								+ camX[Z]*render->camera.position[Z]);
	render->camera.Xiw[1][0] = camY[X];
	render->camera.Xiw[1][1] = camY[Y];
	render->camera.Xiw[1][2] = camY[Z];
	render->camera.Xiw[1][3] = //2.98e-07; // TEMP ERROR IN CALCULATION
							-(camY[X]*render->camera.position[X] 
							+ camY[Y]*render->camera.position[Y]
							+ camY[Z]*render->camera.position[Z]); // WHY IS THIS WRONG OTHER 2 ARE FINE
	render->camera.Xiw[2][0] = camZ[X];
	render->camera.Xiw[2][1] = camZ[Y];
	render->camera.Xiw[2][2] = camZ[Z];
	render->camera.Xiw[2][3] = -(camZ[X]*render->camera.position[X]
								+ camZ[Y]*render->camera.position[Y]
								+ camZ[Z]*render->camera.position[Z]);
	render->camera.Xiw[3][0] = render->camera.Xiw[3][1] = render->camera.Xiw[3][2] = 0.0;
	render->camera.Xiw[3][3] = 1.0;

	// Build xpi
	float rads = (render->camera.FOV / 2.0) * (PI / 180.0);
	//float d = 1/ tan(rads);
	render->camera.Xpi[0][0] = render->camera.Xpi[1][1] = 
		render->camera.Xpi[2][2] = render->camera.Xpi[3][3] = 1.0;
	render->camera.Xpi[0][1] = render->camera.Xpi[0][2] = render->camera.Xpi[0][3] =
		render->camera.Xpi[1][0] = render->camera.Xpi[1][2] = render->camera.Xpi[1][3] =
		render->camera.Xpi[2][0] = render->camera.Xpi[2][1] = render->camera.Xpi[2][3] = 
		render->camera.Xpi[3][0] = render->camera.Xpi[3][1] = 0.0;
	render->camera.Xpi[3][2] = tan(rads);

	// init Ximage
	//render->Ximage[render->matlevel] = render->Xsp;
	// push Xsp
	render->matlevel=-1;
	GzPushMatrix(render, render->Xsp);
	// push Xpi
	GzPushMatrix(render, render->camera.Xpi);
	// push Xiw
	GzPushMatrix(render, render->camera.Xiw);

	render->open = 1; // I don't even know what this does

	return GZ_SUCCESS;
}
示例#14
0
int GzBeginRender(GzRender *render)
{
/*  
- set up for start of each frame - clear frame buffer 
- compute Xiw and projection xform Xpi from camera definition 
- init Ximage - put Xsp at base of stack, push on Xpi and Xiw 
- now stack contains Xsw and app can push model Xforms if it want to. 
*/ 
	if (render == NULL){
		return GZ_FAILURE;
	}

	render->open = 1;
	free(render->display->fbuf);
	render->display->fbuf = (GzPixel *)malloc(sizeof(GzPixel)* (render->display)->xres * (render->display)->yres);
	GzInitDisplay(render->display);

	GzMatrix Xpi,Xiw;
	float d;
	d = tan(((render->camera.FOV) / 2) * (3.14159265 / 180));
	
	for (int i = 0; i < 4; i++){
		for (int j = 0; j < 4; j++){
			if (i == j){
				Xpi[i][j] = 1;
			}
			else
				Xpi[i][j] = 0;
		}
	}
	Xpi[2][3] = 0;
	Xpi[3][2] = d;

	GzCoord cl;
	cl[X] = render->camera.lookat[X] - render->camera.position[X];
	cl[Y] = render->camera.lookat[Y] - render->camera.position[Y];
	cl[Z] = render->camera.lookat[Z] - render->camera.position[Z];

	float mod_cl;

	mod_cl = sqrt((cl[X] * cl[X]) + (cl[Y] * cl[Y]) + (cl[Z] * cl[Z]));

	GzCoord camera_z_axis;

	camera_z_axis[X] = cl[X] / mod_cl;
	camera_z_axis[Y] = cl[Y] / mod_cl;
	camera_z_axis[Z] = cl[Z] / mod_cl;

	GzCoord up, camera_y_axis;
	float dot_up_z = (((render->camera.worldup[X]) * (camera_z_axis[X])) + ((render->camera.worldup[Y]) * (camera_z_axis[Y])) + ((render->camera.worldup[Z]) * (camera_z_axis[Z])));
	up[X] = (render->camera.worldup[X]) - (dot_up_z * (camera_z_axis[X]));
	up[Y] = (render->camera.worldup[Y]) - (dot_up_z * (camera_z_axis[Y]));
	up[Z] = (render->camera.worldup[Z]) - (dot_up_z * (camera_z_axis[Z]));

	float mod_up;
	mod_up = sqrt((up[X] * up[X]) + (up[Y] * up[Y]) + (up[Z] * up[Z]));
	
	camera_y_axis[X] = up[X] / mod_up;
	camera_y_axis[Y] = up[Y] / mod_up;
	camera_y_axis[Z] = up[Z] / mod_up;

	GzCoord camera_x_axis;

	camera_x_axis[X] = (camera_y_axis[Y] * camera_z_axis[Z]) - (camera_y_axis[Z] * camera_z_axis[Y]);
	camera_x_axis[Y] = (camera_y_axis[Z] * camera_z_axis[X]) - (camera_y_axis[X] * camera_z_axis[Z]);
	camera_x_axis[Z] = (camera_y_axis[X] * camera_z_axis[Y]) - (camera_y_axis[Y] * camera_z_axis[X]);

	float xc, yc, zc;
	xc = (camera_x_axis[X] * render->camera.position[X]) + (camera_x_axis[Y] * render->camera.position[Y]) + (camera_x_axis[Z] * render->camera.position[Z]);
	yc = (camera_y_axis[X] * render->camera.position[X]) + (camera_y_axis[Y] * render->camera.position[Y]) + (camera_y_axis[Z] * render->camera.position[Z]);
	zc = (camera_z_axis[X] * render->camera.position[X]) + (camera_z_axis[Y] * render->camera.position[Y]) + (camera_z_axis[Z] * render->camera.position[Z]);
	
	Xiw[0][0] = camera_x_axis[X];
	Xiw[0][1] = camera_x_axis[Y];
	Xiw[0][2] = camera_x_axis[Z];
	Xiw[0][3] = -(xc);
	Xiw[1][0] = camera_y_axis[X];
	Xiw[1][1] = camera_y_axis[Y];
	Xiw[1][2] = camera_y_axis[Z];
	Xiw[1][3] = -(yc);
	Xiw[2][0] = camera_z_axis[X];
	Xiw[2][1] = camera_z_axis[Y];
	Xiw[2][2] = camera_z_axis[Z];
	Xiw[2][3] = -(zc);
	Xiw[3][0] = 0;
	Xiw[3][1] = 0; 
	Xiw[3][2] = 0;
	Xiw[3][3] = 1;

	for (int i = 0; i < 4; i++){
		for (int j = 0; j < 4; j++){

			render->camera.Xpi[i][j] = Xpi[i][j];
			render->camera.Xiw[i][j] = Xiw[i][j];
			
		}
	}
	

	GzPushMatrix(render,render->Xsp);
	GzPushMatrix(render, render->camera.Xpi);
	GzPushMatrix(render, render->camera.Xiw);

	return GZ_SUCCESS;
}
示例#15
0
void CCS580HWView::OnAnimation()
{
	// TODO: Add your command handler code here

	GzCamera	camera;
	GzToken		nameListTriangle[3]; 	/* vertex attribute names */
	GzPointer	valueListTriangle[3]; 	/* vertex attribute pointers */
	GzToken		nameListColor[3];		/* color type names */
	GzPointer	valueListColor[3];	/* color type rgb pointers */
	GzColor		color;
	GzCoord		vertexList[3];	/* vertex position coordinates */
	GzCoord		normalList[3];	/* vertex normals */
	GzTextureIndex  	uvList[3];		/* vertex texture map indices */
	char		dummy[256];
	int			i, j;
	int			xRes, yRes, dispClass;	/* display parameters */
	int			status;

	
	nameListTriangle[0] = GZ_POSITION;

	/*
	* Walk through the list of triangles, set color
	* and render each triangle
	*/
	FILE *outfile;
	FILE *infile;
	for (; m_pApplication->m_pRender->anilevelnow <= m_pApplication->m_pRender->anilevel; m_pApplication->m_pRender->anilevelnow++){
		GzInitDisplay(m_pApplication->m_pDisplay);
		if ((infile = fopen("pot4.asc", "r")) == NULL)
		{
			AfxMessageBox(_T("The input file was not opened\n"));
		}
		//_cprintf("%s#%d\n", "CS580HWView", m_pApplication->m_pRender->anilevelnow);
		if ((outfile = fopen(("animation/output" + std::to_string(m_pApplication->m_pRender->anilevelnow) + ".ppm").c_str(), "wb")) == NULL)
		{
			AfxMessageBox(_T("The output file was not opened\n"));
		}
		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]));


			//		GzPutAttribute(m_pRender, 1, nameListColor, valueListColor);
			shadeForAnimation(normalList[0], color);/* shade based on the norm of vert0 */
			valueListColor[0] = (GzPointer)color;
			nameListColor[0] = GZ_RGB_COLOR;
			GzPutAttribute(m_pApplication->m_pRender, 1, nameListColor, valueListColor);
			/*
			* Set the value pointers to the first vertex of the
			* triangle, then feed it to the renderer
			*/
			valueListTriangle[0] = (GzPointer)vertexList;
			AnimationPutTriangle(m_pApplication->m_pRender, 1, nameListTriangle, valueListTriangle);
		}
		//_cprintf("%s\n", "write to file");
		GzFlushDisplay2File(outfile, m_pApplication->m_pDisplay); 	/* write out or update display to file*/
		if (fclose(outfile))
			AfxMessageBox(_T("The output file was not closed\n")); 
		if (fclose(infile))
			AfxMessageBox(_T("The input file was not closed\n"));
	}
	AfxMessageBox(_T("YAY! It's done! please check the animation folder! :)\n"));
		
}
int Application4::Render() 
{
	GzToken		nameListTriangle[3]; 	/* vertex attribute names */
	GzPointer	valueListTriangle[3]; 	/* vertex attribute pointers */
	GzCoord		vertexList[3], tempVertexList[3];	/* vertex position coordinates */ 
	GzCoord		normalList[3], tempNormalList[3];	/* vertex normals */ 
	GzTextureIndex  	uvList[3], tempUVList[3];		/* vertex texture map indices */ 
	char		dummy[256]; 
	int			status; 
	int			index = 0, x = 0, y = 0, disp_i = 0;
	int		    xRes, yRes, dispClass;
	int i = 0, j = 0;

// ****************************************************************************************
//								START	HW6 CHANGE
// ****************************************************************************************
	/* Initialize Display */
	for(index = 0; index < AAKERNEL_SIZE; index++){
		status |= GzInitDisplay(m_pDisplay[index]); 
	}
// ****************************************************************************************
//								END		HW6 CHANGE
// ****************************************************************************************	
	/* 
	* 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]) ); 

		for(i = 0; i < 3; i++)
			for(j = 0; j < 3; j++)
				tempVertexList[i][j] = vertexList[i][j];

		for(i = 0; i < 3; i++)
			for(j = 0; j < 3; j++)
				tempNormalList[i][j] = normalList[i][j];

		for(i = 0; i < 3; i++)
			for(j = 0; j < 2; j++)
				tempUVList[i][j] = uvList[i][j];


	    /* 
	     * 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
	     */ 
// ****************************************************************************************
//								START		HW6 CHANGE
// ****************************************************************************************
		for(index = 0; index < AAKERNEL_SIZE; index++)
		{				 
			for(i = 0; i < 3; i++)
				for(j = 0; j < 3; j++)
					vertexList[i][j] = tempVertexList[i][j];

			for(i = 0; i < 3; i++)
				for(j = 0; j < 3; j++)
					 normalList[i][j] = tempNormalList[i][j];

			for(i = 0; i < 3; i++)
				for(j = 0; j < 2; j++)
					 uvList[i][j] = tempUVList[i][j];

			valueListTriangle[0] = (GzPointer)vertexList; 
			valueListTriangle[1] = (GzPointer)normalList; 
			valueListTriangle[2] = (GzPointer)uvList; 

			GzPutTriangle(m_pRender[index], 3, nameListTriangle, valueListTriangle); 
		}
// ****************************************************************************************
//								END			HW6 CHANGE
// ****************************************************************************************
	} 

// ****************************************************************************************
//								START	HW6 CHANGE
// ****************************************************************************************
	
	// Create The final display for rendering it into the framebuffer
	status |= GzNewDisplay(&m_pFinalDisplay, GZ_RGBAZ_DISPLAY, m_nWidth, m_nHeight);
	status |= GzGetDisplayParams(m_pFinalDisplay, &xRes, &yRes, &dispClass);  
	status |= GzInitDisplay(m_pFinalDisplay); 
	
	GzColor finalColor;
	for(y = 0; y <= RESOLUTIONY; y++){
		for(x = 0; x <= RESOLUTIONX; x++){
			disp_i = (x+(y*256));
			finalColor[RED]=0;
			finalColor[GREEN]=0;
			finalColor[BLUE]=0;
			for(index = 0; index < AAKERNEL_SIZE; index++){
			 
				finalColor[RED] += ((float)m_pRender[index]->display->fbuf[disp_i].red * AAFilter[index][2]);
				finalColor[GREEN] += ((float)m_pRender[index]->display->fbuf[disp_i].green * AAFilter[index][2]);
				finalColor[BLUE] += ((float)m_pRender[index]->display->fbuf[disp_i].blue * AAFilter[index][2]);
			
			}
			m_pFinalDisplay->fbuf[disp_i].red = (short)finalColor[RED];
			m_pFinalDisplay->fbuf[disp_i].green = (short)finalColor[GREEN];
			m_pFinalDisplay->fbuf[disp_i].blue = (short)finalColor[BLUE];
		}
	}
/*	
	// update the renderers with the sample weights
	for(index = 0; index < 1; index++){
		for(y = 0; y <= RESOLUTIONY; y++){
			for(x = 0; x <= RESOLUTIONX; x++){
				disp_i = (x+(y*256));
				m_pRender[index]->display->fbuf[disp_i].red *= AAFilter[index][2];
				m_pRender[index]->display->fbuf[disp_i].green *= AAFilter[index][2];
				m_pRender[index]->display->fbuf[disp_i].blue *= AAFilter[index][2];
			}
		}
	}

	// Compute the final pixel colors to render in the framebuffer
	for(index = 0; index < 1; index++){
		for(y = 0; y <= RESOLUTIONY; y++){
			for(x = 0; x <= RESOLUTIONX; x++){
				disp_i = (x+(y*256));
				m_pFinalDisplay->fbuf[disp_i].red += m_pRender[index]->display->fbuf[disp_i].red;
				m_pFinalDisplay->fbuf[disp_i].green += m_pRender[index]->display->fbuf[disp_i].green;
				m_pFinalDisplay->fbuf[disp_i].blue += m_pRender[index]->display->fbuf[disp_i].blue;
			}
		}
	}
*/
// ****************************************************************************************
//								END		HW6 CHANGE
// ****************************************************************************************

	
	GzFlushDisplay2File(outfile, m_pFinalDisplay); 	/* write out or update display to file*/
	GzFlushDisplay2FrameBuffer(m_pFrameBuffer, m_pFinalDisplay);	// 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 Application4::Initialize()
{
	GzCamera	camera;  
	int		    xRes = 0, yRes = 0, dispClass;	/* display parameters */ 

	GzToken		nameListShader[9]; 	    /* shader attribute names */
	GzPointer   valueListShader[9];		/* shader attribute pointers */
	GzToken     nameListLights[10];		/* light info */
	GzPointer   valueListLights[10];
	GzToken     nameListShifts[10];		/* Shift info */
	GzPointer   valueListShifts[10];

	int			shaderType, interpStyle;
	float		specpower;
	int			status; 
	int			index = 0;
 
	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
	
	/* 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 
	}; 

		/* 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};

#if 0 	/* 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 

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

// ****************************************************************************************
//								START	HW6 CHANGE
// ****************************************************************************************
	// Create multiple displays and framebuffers(display framebuffers)
	for(index = 0; index < AAKERNEL_SIZE; index++){
		status |= GzNewDisplay(&m_pDisplay[index], GZ_RGBAZ_DISPLAY, m_nWidth, m_nHeight);
		status |= GzGetDisplayParams(m_pDisplay[index], &xRes, &yRes, &dispClass);  
		status |= GzInitDisplay(m_pDisplay[index]); 
		status |= GzNewRender(&m_pRender[index], GZ_Z_BUFFER_RENDER, m_pDisplay[index]); 

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

		/*
		 * 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;

		// store the Directional Light values in the render structures
		status |= GzPutAttribute(m_pRender[index], 3, nameListLights, valueListLights);

		nameListLights[0] = GZ_AMBIENT_LIGHT;
		valueListLights[0] = (GzPointer)&ambientlight;
		
		// store the Ambient Light values in the render structures
		status |= GzPutAttribute(m_pRender[index], 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;
	#if 0
			interpStyle = GZ_COLOR;         /* Gourand shading */
	#else
			interpStyle = GZ_NORMALS;         /* Phong shading */
	#endif

		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 1   /* set up null texture function or valid pointer */
		valueListShader[5] = (GzPointer)0;
	#else
		valueListShader[5] = (GzPointer)(tex_fun);	/* or use ptex_fun */
	#endif

		// store the Ambient shading values in the render structures
		status |= GzPutAttribute(m_pRender[index], 6, nameListShader, valueListShader);
		

		// Pass the Sample offset X for the renderer defined for handling that jittered sample
		nameListShifts[0]  = GZ_AASHIFTX;
		valueListShifts[0] = (GzPointer)&AAFilter[index][X];
		status |= GzPutAttribute(m_pRender[index], 1, nameListShifts, valueListShifts);
		
		// Pass the Sample offset Y for the renderer defined for handling that jittered sample
		nameListShifts[0]  = GZ_AASHIFTY;
		valueListShifts[0] = (GzPointer)&AAFilter[index][Y];
		status |= GzPutAttribute(m_pRender[index], 1, nameListShifts, valueListShifts);
		
		// Push the transformation matrices into all the renderer stacks
		status |= GzPushMatrix(m_pRender[index], scale);  
		status |= GzPushMatrix(m_pRender[index], rotateY); 
		status |= GzPushMatrix(m_pRender[index], rotateX); 
	}
// ****************************************************************************************
//								END		HW6 CHANGE
// ****************************************************************************************
	if (status) exit(GZ_FAILURE); 

	if (status) 
		return(GZ_FAILURE); 
	else 
		return(GZ_SUCCESS); 
}
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
	
	//initialize the final display
	status |= GzNewFrameBuffer(&m_pFrameBuffer, m_nWidth, m_nHeight);

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

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

/* 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 
}; 

	m_pRender = new GzRender* [AAKERNEL_SIZE * sizeof(GzRender*)];
	m_pDisplay = new GzDisplay* [AAKERNEL_SIZE * sizeof(GzDisplay*)];
	
	//initialize the seperated display and renderer for different offsets
	for (int i = 0; i < AAKERNEL_SIZE; i++)
	{
		status |= GzNewDisplay(&m_pDisplay[i], GZ_RGBAZ_DISPLAY, m_nWidth, m_nHeight);

		status |= GzGetDisplayParams(m_pDisplay[i], &xRes, &yRes, &dispClass); 
	 
		status |= GzInitDisplay(m_pDisplay[i]); 
 
		status |= GzNewRender(&m_pRender[i], GZ_Z_BUFFER_RENDER, m_pDisplay[i]);
#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[i], &camera); 
#endif 

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

		/* 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[i], 3, nameListLights, valueListLights);

        nameListLights[0] = GZ_AMBIENT_LIGHT;
        valueListLights[0] = (GzPointer)&ambientlight;
        status |= GzPutAttribute(m_pRender[i], 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 */
		//interpStyle = GZ_COLOR; //gourand 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);	/* use tex_fun */
		//valueListShader[5] = (GzPointer)(ptex_fun);	// use ptex_fun
#endif
        nameListShader[6]  = GZ_AASHIFTX;
        valueListShader[6] = (GzPointer)&(AAFilter[i][X]);
		nameListShader[7]  = GZ_AASHIFTY;
        valueListShader[7] = (GzPointer)&(AAFilter[i][Y]);

		status |= GzPutAttribute(m_pRender[i], 8, nameListShader, valueListShader);


		status |= GzPushMatrix(m_pRender[i], scale);  
		status |= GzPushMatrix(m_pRender[i], rotateY); 
		status |= GzPushMatrix(m_pRender[i], rotateX); 
	}

	if (status) exit(GZ_FAILURE); 

	if (status) 
		return(GZ_FAILURE); 
	else 
		return(GZ_SUCCESS); 
}
int Application2::Render() 
{
	GzToken		nameListTriangle[3]; 	/* vertex attribute names */
	GzPointer	valueListTriangle[3]; 		/* vertex attribute pointers */
	GzToken         nameListColor[3];       /* color type names */
	GzPointer       valueListColor[3];      /* color type rgb pointers */
	GzColor		color; 
	GzCoord		vertexList[3];	/* vertex position coordinates */ 
	GzCoord		normalList[3];	/* vertex normals */ 
	GzTextureIndex	uvList[3];		/* vertex texture map indices */ 
	char		dummy[256]; 
	int		i; 
	int		xRes, yRes, dispClass;	/* display parameters */ 
	int		status; 
 
	status = 0; 

	/* 
	 * 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); 

	status |= GzBeginRender(m_pRender); 

	if (status) exit(GZ_FAILURE); 

	/* 
	 * Tokens associated with triangle vertex values 
	 */ 
	nameListTriangle[0] = GZ_POSITION; /* define vert coordinates only */

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

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

	/* 
	* Walk through the list of triangles, set color 
	* and pass vert info to render/scan convert each triangle 
	*/ 
	i = 0; 

	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 up shading attributes for each triangle 
	    */ 
	    shade2(normalList[0], color);/* shade based on the norm of vert0 */
	    valueListColor[0] = (GzPointer)color; 
	    nameListColor[0] = GZ_RGB_COLOR; 
	    GzPutAttribute(m_pRender, 1, nameListColor, valueListColor); 
 
	    /* 
	     * Set the value pointers to the first vertex of the 	
	     * triangle, then feed it to the renderer 
	     */ 
	     valueListTriangle[0] = (GzPointer)vertexList; 


	     GzPutTriangle(m_pRender, 1, 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
 
	/* 
	 * Clean up and exit 
	 */ 

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

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

	status |= GzFreeRender(m_pRender); 
	status |= GzFreeDisplay(m_pDisplay); 
 
	if (status) 
		return(GZ_FAILURE); 
	else 
		return(GZ_SUCCESS); 
} 
int Application3::Initialize()
{
	/* to be filled in by the app if it sets camera params */

	GzCamera	camera;
	int		i, j;
	int		xRes, yRes, dispClass;	/* display parameters */
	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

	/* 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
	};

	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);

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

	camera.lookat[X] = 0.8;
	camera.lookat[Y] = 0.7;
	camera.lookat[Z] = 4.5;

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

	camera.FOV = 53.7;              /* degrees */

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

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

	status |= GzPushMatrix(m_pRender, scale);
	status |= GzPushMatrix(m_pRender, rotateY);
	status |= GzPushMatrix(m_pRender, rotateX);

	if (status)
		return(GZ_FAILURE);
	else
		return(GZ_SUCCESS);
}
示例#21
0
int GzBeginRender(GzRender	*render)
{
	/*
	- set up for start of each frame - init frame buffer
	*/
	if (NULL == render)
		return GZ_FAILURE;
	GzInitDisplay(render->display);


	//render->flatcolor[RED] = 0;
	//render->flatcolor[GREEN] = 0;
	//render->flatcolor[BLUE] = 0;

	/* perspective projection xform  camera --> NDC*/
	/*GzMatrix Xpi = {
	1.0, 0.0, 0.0, 0.0,
	0.0, 1.0, 0.0, 0.0,
	0.0, 0.0, 1.0 / d, 0.0,
	0.0, 0.0, 1.0 / d, 1.0
	};*/

	InitGzMatrix(render->camera.Xpi);
	render->camera.Xpi[2][2] = 1.0 / (1.0 / tan(DEGREE_TO_RADIAN(render->camera.FOV / 2.0)));  //  1 /d
	render->camera.Xpi[3][2] = 1.0 / (1.0 / tan(DEGREE_TO_RADIAN(render->camera.FOV / 2.0)));  //  1 /d


	/*GzMatrix Xiw = {
	Xx    Xy     Xz      -X * C
	Yx    Yy     Yz      -Y * C
	Zx    Zy     Zz      -Z * C
	0     0      0        1       */
	InitGzMatrix(render->camera.Xiw);

	float norm;
	GzCoord Camera_X, Camera_Y, Camera_Z;

	//Camera_Z
	Camera_Z[X] = render->camera.lookat[X] - render->camera.position[X];
	Camera_Z[Y] = render->camera.lookat[Y] - render->camera.position[Y];
	Camera_Z[Z] = render->camera.lookat[Z] - render->camera.position[Z];

	NormalizeVector(Camera_Z);
	NormalizeVector(render->camera.worldup);

	//Camera_Y  
	// UP' = UP - (UP * Z) Z
	float sum_zaxis = (render->camera.worldup[X]) * Camera_Z[X] + (render->camera.worldup[Y]) * Camera_Z[Y] + (render->camera.worldup[Z]) * Camera_Z[Z];
	Camera_Y[X] = render->camera.worldup[X] - sum_zaxis * Camera_Z[X];
	Camera_Y[Y] = render->camera.worldup[Y] - sum_zaxis * Camera_Z[Y];
	Camera_Y[Z] = render->camera.worldup[Z] - sum_zaxis * Camera_Z[Z];

	NormalizeVector(Camera_Y);


	//Camera_X
	Camera_X[X] = Camera_Y[Y] * Camera_Z[Z] - Camera_Y[Z] * Camera_Z[Y];
	Camera_X[Y] = Camera_Y[Z] * Camera_Z[X] - Camera_Y[X] * Camera_Z[Z];
	Camera_X[Z] = Camera_Y[X] * Camera_Z[Y] - Camera_Y[Y] * Camera_Z[X];

	render->camera.Xiw[0][0] = Camera_X[X];
	render->camera.Xiw[0][1] = Camera_X[Y];
	render->camera.Xiw[0][2] = Camera_X[Z];
	render->camera.Xiw[0][3] = -(Camera_X[X] * render->camera.position[X] + Camera_X[Y] * render->camera.position[Y] + Camera_X[Z] * render->camera.position[Z]);
	render->camera.Xiw[1][0] = Camera_Y[X];
	render->camera.Xiw[1][1] = Camera_Y[Y];
	render->camera.Xiw[1][2] = Camera_Y[Z];
	render->camera.Xiw[1][3] = -(Camera_Y[X] * render->camera.position[X] + Camera_Y[Y] * render->camera.position[Y] + Camera_Y[Z] * render->camera.position[Z]);
	render->camera.Xiw[2][0] = Camera_Z[X];
	render->camera.Xiw[2][1] = Camera_Z[Y];
	render->camera.Xiw[2][2] = Camera_Z[Z];
	render->camera.Xiw[2][3] = -(Camera_Z[X] * render->camera.position[X] + Camera_Z[Y] * render->camera.position[Y] + Camera_Z[Z] * render->camera.position[Z]);

	GzPushMatrix(render, render->Xsp);            //render->matlevel[1]
	GzPushMatrix(render, render->camera.Xpi);	  //render->matlevel[2]
	GzPushMatrix(render, render->camera.Xiw);	  //render->matlevel[3]


	return GZ_SUCCESS;
}