Ejemplo n.º 1
0
void BeginRender()
{
	Color24 *temp;
	temp = renderImage.GetPixels();
	float *zBuffer = renderImage.GetZBuffer();
	Color materialColor;
	Color24 newColor;
	for (int i = 0; i < camera.imgWidth; i++)
	{
		for (int j = 0; j < camera.imgHeight; j++)
		{
			Ray currentRay = ComputeCameraRay(i, j);
			HitInfo hInfo;
			hInfo.Init();
			bool value = TraceRay(currentRay, hInfo, &rootNode);
			if (value) {
				hInfo.node->FromNodeCoords(hInfo);
				const Material *m = hInfo.node->GetMaterial();
				materialColor = m->Shade(currentRay, hInfo, lights);
				temp[camera.imgWidth*j + i].r = (materialColor.r)*255;
				temp[camera.imgWidth*j + i].g = (materialColor.g)*255;
				temp[camera.imgWidth*j + i].b = (materialColor.b)*255;
			}
			else {
				temp[camera.imgWidth*j + i].r = 0;
				temp[camera.imgWidth*j + i].g = 0;
				temp[camera.imgWidth*j + i].b = 0;
			}
			zBuffer[camera.imgWidth*j + i] = hInfo.z;
		}
	}
	renderImage.ComputeZBufferImage();
	renderImage.SaveImage("Output");
}
Ejemplo n.º 2
0
void Renderer::startRendering(size_t i_threadCount)
{
	threadCount = i_threadCount;
	mThreadHandle = ThreadHandle(threadCount);
	imageWidth = renderImage.GetWidth();
	imageHeight = renderImage.GetHeight();
	noOfRowsToRenderPerThread = imageHeight / threadCount;
	renderingImage = renderImage.GetPixels();
	zBufferImage = renderImage.GetZBuffer();
	sampleCountImage = renderImage.GetSampleCount();
	operationCountImage = renderImage.GetOperationCountImage();

	myEngine::Timing::Clock *clock = myEngine::Timing::Clock::createAndStart();

	/*for (int i = 0; i < renderImage.GetHeight(); ++i)
	{
		for (int j = 0; j < renderImage.GetWidth(); ++j)
		{
			calculatePixelColor(rootNode,lights,j, i);
		}
	}*/

	

	int *threadVal = new int[threadCount];
	for (size_t i = 0; i < threadCount; i++)
	{
		threadVal[i] = i;
		std::cout << "\nPassing Value to thread" << threadVal[i];
		mThreadHandle.thread[i] = CreateThread(nullptr, 0, static_cast<LPTHREAD_START_ROUTINE>(renderPixel), &threadVal[i], CREATE_SUSPENDED, nullptr);
	}

	for (size_t i = 0; i < threadCount;i++)
	{
		ResumeThread(mThreadHandle.thread[i]);
	}
	
	std::cout << "Wait Val"<<std::endl << WaitForMultipleObjects(threadCount, mThreadHandle.thread, TRUE, INFINITE) << std::endl;;
	std::cout << "Wait finished";
	
	if (WaitForMultipleObjects(threadCount + 1, mThreadHandle.thread, TRUE, INFINITE))
	{
		mThreadHandle.destroyThread();
	}

	TCHAR* mutexName = __T("WritingMutex");
	HANDLE mutexHandle = OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutexName);
	CloseHandle(mutexHandle);


	renderImage.SaveImage("RayCasted.ppm");
	renderImage.ComputeZBufferImage();
	renderImage.SaveZImage("RayCastWithZ.ppm");
	renderImage.ComputeSampleCountImage();
	renderImage.SaveSampleCountImage("SampleCountImage.ppm");
	clock->updateDeltaTime();
	double time = clock->getdeltaTime();
	printf("Time to render ray casting  %f", clock->getdeltaTime());
}
Ejemplo n.º 3
0
void GlutKeyboard(unsigned char key, int x, int y)
{
    switch ( key ) {
        case 27:    // ESC
            exit(0);
            break;
        case ' ':
            switch ( mode ) {
                case MODE_READY:
                    mode = MODE_RENDERING;
                    viewMode = VIEWMODE_IMAGE;
                    if ( dofImage ) {
                        Color24 *p = renderImage.GetPixels();
                        for ( int i=0; i<camera.imgWidth*camera.imgHeight; i++ ) p[i] = dofImage[i];
                    } else {
                        DrawScene();
                        glReadPixels( 0, 0, renderImage.GetWidth(), renderImage.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, renderImage.GetPixels() );
                    }
                    startTime = time(NULL);
                    BeginRender();
                    break;
                case MODE_RENDERING:
                    mode = MODE_READY;
                    StopRender();
                    glutPostRedisplay();
                    break;
                case MODE_RENDER_DONE:
                    mode = MODE_READY;
                    viewMode = VIEWMODE_OPENGL;
                    glutPostRedisplay();
                    break;
            }
            break;
        case '1':
            viewAngle1 = viewAngle2 = 0;
            viewMode = VIEWMODE_OPENGL;
            glutPostRedisplay();
            break;
        case '2':
            viewMode = VIEWMODE_IMAGE;
            glutPostRedisplay();
            break;
        case '3':
            viewMode = VIEWMODE_Z;
            glutPostRedisplay();
            break;
        case '4':
            viewMode = VIEWMODE_SAMPLECOUNT;
            glutPostRedisplay();
            break;
        case '5':
            viewMode = VIEWMODE_IRRADCOMP;
            glutPostRedisplay();
            break;
    }
}
Ejemplo n.º 4
0
void PrintPixelData(int x, int y)
{
    if ( x < renderImage.GetWidth() && y < renderImage.GetHeight() ) {
        Color24 *colors = renderImage.GetPixels();
        float *zbuffer = renderImage.GetZBuffer();
        int i = y*renderImage.GetWidth() + x;
        printf("Pixel [ %d, %d ] Color24: %d, %d, %d   Z: %f\n", x, y, colors[i].r, colors[i].g, colors[i].b, zbuffer[i] );
    } else {
        printf("-- Invalid pixel (%d,%d) --\n",x,y);
    }
}
Ejemplo n.º 5
0
void GlutDisplay()
{
    switch ( viewMode ) {
        case VIEWMODE_OPENGL:
            if ( dofImage ) {
                if ( dofDrawCount < MAX_DOF_DRAW ) {
                    DrawScene();
                    glReadPixels( 0, 0, camera.imgWidth, camera.imgHeight, GL_RGB, GL_UNSIGNED_BYTE, dofBuffer );
                    for ( int i=0; i<camera.imgWidth*camera.imgHeight; i++ ) {
                        dofImage[i] = (dofImage[i]*dofDrawCount + dofBuffer[i].ToColor())/(dofDrawCount+1);
                    }
                    dofDrawCount++;
                }
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
                glDrawPixels( camera.imgWidth, camera.imgHeight, GL_RGB, GL_FLOAT, dofImage );
                if ( dofDrawCount < MAX_DOF_DRAW ) {
                    DrawProgressBar(float(dofDrawCount)/MAX_DOF_DRAW);
                    glutPostRedisplay();
                }
            } else {
                DrawScene();
            }
            break;
        case VIEWMODE_IMAGE:
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
            glDrawPixels( renderImage.GetWidth(), renderImage.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, renderImage.GetPixels() );
            DrawRenderProgressBar();
            break;
        case VIEWMODE_Z:
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
            if ( ! renderImage.GetZBufferImage() ) renderImage.ComputeZBufferImage();
            glDrawPixels( renderImage.GetWidth(), renderImage.GetHeight(), GL_LUMINANCE, GL_UNSIGNED_BYTE, renderImage.GetZBufferImage() );
            break;
        case VIEWMODE_SAMPLECOUNT:
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
            if ( ! renderImage.GetSampleCountImage() ) renderImage.ComputeSampleCountImage();
            glDrawPixels( renderImage.GetWidth(), renderImage.GetHeight(), GL_LUMINANCE, GL_UNSIGNED_BYTE, renderImage.GetSampleCountImage() );
            break;
        case VIEWMODE_IRRADCOMP:
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
            if ( renderImage.GetIrradianceComputationImage() ) {
                glDrawPixels( renderImage.GetWidth(), renderImage.GetHeight(), GL_LUMINANCE, GL_UNSIGNED_BYTE, renderImage.GetIrradianceComputationImage() );
            }
            break;
    }
    
    glutSwapBuffers();
}
Ejemplo n.º 6
0
void GlutKeyboard(unsigned char key, int x, int y)
{
	switch ( key ) {
	case 27:	// ESC
		exit(0);
		break;
	case ' ':
		switch ( mode ) {
		case MODE_READY: 
			mode = MODE_RENDERING;
			viewMode = VIEWMODE_IMAGE;
			DrawScene();
			glReadPixels( 0, 0, renderImage.GetWidth(), renderImage.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, renderImage.GetPixels() );
			startTime = time(NULL);
			BeginRender();
			break;
		case MODE_RENDERING:
			mode = MODE_READY;
			StopRender();
			glutPostRedisplay();
			break;
		case MODE_RENDER_DONE: 
			mode = MODE_READY;
			viewMode = VIEWMODE_OPENGL;
			glutPostRedisplay();
			break;
		}
		break;
	case '1':
		viewAngle1 = viewAngle2 = 0;
		viewMode = VIEWMODE_OPENGL;
		glutPostRedisplay();
		break;
	case '2':
		viewMode = VIEWMODE_IMAGE;
		glutPostRedisplay();
		break;
	case '3':
		viewMode = VIEWMODE_Z;
		glutPostRedisplay();
		break;
	}
}
Ejemplo n.º 7
0
void GlutDisplay()
{
	switch ( viewMode ) {
	case VIEWMODE_OPENGL:
		DrawScene();
		break;
	case VIEWMODE_IMAGE:
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
		glDrawPixels( renderImage.GetWidth(), renderImage.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, renderImage.GetPixels() );
		DrawRenderProgressBar();
		break;
	case VIEWMODE_Z:
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
		if ( ! renderImage.GetZBufferImage() ) renderImage.ComputeZBufferImage();
		glDrawPixels( renderImage.GetWidth(), renderImage.GetHeight(), GL_LUMINANCE, GL_UNSIGNED_BYTE, renderImage.GetZBufferImage() );
		break;
	}

	glutSwapBuffers();
}