Ejemplo n.º 1
0
void Matrix::CreateViewMatrix(const Vector3F& position, const Vector3F& lookat, const Vector3F& up)
{
	Vector3F camXAxis, camYAxis, camZAxis;

	camZAxis = lookat - position;
	camZAxis.Normalize();

	camXAxis = up ^ camZAxis;
	camXAxis.Normalize();

	camYAxis = camZAxis ^ camXAxis;
	camYAxis.Normalize();

	CreateViewMatrix(position, camXAxis, camYAxis, camZAxis);
}
Ejemplo n.º 2
0
Archivo: Scene.cpp Proyecto: MrMCG/MGL
void Scene::RenderScene() {
	// Poll input
	PollEvents();

	// Clear screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Update Matrices
	CreateProjectionMatrix();
	CreateViewMatrix();

	// Set uniforms
	glUniformMatrix4fv(glGetUniformLocation(shader->Program(), "viewMatrix"), 1, false, glm::value_ptr(viewMatrix));
	glUniformMatrix4fv(glGetUniformLocation(shader->Program(), "projMatrix"), 1, false, glm::value_ptr(projMatrix));

	// Draw dino
	modelMatrix = glm::translate(glm::mat4(), glm::vec3(50.0f, 00.0f, 0.0f));
	modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(0, -1, 0));
	modelMatrix = glm::scale(modelMatrix, glm::vec3(20.0f, 20.0f, 20.0f));
	glUniformMatrix4fv(glGetUniformLocation(shader->Program(), "modelMatrix"), 1, false, glm::value_ptr(modelMatrix));
	(*meshes)["dino"]->Draw();

	// Draw box
	modelMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, 0.0f));
	modelMatrix = glm::scale(modelMatrix, glm::vec3(10.0f, 10.0f, 10.0f));
	glUniformMatrix4fv(glGetUniformLocation(shader->Program(), "modelMatrix"), 1, false, glm::value_ptr(modelMatrix));
	(*meshes)["box"]->Draw();

	// Draw death star
	modelMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 750.0f, 0.0f));
	modelMatrix = glm::scale(modelMatrix, glm::vec3(0.5f, 0.5f, 0.5f));
	glUniformMatrix4fv(glGetUniformLocation(shader->Program(), "modelMatrix"), 1, false, glm::value_ptr(modelMatrix));
	(*meshes)["deathstar"]->Draw();

	// Swap buffers
	SwapBuffers();
}
Ejemplo n.º 3
0
int	main(int argc, CHAR *argv[])
	{
	INT	i;
	UINT	begin;
	UINT	end;
	UINT	lapsed;
	MATRIX	vtrans, Vinv;		/*  View transformation and inverse. */


	/*
	 *	First, process command line arguments.
	 */
	i = 1;
	while ((i < argc) && (argv[i][0] == '-')) {
		switch (argv[i][1]) {
			case '?':
			case 'h':
			case 'H':
				Usage();
				exit(1);

			case 'a':
			case 'A':
				AntiAlias = TRUE;
				if (argv[i][2] != '\0') {
					NumSubRays = atoi(&argv[i][2]);
				} else {
					NumSubRays = atoi(&argv[++i][0]);
				}
				break;

			case 'm':
				if (argv[i][2] != '\0') {
					MaxGlobMem = atoi(&argv[i][2]);
				} else {
					MaxGlobMem = atoi(&argv[++i][0]);
				}
				break;

			case 'p':
				if (argv[i][2] != '\0') {
					nprocs = atoi(&argv[i][2]);
				} else {
					nprocs = atoi(&argv[++i][0]);
				}
				break;

			case 's':
			case 'S':
				dostats = TRUE;
				break;

			default:
				fprintf(stderr, "%s: Invalid option \'%c\'.\n", ProgName, argv[i][0]);
				exit(1);
		}
		i++;
	}

	if (i == argc) {
		Usage();
		exit(1);
	}


	/*
	 *	Make sure nprocs is within valid range.
	 */

	if (nprocs < 1 || nprocs > MAX_PROCS)
		{
		fprintf(stderr, "%s: Valid range for #processors is [1, %d].\n", ProgName, MAX_PROCS);
		exit(1);
		}


	/*
	 *	Print command line parameters.
	 */

	printf("\n");
	printf("Number of processors:     \t%ld\n", nprocs);
	printf("Global shared memory size:\t%ld MB\n", MaxGlobMem);
	printf("Samples per pixel:        \t%ld\n", NumSubRays);
	printf("\n");


	/*
	 *	Initialize the shared memory environment and request the total
	 *	amount of amount of shared memory we might need.  This
	 *	includes memory for the database, grid, and framebuffer.
	 */

	MaxGlobMem <<= 20;			/* Convert MB to bytes.      */
	MAIN_INITENV(,MaxGlobMem + 512*1024)
   THREAD_INIT_FREE();
	gm = (GMEM *)G_MALLOC(sizeof(GMEM));


	/*
	 *	Perform shared environment initializations.
	 */

	gm->nprocs = nprocs;
	gm->pid    = 0;
	gm->rid    = 1;

	BARINIT(gm->start, nprocs)
	LOCKINIT(gm->pidlock)
	LOCKINIT(gm->ridlock)
	LOCKINIT(gm->memlock)
	ALOCKINIT(gm->wplock, nprocs)

/* POSSIBLE ENHANCEMENT:  Here is where one might distribute the
   raystruct data structure across physically distributed memories as
   desired.  */

	if (!GlobalHeapInit(MaxGlobMem))
		{
		fprintf(stderr, "%s: Cannot initialize global heap.\n", ProgName);
		exit(1);
		}


	/*
	 *	Initialize HUG parameters, read environment and geometry files.
	 */

	Huniform_defaults();
	ReadEnvFile(/* *argv*/argv[i]);
	ReadGeoFile(GeoFileName);
	OpenFrameBuffer();


	/*
	 *	Compute view transform and its inverse.
	 */

	CreateViewMatrix();
	MatrixCopy(vtrans, View.vtrans);
	MatrixInverse(Vinv, vtrans);
	MatrixCopy(View.vtransInv, Vinv);


	/*
	 *	Print out what we have so far.
	 */

	printf("Number of primitive objects: \t%ld\n", prim_obj_cnt);
	printf("Number of primitive elements:\t%ld\n", prim_elem_cnt);

	/*
	 *	Preprocess database into hierarchical uniform grid.
	 */

	if (TraversalType == TT_HUG)
		BuildHierarchy_Uniform();



	/*
	 *	Now create slave processes.
	 */

	CLOCK(begin)
	CREATE(StartRayTrace, gm->nprocs);
	WAIT_FOR_END(gm->nprocs);
	CLOCK(end)



	/*
	 *	We are finished.  Clean up, print statistics and run time.
	 */

	CloseFrameBuffer(PicFileName);
	PrintStatistics();

	lapsed = (end - begin) & 0x7FFFFFFF;



	printf("TIMING STATISTICS MEASURED BY MAIN PROCESS:\n");
	printf("        Overall start time     %20lu\n", begin);
	printf("        Overall end time   %20lu\n", end);
	printf("        Total time with initialization  %20lu\n", lapsed);
	printf("        Total time without initialization  %20lu\n", end - gm->par_start_time);

    if (dostats) {
        unsigned totalproctime, maxproctime, minproctime;

        printf("\n\n\nPER-PROCESS STATISTICS:\n");

        printf("%20s%20s\n","Proc","Time");
        printf("%20s%20s\n\n","","Tracing Rays");
        for (i = 0; i < gm->nprocs; i++)
            printf("%20ld%20ld\n",i,gm->partime[i]);

        totalproctime = gm->partime[0];
        minproctime = gm->partime[0];
        maxproctime = gm->partime[0];

        for (i = 1; i < gm->nprocs; i++) {
            totalproctime += gm->partime[i];
            if (gm->partime[i] > maxproctime)
                maxproctime = gm->partime[i];
            if (gm->partime[i] < minproctime)
                minproctime = gm->partime[i];
        }
        printf("\n\n%20s%20d\n","Max = ",maxproctime);
        printf("%20s%20d\n","Min = ",minproctime);
        printf("%20s%20d\n","Avg = ",(int) (((double) totalproctime) / ((double) (1.0 * gm->nprocs))));
    }

	MAIN_END
	}
