COculusVR::COculusVR(bool latency)
{
	m_isReady = true;

	// Initializes LibOVR, and the Rift
    ovr_Initialize();

    Hmd = ovrHmd_Create(0);
    if (!Hmd)
    {
        MessageBoxA(NULL, "Oculus Rift not detected.", "", MB_OK);
        return;
    }
    if (Hmd->ProductName[0] == '\0')
        MessageBoxA(NULL, "Rift detected, display not enabled.", "", MB_OK);

    if (Hmd->HmdCaps & ovrHmdCap_ExtendDesktop)
    {
        WindowSize = Hmd->Resolution;
    }
    else
    {
        // In Direct App-rendered mode, we can use smaller window size,
        // as it can have its own contents and isn't tied to the buffer.
        WindowSize = Sizei(1100, 618);//Sizei(960, 540); avoid rotated output bug.
    }

	ovrHmd_AttachToWindow(Hmd, wzGetWindowHandle(), NULL, NULL);

	// Configure Stereo settings.
	Sizei recommenedTex0Size = ovrHmd_GetFovTextureSize(Hmd, ovrEye_Left, Hmd->DefaultEyeFov[0], 1.0f);
	Sizei recommenedTex1Size = ovrHmd_GetFovTextureSize(Hmd, ovrEye_Right, Hmd->DefaultEyeFov[1], 1.0f);

    EyeRenderTargetSize.w = recommenedTex0Size.w + recommenedTex1Size.w;
    EyeRenderTargetSize.h = Alg::Max( recommenedTex0Size.h, recommenedTex1Size.h );

	//Create Framebuffer
	wzCreateRenderTarget(&m_screenRender);
	wzCreateRenderBufferDepth(&m_screenBuffer,EyeRenderTargetSize.w,EyeRenderTargetSize.h);
	wzCreateTexture(&m_screenTex,EyeRenderTargetSize.w,EyeRenderTargetSize.h,WZ_FORMATTYPE_RGB,NULL);
	//attach
	wzSetRenderBuffer(&m_screenRender,&m_screenBuffer);
	wzSetRenderTexture(&m_screenRender,&m_screenTex);

    // Initialize eye rendering information.
    // The viewport sizes are re-computed in case RenderTargetSize changed due to HW limitations.
    ovrFovPort eyeFov[2] = { Hmd->DefaultEyeFov[0], Hmd->DefaultEyeFov[1] } ;

    EyeRenderViewport[0].Pos  = Vector2i(0,0);
    EyeRenderViewport[0].Size = Sizei(EyeRenderTargetSize.w / 2, EyeRenderTargetSize.h);
    EyeRenderViewport[1].Pos  = Vector2i((EyeRenderTargetSize.w + 1) / 2, 0);
    EyeRenderViewport[1].Size = EyeRenderViewport[0].Size;

	//Shader vertex format
	wzVertexElements ve_var[] = {
		{WZVETYPE_FLOAT2,"position"},
		{WZVETYPE_FLOAT1,"timewarpLerpFactor"},
		{WZVETYPE_FLOAT1,"vignette"},
		{WZVETYPE_FLOAT2,"texCoord0"},
		{WZVETYPE_FLOAT2,"texCoord1"},
		{WZVETYPE_FLOAT2,"texCoord2"},
		WZVE_TMT()
	};

	//carete mesh
	for ( int eyeNum = 0; eyeNum < 2; eyeNum++ )
	{
		// Allocate mesh vertices, registering with renderer using the OVR vertex format.
		ovrDistortionMesh meshData;
		ovrHmd_CreateDistortionMesh(Hmd, (ovrEyeType) eyeNum, eyeFov[eyeNum],
									ovrDistortionCap_Chromatic | ovrDistortionCap_TimeWarp, &meshData);
		//Create datas
		wzVector2* vertex_pos = new wzVector2[meshData.VertexCount];
		float* vertex_posTimewarp = new float[meshData.VertexCount];
		float* vertex_posVignette = new float[meshData.VertexCount];
		wzVector2* vertex_textanR = new wzVector2[meshData.VertexCount];
		wzVector2* vertex_textanG = new wzVector2[meshData.VertexCount];
		wzVector2* vertex_textanB = new wzVector2[meshData.VertexCount];

		//data copy
		for(unsigned int i = 0; i < meshData.VertexCount; i++) {
			vertex_pos[i].x = meshData.pVertexData[i].ScreenPosNDC.x;
			vertex_pos[i].y = meshData.pVertexData[i].ScreenPosNDC.y;
			vertex_posTimewarp[i] = meshData.pVertexData[i].TimeWarpFactor;
			vertex_posVignette[i] = meshData.pVertexData[i].VignetteFactor;
			vertex_textanR[i].x = meshData.pVertexData[i].TanEyeAnglesR.x;
			vertex_textanR[i].y = meshData.pVertexData[i].TanEyeAnglesR.y;
			vertex_textanG[i].x = meshData.pVertexData[i].TanEyeAnglesG.x;
			vertex_textanG[i].y = meshData.pVertexData[i].TanEyeAnglesG.y;
			vertex_textanB[i].x = meshData.pVertexData[i].TanEyeAnglesB.x;
			vertex_textanB[i].y = meshData.pVertexData[i].TanEyeAnglesB.y;
		}

		void* vertex_pointer[] = {vertex_pos,vertex_posTimewarp,vertex_posVignette,vertex_textanR,vertex_textanG,vertex_textanB};

		if(wzCreateMesh(&MeshBuffer[eyeNum], vertex_pointer, ve_var,
			meshData.pIndexData, meshData.VertexCount, meshData.IndexCount)) {
				MessageBoxA(NULL, "Lens Distort Mesh Error.", "", MB_OK);
				
			delete[] vertex_pos;
			delete[] vertex_posTimewarp;
			delete[] vertex_posVignette;
			delete[] vertex_textanR;
			delete[] vertex_textanG;
			delete[] vertex_textanB;

			return;	//error
		}
		wzChangeDrawMode(&MeshBuffer[eyeNum],WZ_MESH_DF_TRIANGLELIST);

		delete[] vertex_pos;
		delete[] vertex_posTimewarp;
		delete[] vertex_posVignette;
		delete[] vertex_textanR;
		delete[] vertex_textanG;
		delete[] vertex_textanB;

		ovrHmd_DestroyDistortionMesh(&meshData);

		//Create eye render description for use later
		EyeRenderDesc[eyeNum] = ovrHmd_GetRenderDesc(Hmd, (ovrEyeType) eyeNum,  eyeFov[eyeNum]);

		//Do scale and offset
		ovrHmd_GetRenderScaleAndOffset(eyeFov[eyeNum],EyeRenderTargetSize, EyeRenderViewport[eyeNum], UVScaleOffset[eyeNum]);
	}

	//Create shader
	if(wzCreateShader(&LensShader, ols_vertexshader,ols_flagshader, ve_var)) {
		MessageBoxA(NULL, "Lens Shader Compile Error.", "", MB_OK);
		return;
	}

    if(latency) ovrHmd_SetEnabledCaps(Hmd, ovrHmdCap_DynamicPrediction);	//ovrHmdCap_LowPersistence
	// Start the sensor which informs of the Rift's pose and motion
	ovrHmd_ConfigureTracking(Hmd, ovrTrackingCap_Orientation |
								ovrTrackingCap_MagYawCorrection, 0);		//not use : ovrTrackingCap_Position

	m_isReady = false;
}
Example #2
0
bool C3DCubeModel::CreateModel(float size)
{
	if(m_isCreated)
		return false;

	// Shader vertex format
	wzVertexElements ve_var[] = {
		{WZVETYPE_FLOAT3,"vPos"},
		{WZVETYPE_FLOAT3,"vNml"},
		WZVE_TMT()
	};
	
	// Create shader.
	if(wzCreateShader_GLSL(&m_shader,
		g_c3d_mdsource_vs,g_c3d_mdsource_fs,ve_var))
			return false;

	// Create mesh
	float half_width = size * 0.5f;
	float half_depth = size * 0.5f;
	const float vertex_pointer_v[] = {
		-half_width,	0.000,	half_depth,
		half_width,		0.000,	half_depth,
		-half_width,	size,	half_depth,
		-half_width,	size,	half_depth,
		half_width,		0.000,	half_depth,
		half_width,		size,	half_depth,
		-half_width,	size,	half_depth,
		half_width,		size,	half_depth,
		-half_width,	size,	-half_depth,
		-half_width,	size,	-half_depth,
		half_width,		size,	half_depth,
		half_width,		size,	-half_depth,
		-half_width,	size,	-half_depth,
		half_width,		size,	-half_depth,
		-half_width,	0.000,	-half_depth,
		-half_width,	0.000,	-half_depth,
		half_width,		size,	-half_depth,
		half_width,		0.000,	-half_depth,
		-half_width,	0.000,	-half_depth,
		half_width,		0.000,	-half_depth,
		-half_width,	0.000,	half_depth,
		-half_width,	0.000,	half_depth,
		half_width,		0.000,	-half_depth,
		half_width,		0.000,	half_depth,
		half_width,		0.000,	half_depth,
		half_width,		0.000,	-half_depth,
		half_width,		size,	half_depth,
		half_width,		size,	half_depth,
		half_width,		0.000,	-half_depth,
		half_width,		size,	-half_depth,
		-half_width,	0.000,	-half_depth,
		-half_width,	0.000,	half_depth,
		-half_width,	size,	-half_depth,
		-half_width,	size,	-half_depth,
		-half_width,	0.000,	half_depth,
		-half_width,	size,	half_depth,
	};
	const float vertex_pointer_nml[] = {
		0.000000,	0.000000,	1.000000,
		0.000000,	0.000000,	1.000000,
		0.000000,	0.000000,	1.000000,
		0.000000,	0.000000,	1.000000,
		0.000000,	0.000000,	1.000000,
		0.000000,	0.000000,	1.000000,
		0.000000,	1.000000,	0.000000,
		0.000000,	1.000000,	0.000000,
		0.000000,	1.000000,	0.000000,
		0.000000,	1.000000,	0.000000,
		0.000000,	1.000000,	0.000000,
		0.000000,	1.000000,	0.000000,
		0.000000,	0.000000,	-1.000000,
		0.000000,	0.000000,	-1.000000,
		0.000000,	0.000000,	-1.000000,
		0.000000,	0.000000,	-1.000000,
		0.000000,	0.000000,	-1.000000,
		0.000000,	0.000000,	-1.000000,
		0.000000,	-1.000000,	0.000000,
		0.000000,	-1.000000,	0.000000,
		0.000000,	-1.000000,	0.000000,
		0.000000,	-1.000000,	0.000000,
		0.000000,	-1.000000,	0.000000,
		0.000000,	-1.000000,	0.000000,
		1.000000,	0.000000,	0.000000,
		1.000000,	0.000000,	0.000000,
		1.000000,	0.000000,	0.000000,
		1.000000,	0.000000,	0.000000,
		1.000000,	0.000000,	0.000000,
		1.000000,	0.000000,	0.000000,
		-1.000000,	0.000000,	0.000000,
		-1.000000,	0.000000,	0.000000,
		-1.000000,	0.000000,	0.000000,
		-1.000000,	0.000000,	0.000000,
		-1.000000,	0.000000,	0.000000,
		-1.000000,	0.000000,	0.000000,	
	};
	const float* vertex_pointer[] = {vertex_pointer_v, vertex_pointer_nml};
	if(wzCreateMesh(&m_cube, (void**)&vertex_pointer, ve_var, NULL, 36, 12)) {
		return false;
	}
	wzChangeDrawMode(&m_cube,WZ_MESH_DF_TRIANGLELIST);

	// Created flag
	m_isCreated = true;

	return true;
}