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; }
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 == nullptr) { return GZ_FAILURE; } // Open the renderer render->open = 1; render->flatcolor[RED] = 127; render->flatcolor[GREEN] = 127; render->flatcolor[BLUE] = 127; // Initialize stack to be empty render->matlevel = -1; //Build Xsp Matrix and push onto stack GzBuildXsp(render); if(render->Xsp == nullptr) { return GZ_FAILURE; } GzPushMatrix(render,render->Xsp); //Build Xpi Matrix and push onto stack GzBuildXpi(render); if(render->camera.Xpi == nullptr) { return GZ_FAILURE; } GzPushMatrix(render,render->camera.Xpi); //Build Xiw Matrix and push onto stack GzBuildXiw(render); if(render->camera.Xiw == nullptr) { return GZ_FAILURE; } GzPushMatrix(render,render->camera.Xiw); //Open the renderer render->open = 1; return GZ_SUCCESS; }
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 */ render->flatcolor[RED] = 0; render->flatcolor[GREEN] = 0; render->flatcolor[BLUE] = 0; //calculate the CL GzCamera *camera = &(render->camera); for (int nCntRow = 0; nCntRow < 4; nCntRow++) { for (int nCntLine = 0; nCntLine < 4; nCntLine++) { camera->Xpi[nCntRow][nCntLine] = 0; camera->Xiw[nCntRow][nCntLine] = 0; render->Xsp[nCntRow][nCntLine] = 0; } } float fovTemp = 3.14*camera->FOV / 180; float tempd = tan((fovTemp) / 2); camera->Xpi[0][0] = 1; camera->Xpi[1][1] = 1; camera->Xpi[2][2] = tempd; camera->Xpi[3][2] = tempd; camera->Xpi[3][3] = 1; float cl[3] = { camera->lookat[X] - camera->position[X], camera->lookat[Y] - camera->position[Y], camera->lookat[Z] - camera->position[Z] }; float clLength = sqrt(pow(cl[X], 2) + pow(cl[Y], 2) + pow(cl[Z], 2)); float vectorZ[3] = { cl[X] / clLength, cl[Y] / clLength, cl[Z] / clLength }; float productUPZ = vectorZ[X] * camera->worldup[X] + vectorZ[Y] * camera->worldup[Y] + vectorZ[Z] * camera->worldup[Z]; float upapo[3] = { camera->worldup[X] - productUPZ*vectorZ[X], camera->worldup[Y] - productUPZ*vectorZ[Y], camera->worldup[Z] - productUPZ*vectorZ[Z] }; float upapoLength = sqrt(pow(upapo[X], 2) + pow(upapo[Y], 2) + pow(upapo[Z], 2)); float vectorY[3] = { upapo[X] / upapoLength, upapo[Y] / upapoLength, upapo[Z] / upapoLength }; float vectorX[3] = { vectorY[Y] * vectorZ[Z] - vectorY[Z] * vectorZ[Y], vectorY[Z] * vectorZ[X] - vectorY[X] * vectorZ[Z], vectorY[X] * vectorZ[Y] - vectorY[Y] * vectorZ[X] }; camera->Xiw[0][0] = vectorX[0]; camera->Xiw[0][1] = vectorX[1]; camera->Xiw[0][2] = vectorX[2]; camera->Xiw[0][3] = -dotproduct(vectorX, camera->position); camera->Xiw[1][0] = vectorY[0]; camera->Xiw[1][1] = vectorY[1]; camera->Xiw[1][2] = vectorY[2]; camera->Xiw[1][3] = -dotproduct(vectorY, camera->position); camera->Xiw[2][0] = vectorZ[0]; camera->Xiw[2][1] = vectorZ[1]; camera->Xiw[2][2] = vectorZ[2]; camera->Xiw[2][3] = -dotproduct(vectorZ, camera->position); camera->Xiw[3][3] = 1; render->Xsp[0][0] = render->display->xres / 2; render->Xsp[0][3] = render->display->xres / 2; render->Xsp[1][1] = -render->display->yres / 2; render->Xsp[1][3] = render->display->yres / 2; render->Xsp[2][2] = INT_MAX;// *tempd; render->Xsp[3][3] = 1; GzPushMatrix(render, render->Xsp); // push Xpi GzPushMatrix(render, render->camera.Xpi); // push Xiw GzPushMatrix(render, render->camera.Xiw); return GZ_SUCCESS; }
static int render_teapot_refraction(GzRender* in_renderer) { //setup shader and teapot material in_renderer->v_shader = GouraudVertexShader; in_renderer->p_shader = GouraudAlphaPixelShader; //use the same material for ambient, diffuse and specular GzColor material_color = {0.7f, 0.3f, 0.1f}; for(int i=0; i<3; i++) { in_renderer->Ka[i] = material_color[i]; in_renderer->Kd[i] = material_color[i]; in_renderer->Ks[i] = material_color[i]; } in_renderer->spec = 32; //setup transform GzMatrix m; GzScaleMat(teapot_scale, m); GzPushMatrix(in_renderer, m); GzCoord pos = {teapot_position[0], teapot_position[1]/1.33f, teapot_position[2]}; GzTrxMat(pos, m); GzPushMatrix(in_renderer, m); GzTrxMat(teapot_rotation, m); GzPushMatrix(in_renderer, m); GzPutCamera(renderer, &default_camera); GzBeginRender(in_renderer); GzToken nameListTriangle[4]; /* vertex attribute names */ GzPointer valueListTriangle[4]; /* vertex attribute pointers */ nameListTriangle[0] = GZ_POSITION; nameListTriangle[1] = GZ_NORMAL; nameListTriangle[2] = GZ_TEXTURE_INDEX; nameListTriangle[3] = GZ_RGB_COLOR; const Model::TriangleVector& triangles = teapot_model.GetData(); for(Model::TriangleVector::const_iterator it = triangles.begin(); it != triangles.end(); it++) { valueListTriangle[0] = (GzPointer)(*it)->vertices; valueListTriangle[1] = (GzPointer)(*it)->normals; valueListTriangle[2] = (GzPointer)(*it)->uvs; GzPutTriangle(in_renderer, 3, nameListTriangle, valueListTriangle); } return GZ_SUCCESS; }
// Callback function for Scaling void CCS580HWView::OnScale() { // TODO: Add your command handler code here CScaleDlg dlg; GzInput* input; GzMatrix scaleMat = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; if(m_pApplication == NULL) return; input = m_pApplication->m_pUserInput; if(input == NULL) return; // Initialize input->scale[0] = input->scale[1] = input->scale[2] = 1; dlg.Initialize(input->scale[0], input->scale[1], input->scale[2]); if(dlg.DoModal() == IDOK) { // Update input scale value input->scale[0] = dlg.m_fSx; input->scale[1] = dlg.m_fSy; input->scale[2] = dlg.m_fSz; // Create Scaling Matrix GzScaleMat(input->scale, scaleMat); // Accumulate matrix GzPushMatrix(m_pApplication->m_pRender, scaleMat); } }
// Callback function for Translation void CCS580HWView::OnTranslate() { // TODO: Add your command handler code here CTranslateDlg dlg; GzInput* input; GzMatrix trxMat = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; if(m_pApplication == NULL) return; input = m_pApplication->m_pUserInput; if(input == NULL) return; // Initialize input->translation[0] = input->translation[1] = input->translation[2] = 0; dlg.Initialize(input->translation[0], input->translation[1], input->translation[2]); if(dlg.DoModal() == IDOK) { // Update input translation value input->translation[0] = dlg.m_fTx; input->translation[1] = dlg.m_fTy; input->translation[2] = dlg.m_fTz; // Create Translation Matrix GzTrxMat(input->translation, trxMat); // Accumulate matrix GzPushMatrix(m_pApplication->m_pRender, trxMat); } }
// Callback function for rotation void CCS580HWView::OnRotate() { // TODO: Add your command handler code here CRotateDlg dlg; GzInput* input; GzMatrix rotMat = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; if(m_pApplication == NULL) return; input = m_pApplication->m_pUserInput; if(input == NULL) return; // Initialize input->rotation[0] = input->rotation[1] = input->rotation[2] = 0; dlg.Initialize(input->rotation[0], input->rotation[1], input->rotation[2]); if(dlg.DoModal() == IDOK) { // Update input rotation value input->rotation[dlg.m_nAxis] = dlg.m_fRot; // Create Rotation Matrix switch(dlg.m_nAxis) { case 0 : // Create matrix for Rot X GzRotXMat(input->rotation[0], rotMat); break; case 1: // Create matrix for Rot Y GzRotYMat(input->rotation[1], rotMat); break; case 2: // Create matrix for Rot Z GzRotZMat(input->rotation[2], rotMat); break; } // Accumulate matrix GzPushMatrix(m_pApplication->m_pRender, rotMat); OnRender(); } }
//------------------------------------------------------------------------------ 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; }
static int render_water_plane(GzRender* in_renderer) { //setup shader and teapot material in_renderer->v_shader = PhongVertexShader; in_renderer->p_shader = PhongPixelShader; //use the same material for ambient, diffuse and specular GzColor material_color = {0.04f, 0.4f, 0.6f}; for(int i=0; i<3; i++) { in_renderer->Ka[i] = material_color[i]; in_renderer->Kd[i] = material_color[i]; in_renderer->Ks[i] = material_color[i]; } in_renderer->spec = 32; GzPutCamera(renderer, &default_camera); //setup transform GzCoord scale = {3.0f, 1.0f, 1.5f}; GzMatrix m; GzScaleMat(scale,m); GzPushMatrix(in_renderer, m); GzBeginRender(in_renderer); GzToken nameListTriangle[4]; /* vertex attribute names */ GzPointer valueListTriangle[4]; /* vertex attribute pointers */ nameListTriangle[0] = GZ_POSITION; nameListTriangle[1] = GZ_NORMAL; nameListTriangle[2] = GZ_TEXTURE_INDEX; nameListTriangle[3] = GZ_RGB_COLOR; const Model::TriangleVector& triangles = water_plane_model.GetData(); for(Model::TriangleVector::const_iterator it = triangles.begin(); it != triangles.end(); it++) { valueListTriangle[0] = (GzPointer)(*it)->vertices; valueListTriangle[1] = (GzPointer)(*it)->normals; valueListTriangle[2] = (GzPointer)(*it)->uvs; GzPutTriangle(in_renderer, 3, nameListTriangle, valueListTriangle); } return GZ_SUCCESS; }
//------------------------------------------------------------------------------ int GzPutCamera(GzRender *render, GzCamera *camera) { //Do sanity checking if(render == NULL || camera == NULL) { return GZ_FAILURE; } memcpy(&(render->camera), camera, sizeof(GzCamera)); //I must empty the stack if someone has called this method. //Xsp need to be at the bottom of the stack render->xStack.emptyStack(); //Prepare new Xsp using current user provided camera parameters prepareXsp(render->Xsp, render->display->xres, render->display->xres, render->camera.FOV); GzPushMatrix(render,render->Xsp, false); return GZ_SUCCESS; }
// I moved all transformations into Transformation.h and Transformation.cpp to // reduce clutter here in rend.cpp //---------------------------------------------------------- // Begin main functions int GzNewRender(GzRender **render, GzRenderClass renderClass, GzDisplay *display) { if(render == NULL || renderClass != GZ_Z_BUFFER_RENDER || display == NULL) { return GZ_FAILURE; } *render = new GzRender(); if(NULL == *render) { return GZ_FAILURE; } (*render)->open = 0;//i.e. render is closed. (*render)->renderClass = renderClass; (*render)->display = display; //Prepare Xsp using default camera parameters prepareXsp((*render)->Xsp,(*render)->display->xres, (*render)->display->xres, (*render)->camera.FOV); GzPushMatrix(*render, (*render)->Xsp, false); return GZ_SUCCESS; }
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; int index; // used to cycle through the framebuffer // Initializes values for background color for (int i = 0; i < render->display->xres; ++i) { for (int j = 0; j < render->display->yres; ++j) { index = (j*render->display->xres) + i; render->display->fbuf[index].blue = 4095 / 2; render->display->fbuf[index].green = 4095 / 2; render->display->fbuf[index].red = 4095 / 2; render->display->fbuf[index].alpha = 4095; render->display->fbuf[index].z = MAXINT; } } float d_inv = tan((render->camera.FOV / 2) * M_PI / 180.0); GzMatrix Xpi = // finds matrix based on fov { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, d_inv, 0.0, 0.0, 0.0, d_inv, 1.0 }; float xaxis[3]; float yaxis[3]; float zaxis[3]; float up[3]; // find z-axis of camera for (int i = 0; i < 3; ++i) zaxis[i] = render->camera.lookat[i] - render->camera.position[i]; normalize(zaxis); // find y-axis of camera for (int i = 0; i < 3; ++i) up[i] = dotProduct(render->camera.worldup, zaxis) * zaxis[i]; for (int i = 0; i < 3; ++i) yaxis[i] = render->camera.worldup[i] - up[i]; normalize(yaxis); // find x-axis of camera crossProduct(yaxis, zaxis, xaxis); // gets cross product for xaxis normalize(xaxis); GzMatrix Xiw = // finds matrix based on camera's coordinates { xaxis[X], xaxis[Y], xaxis[Z], -dotProduct(xaxis, render->camera.position), yaxis[X], yaxis[Y], yaxis[Z], -dotProduct(yaxis, render->camera.position), zaxis[X], zaxis[Y], zaxis[Z], -dotProduct(zaxis, render->camera.position), 0.0, 0.0, 0.0, 1.0 }; for (int i = 0; i < 4; ++i) { // copy the value into the camera for (int j = 0; j < 4; ++j) { render->camera.Xiw[j][i] = Xiw[j][i]; render->camera.Xpi[j][i] = Xpi[j][i]; } } GzPushMatrix(render, render->Xsp); GzPushMatrix(render, render->camera.Xpi); GzPushMatrix(render, render->camera.Xiw); return GZ_SUCCESS; }
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); }
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; }
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; }
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. */ /* Compoute Xiw */ GzCoord CamX, CamY, CamZ, C; // Calculate CamZ for (int i = 0; i < 3; i++) { C[i] = render->camera.position[i]; //CamY[i] = render->camera.worldup[i]; CamZ[i] = render->camera.lookat[i] - render->camera.position[i]; } float lenZ = length(CamZ); for (int i = 0; i < 3; i++) CamZ[i] = CamZ[i] / lenZ; // Calculate CamY float dotUpZ = dotProduct(render->camera.worldup, CamZ); for (int i = 0; i < 3; i++) { CamY[i] = render->camera.worldup[i] - dotUpZ * CamZ[i]; } float lenY = length(CamY); for (int i = 0; i < 3; i++) CamY[i] = CamY[i] / lenY; // Calculate CamX float *ptr = crossProduct(CamY, CamZ); for (int i = 0; i < 3; i++) CamX[i] = ptr[i]; float lenX = length(CamX); for (int i = 0; i < 3; i++) CamX[i] = CamX[i] / lenX; for (int i = 0; i < 3; i++) { render->camera.Xiw[0][i] = CamX[i]; render->camera.Xiw[1][i] = CamY[i]; render->camera.Xiw[2][i] = CamZ[i]; render->camera.Xiw[3][i] = 0; } render->camera.Xiw[0][3] = -(CamX[0] * C[0] + CamX[1] * C[1] + CamX[2] * C[2]); render->camera.Xiw[1][3] = -(CamY[0] * C[0] + CamY[1] * C[1] + CamY[2] * C[2]); render->camera.Xiw[2][3] = -(CamZ[0] * C[0] + CamZ[1] * C[1] + CamZ[2] * C[2]); render->camera.Xiw[3][3] = 1; /* Compute Xpi */ for (int i = 0; i < 4; i++) memset(render->camera.Xpi[i], 0, sizeof(float)*4); for (int i = 0; i < 4; i++) render->camera.Xpi[i][i] = 1; render->camera.Xpi[3][2] = tan((render->camera.FOV/2)*3.14159265/180); /* Init Ximage */ render->matlevel = 0; // initial position of the stack GzPushMatrix(render, render->Xsp); // push Xsp GzPushMatrix(render, render->camera.Xpi); // push Xpi GzPushMatrix(render, render->camera.Xiw); // push Xiw 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 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; }
int GzPutTriangle(GzRender *render, int numParts, GzToken *nameList, GzPointer *valueList) /* numParts : how many names and values */ { /* - pass in a triangle description with tokens and values corresponding to GZ_POSITION:3 vert positions in model space - Xform positions of verts - Clip - just discard any triangle with verts behind view plane // ALL OF THEM? or any one of them? - test for triangles with all three verts off-screen - invoke triangle rasterizer */ // error checking if (render == NULL) { return GZ_FAILURE; } if (nameList == NULL) { return GZ_FAILURE; } if (valueList == NULL) { return GZ_FAILURE; } // get Xsm if (render->matlevel > 0) { GzMatrix Xsm, temp; memcpy(Xsm, render->Ximage[0], sizeof(GzMatrix)); // copy first one //copyMatrix(Xsm, render->Ximage[0]); for (int i = 1; i <= render->matlevel; ++i) { memcpy(temp, Xsm, sizeof(GzMatrix)); //copyMatrix(temp, Xsm); Xsm[0][0] = temp[0][0]*render->Ximage[i][0][0] + temp[0][1]*render->Ximage[i][1][0] + temp[0][2]*render->Ximage[i][2][0] + temp[0][3]*render->Ximage[i][3][0]; Xsm[0][1] = temp[0][0]*render->Ximage[i][0][1] + temp[0][1]*render->Ximage[i][1][1] + temp[0][2]*render->Ximage[i][2][1] + temp[0][3]*render->Ximage[i][3][1]; Xsm[0][2] = temp[0][0]*render->Ximage[i][0][2] + temp[0][1]*render->Ximage[i][1][2] + temp[0][2]*render->Ximage[i][2][2] + temp[0][3]*render->Ximage[i][3][2]; Xsm[0][3] = temp[0][0]*render->Ximage[i][0][3] + temp[0][1]*render->Ximage[i][1][3] + temp[0][2]*render->Ximage[i][2][3] + temp[0][3]*render->Ximage[i][3][3]; ///// Xsm[1][0] = temp[1][0]*render->Ximage[i][0][0] + temp[1][1]*render->Ximage[i][1][0] + temp[1][2]*render->Ximage[i][2][0] + temp[1][3]*render->Ximage[i][3][0]; Xsm[1][1] = temp[1][0]*render->Ximage[i][0][1] + temp[1][1]*render->Ximage[i][1][1] + temp[1][2]*render->Ximage[i][2][1] + temp[1][3]*render->Ximage[i][3][1]; Xsm[1][2] = temp[1][0]*render->Ximage[i][0][2] + temp[1][1]*render->Ximage[i][1][2] + temp[1][2]*render->Ximage[i][2][2] + temp[1][3]*render->Ximage[i][3][2]; Xsm[1][3] = temp[1][0]*render->Ximage[i][0][3] + temp[1][1]*render->Ximage[i][1][3] + temp[1][2]*render->Ximage[i][2][3] + temp[1][3]*render->Ximage[i][3][3]; //////////// Xsm[2][0] = temp[2][0]*render->Ximage[i][0][0] + temp[2][1]*render->Ximage[i][1][0] + temp[2][2]*render->Ximage[i][2][0] + temp[2][3]*render->Ximage[i][3][0]; Xsm[2][1] = temp[2][0]*render->Ximage[i][0][1] + temp[2][1]*render->Ximage[i][1][1] + temp[2][2]*render->Ximage[i][2][1] + temp[2][3]*render->Ximage[i][3][1]; Xsm[2][2] = temp[2][0]*render->Ximage[i][0][2] + temp[2][1]*render->Ximage[i][1][2] + temp[2][2]*render->Ximage[i][2][2] + temp[2][3]*render->Ximage[i][3][2]; Xsm[2][3] = temp[2][0]*render->Ximage[i][0][3] + temp[2][1]*render->Ximage[i][1][3] + temp[2][2]*render->Ximage[i][2][3] + temp[2][3]*render->Ximage[i][3][3]; ///////////// Xsm[3][0] = temp[3][0]*render->Ximage[i][0][0] + temp[3][1]*render->Ximage[i][1][0] + temp[3][2]*render->Ximage[i][2][0] + temp[3][3]*render->Ximage[i][3][0]; Xsm[3][1] = temp[3][0]*render->Ximage[i][0][1] + temp[3][1]*render->Ximage[i][1][1] + temp[3][2]*render->Ximage[i][2][1] + temp[3][3]*render->Ximage[i][3][1]; Xsm[3][2] = temp[3][0]*render->Ximage[i][0][2] + temp[3][1]*render->Ximage[i][1][2] + temp[3][2]*render->Ximage[i][2][2] + temp[3][3]*render->Ximage[i][3][2]; Xsm[3][3] = temp[3][0]*render->Ximage[i][0][3] + temp[3][1]*render->Ximage[i][1][3] + temp[3][2]*render->Ximage[i][2][3] + temp[3][3]*render->Ximage[i][3][3]; } // POP EVERYTHING for (int i = render->matlevel; i >= 0; --i) { GzPopMatrix(render); } // push on Xsm GzPushMatrix(render, Xsm); } // Get vert positions for (int i = 0; i < numParts; ++i) { if (nameList[i] == GZ_POSITION) { // get points GzCoord* tri = (GzCoord*) valueList[i]; GzCoord* xformTri = new GzCoord[3]; bool behindVP = false; GzMatrix topMat; int top = render->matlevel; memcpy(topMat, render->Ximage[top], sizeof(GzMatrix)); //copyMatrix(topMat, render->xim float W; for (int j = 0; j < 3; ++j) { // xform verticies xformTri[j][X] = topMat[0][0]*tri[j][X] + topMat[0][1]*tri[j][Y] + topMat[0][2]*tri[j][Z] + topMat[0][3]*1.0; xformTri[j][Y] = topMat[1][0]*tri[j][X] + topMat[1][1]*tri[j][Y] + topMat[1][2]*tri[j][Z] + topMat[1][3]*1.0; xformTri[j][Z] = topMat[2][0]*tri[j][X] + topMat[2][1]*tri[j][Y] + topMat[2][2]*tri[j][Z] + topMat[2][3]*1.0; W = topMat[3][0]*tri[j][X] + topMat[3][1]*tri[j][Y] + topMat[3][2]*tri[j][Z] + topMat[3][3]*1; xformTri[j][X] /= W; xformTri[j][Y] /= W; xformTri[j][Z] /= W; // check if any vets in behind view plane if (xformTri[j][Z] < render->camera.position[Z]) { behindVP = true; break; } } // float x0 = tri[0][X]; // float x1 = tri[1][X]; // float x2 = tri[2][X]; // float tx0 = xformTri[0][X]; // float tx1 = xformTri[1][X]; // float tx2 = xformTri[2][X]; // float y0 = tri[0][Y]; // float y1 = tri[1][Y]; // float y2 = tri[2][Y]; // float ty0 = xformTri[0][Y]; // float ty1 = xformTri[1][Y]; // float ty2 = xformTri[2][Y]; // float z0 = tri[0][Z]; // float z1 = tri[1][Z]; // float z2 = tri[2][Z]; // float tz0 = xformTri[0][Z]; // float tz1 = xformTri[1][Z]; // float tz2 = xformTri[2][Z]; // this tri is in front of viewing plane if (behindVP == true) { break; } // CLIPPING //if (xformTri[0][Z] < render->camera.position[Z] // && xformTri[1][Z] < render->camera.position[Z] // && xformTri[2][Z] < render->camera.position[Z]) { // break; //} //if ((xformTri[0][X] < 0 && xformTri[1][ // check if all verts are in screen (think i handle this in my rasterizer) // RASTERIZE // sort by Y int minY = 0; for (int i = 0; i < 2; ++i) { minY = i; for (int j = i + 1; j < 3; ++j) { if (xformTri[minY][Y] > xformTri[j][Y]) { minY = j; } } // swapping if (minY != i) { float tempX = xformTri[i][X]; float tempY = xformTri[i][Y]; float tempZ = xformTri[i][Z]; xformTri[i][X] = xformTri[minY][X]; xformTri[i][Y] = xformTri[minY][Y]; xformTri[i][Z] = xformTri[minY][Z]; xformTri[minY][X] = tempX; xformTri[minY][Y] = tempY; xformTri[minY][Z] = tempZ; } } // Get bounding box int topY = floor(xformTri[0][Y]); int bottomY = ceil(xformTri[2][Y]); int leftX, rightX; // 0 comes before 1 if (xformTri[0][X] < xformTri[1][X]) { // 201 if (xformTri[2][X] < xformTri[0][X]) { leftX = floor(xformTri[2][X]); rightX = ceil(xformTri[1][X]); } else { //0 leftX = floor(xformTri[0][X]); // 021 if (xformTri[2][X] < xformTri[1][X]) { rightX = ceil(xformTri[1][X]); } else { // 012 rightX = ceil(xformTri[2][X]); } } } else { // 1 comes before 0 //210 if (xformTri[2][X] < xformTri[1][X]) { leftX = floor(xformTri[2][X]); rightX = ceil(xformTri[0][X]); } else { //1 leftX = floor(xformTri[1][X]); // 120 if (xformTri[2][X] < xformTri[0][X]) { rightX = ceil(xformTri[0][X]); } else { // 102 rightX = ceil(xformTri[2][X]); } } } // For Z Interpolation // Ax + By + Cz + D = 0; // xformTri[0] x xformTri[1] = (A, B, C) GzCoord edge01; edge01[X] = xformTri[1][X]- xformTri[0][X]; edge01[Y] = xformTri[1][Y] - xformTri[0][Y]; edge01[Z] = xformTri[1][Z] - xformTri[0][Z]; GzCoord edge12; edge12[X] = xformTri[2][X] - xformTri[1][X]; edge12[Y] = xformTri[2][Y] - xformTri[1][Y]; edge12[Z] = xformTri[2][Z] - xformTri[1][Z]; float A = edge01[Y]*edge12[Z] - edge01[Z]*edge12[Y]; float B = edge01[Z]*edge12[X] - edge01[X]*edge12[Z]; float C = edge01[X]*edge12[Y] - edge01[Y]*edge12[X]; // get D float D = -(A*xformTri[0][X]) - (B*xformTri[0][Y]) - (C*xformTri[0][Z]); // DRAW PIXELS float interpZ; for (int i = leftX; i < rightX; ++i) { // bounds check if (i < 0 || i > render->display->xres) { continue; } for (int j = topY; j < bottomY; ++j) { //// bounds check if (j < 0 || j > render->display->yres) { continue; } // Compute LEES // E(x, y) = dY(x-X) - dX(y-Y) // EDGE 0-1 float e01 = (xformTri[1][Y] - xformTri[0][Y])*((float)i - xformTri[0][X]) - (xformTri[1][X] - xformTri[0][X])*((float)j - xformTri[0][Y]); // EDGE 1-2 float e12 = (xformTri[2][Y] - xformTri[1][Y])*((float)i - xformTri[1][X]) - (xformTri[2][X] - xformTri[1][X])*((float)j - xformTri[1][Y]); // EDGE 2-0 float e20 = (xformTri[0][Y] - xformTri[2][Y])*((float)i - xformTri[2][X]) - (xformTri[0][X] - xformTri[2][X])*((float)j - xformTri[2][Y]); // if all have same sign then this pixel should be drawn if (e01 == 0 || e12 == 0 || e20 == 0) { interpZ = (-(A*i) - (B*j) - D) / C; // get current z at this pixel GzIntensity r, g, b, a; GzDepth z = 0; GzGetDisplay(render->display, i, j, &r, &g, &b, &a, &z); // compare, if interpZ less than draw over if (interpZ < z) { r = (GzIntensity ) ctoi((float) render->flatcolor[0]); g = (GzIntensity ) ctoi((float)render->flatcolor[1]); b = (GzIntensity ) ctoi((float)render->flatcolor[2]); z = interpZ; GzPutDisplay(render->display, i, j, r, g, b, a, z); } } else if ( ((e01 > 0) && (e12 > 0) && (e20 > 0)) || ((e01 < 0) && (e12 < 0) && (e20 < 0))) { //(e01 == 0 && e12 == 0 && e20 == 0) // Interpolate Z value interpZ = (-(A*i) - (B*j) - D) / C; // get current z at this pixel GzIntensity r, g, b, a; GzDepth z = 0; GzGetDisplay(render->display, i, j, &r, &g, &b, &a, &z); // compare, if interpZ less than draw over if (interpZ < z) { r = (GzIntensity ) ctoi((float) render->flatcolor[0]); g = (GzIntensity ) ctoi((float)render->flatcolor[1]); b = (GzIntensity ) ctoi((float)render->flatcolor[2]); z = interpZ; GzPutDisplay(render->display, i, j, r, g, b, a, z); } } } } } } return GZ_SUCCESS; }
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 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. */ int i,j; //FROM PERSPECTIVE TO SCREEN render->Xsp[0][0]=(render->display->xres)/2; render->Xsp[0][3]=(render->display->xres)/2; render->Xsp[1][1]=-(render->display->yres)/2; render->Xsp[1][3]=(render->display->yres)/2; render->Xsp[2][2]=tan((render->camera.FOV*PI)/360)*INT_MAX; render->Xsp[3][3]=1; GzPushMatrix(render, render->Xsp); //FROM IMAGE OR CAMERA TO PERSPECTIVE for(i=0;i<4;i++) for(j=0;j<4;j++) { if(i==j) render->camera.Xpi[i][j]=1; else render->camera.Xpi[i][j]=0; } render->camera.Xpi[3][2]=tan((render->camera.FOV*PI)/360); GzPushMatrix(render, render->camera.Xpi); //FROM WORLD TO IMAGE GzCoord camx,camy,camz; float updotz; for(i=0;i<3;i++) camz[i]=render->camera.lookat[i]-render->camera.position[i]; normalize(camz); normalize(render->camera.worldup); updotz=dotproduct(render->camera.worldup,camz); for(i=0; i<3; i++) camy[i] = render->camera.worldup[i] - updotz*camz[i]; normalize(camy); crossproduct(camy,camz,camx); float cdotx,cdoty,cdotz; cdotx=dotproduct(camx,render->camera.position); cdoty=dotproduct(camy,render->camera.position); cdotz=dotproduct(camz,render->camera.position); for(int i=0; i<3; i++) { render->camera.Xiw[0][i] = camx[i]; render->camera.Xiw[1][i] = camy[i]; render->camera.Xiw[2][i] = camz[i]; render->camera.Xiw[3][i] = 0; } render->camera.Xiw[0][3] = -1*cdotx; render->camera.Xiw[1][3] = -1*cdoty; render->camera.Xiw[2][3] = -1*cdotz; render->camera.Xiw[3][3] = 1; GzPushMatrix(render, render->camera.Xiw); render->open = 1; render->numlights=0; 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); }
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); }