Ejemplo n.º 4
0
//Initialisation
bool 
ASCDX9Renderer::Initialise( SRendererInit& rParameters )
{
	WNDCLASSEX wc = 
	{ 
		sizeof(WNDCLASSEXA), 
		CS_CLASSDC,
		ASCDX9Renderer::MsgProc, 
		0L, 0L, 
		GetModuleHandle(NULL), 
		NULL, NULL, 
		NULL, NULL,
#ifdef _DEBUG
		"AscensionWindow",
#else
		L"AscensionWindow",
#endif
		NULL 
	};
	RegisterClassEx( &wc );

	LPCWSTR lstrWindowTitle = CharStrToLPCWSTR(rParameters.m_strWindowName.c_str());

	RECT rc = { 0, 0, rParameters.m_uScreenWidth, rParameters.m_uScreenHeight };

	AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, false );

	m_hWnd = CreateWindowW(		L"AscensionWindow",
								lstrWindowTitle,
								WS_OVERLAPPEDWINDOW - (WS_MAXIMIZEBOX|WS_THICKFRAME), 
								CW_USEDEFAULT, CW_USEDEFAULT, 
								rc.right - rc.left, 
								rc.bottom - rc.top, 
								GetDesktopWindow(), NULL, 
								wc.hInstance, NULL );
	
	assert_msg(m_hWnd != NULL, "Guts, Failed to create window");
	
	UINT32 uScreenX = GetSystemMetrics(SM_CXFULLSCREEN);
	UINT32 uScreenY = GetSystemMetrics(SM_CYFULLSCREEN);

	UINT32 uX = (uScreenX / 2) - (rParameters.m_uScreenWidth / 2);
	UINT32 uY = (uScreenY / 2) - (rParameters.m_uScreenHeight / 2);

	SetWindowPos(m_hWnd, NULL, uX, uY, 0, 0, SWP_NOSIZE);

	if ( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		assert_now("Guts, Failed to create D3D9");
		return false;
	}
	
	//Window parameters
	ZeroMemory( &m_d3dPP, sizeof( m_d3dPP ) );
	
	m_d3dPP.Windowed = rParameters.m_bWindowMode;
	m_d3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
	m_d3dPP.BackBufferFormat = D3DFMT_X8R8G8B8;
	m_d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	m_d3dPP.EnableAutoDepthStencil = TRUE;
	m_d3dPP.AutoDepthStencilFormat = D3DFMT_D24X8;
	m_d3dPP.BackBufferWidth = rParameters.m_uScreenWidth;
	m_d3dPP.BackBufferHeight = rParameters.m_uScreenHeight;

	UINT32 AdapterToUse = D3DADAPTER_DEFAULT;
	D3DDEVTYPE DeviceType=D3DDEVTYPE_HAL;
	for (UINT32 Adapter = 0; Adapter < m_pD3D->GetAdapterCount(); Adapter++)
	{
		D3DADAPTER_IDENTIFIER9 Identifier;
		HRESULT Res;
		Res = m_pD3D->GetAdapterIdentifier(Adapter,0,&Identifier);
		if (strstr(Identifier.Description,"PerfHUD") != 0)
		{
			AdapterToUse=Adapter;
			DeviceType=D3DDEVTYPE_REF;
			break;
		}
	}
	
	if ( D3D_OK != m_pD3D->CreateDevice( AdapterToUse, DeviceType, 
										m_hWnd,
										 D3DCREATE_HARDWARE_VERTEXPROCESSING,
										&m_d3dPP, &m_pDevice ) ) 
	{
		assert_now("Guts, Failed to create D3D9 device");
		return false;
	}
	
	// Show the window
	ShowWindow( m_hWnd, SW_SHOWDEFAULT );
	UpdateWindow( m_hWnd );

	m_hInst = wc.hInstance;
	
	VertElement sDeclElem[] = {	VertElement::Init(0,	ES_POSITION,	0, ET_FLOAT32,	4),
								VertElement::Init(16,	ES_COLOUR,		0, ET_UINT8,		4),
								VertElement::Init(20,	ES_TANGENT,		0, ET_UINT8,		4),
								VertElement::Init(24,	ES_TEXTURE,		0, ET_FLOAT32,	2),
								VertElement::Init(32,	ES_NORMAL,		0, ET_FLOAT32,	3),
								VertElement::Init(44,	ES_BINORMAL,	0, ET_FLOAT32,	3),};

	m_pDefaultVertDecl = new ASCDX9VertexDeclaration(sDeclElem, 6, m_pDevice);
	m_pDefaultVertDecl->Apply();

	D3DCAPS9 pCaps;
	m_pD3D->GetDeviceCaps(AdapterToUse, DeviceType, &pCaps);
	if (pCaps.AlphaCmpCaps & D3DPCMPCAPS_GREATEREQUAL)
	{
		m_pDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x00000001);
		m_pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); 
		m_pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
	}

	SetRenderState( RS_bAlphaBlendEnable, true );
	SetRenderState( RS_bAlphaBlendEnable, true );
	SetRenderState( RS_eAlphaSrcBlend, AB_SrcAlpha );
	SetRenderState( RS_eDestBlend, AB_InvSrcAlpha );
	
	m_pTextureManager = new ASCDX9TextureManager( m_pDevice );
	m_pShaderManager = new ASCDX9ShaderManager( m_pDevice );

	m_ProjectionMatrix = CreateProjectionMatrix( SC_FLOAT(rParameters.m_uScreenWidth), SC_FLOAT(rParameters.m_uScreenHeight), rParameters.m_fNear, rParameters.m_fFar);
	SetProjectionMatrix(m_ProjectionMatrix);

	m_ViewMatrix = CreateViewMatrix();
	SetViewMatrix(m_ViewMatrix);

	return true;
}