3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Based on example code:
21 * Content : Simple minimal VR demo
22 * Created : December 1, 2014
24 * Copyright : Copyright 2012 Oculus, Inc. All Rights reserved.
26 * Licensed under the Apache License, Version 2.0 (the "License");
27 * you may not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
30 * http://www.apache.org/licenses/LICENSE-2.0
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
44 #include "../libsdl/print.h"
48 #include <OVR_CAPI_GL.h>
51 #define GL3_PROTOTYPES 1
56 #include <dxgi.h> // for GetDefaultAdapterLuid
57 #pragma comment(lib, "dxgi.lib")
60 static ovrGraphicsLuid GetDefaultAdapterLuid()
62 ovrGraphicsLuid luid = ovrGraphicsLuid();
65 IDXGIFactory* factory = nullptr;
67 if (SUCCEEDED(CreateDXGIFactory(IID_PPV_ARGS(&factory)))) {
68 IDXGIAdapter* adapter = nullptr;
70 if (SUCCEEDED(factory->EnumAdapters(0, &adapter))) {
71 DXGI_ADAPTER_DESC desc;
73 adapter->GetDesc(&desc);
74 memcpy(&luid, &desc.AdapterLuid, sizeof(luid));
85 static int Compare(const ovrGraphicsLuid lhs, const ovrGraphicsLuid rhs)
87 return memcmp(&lhs, &rhs, sizeof(ovrGraphicsLuid));
91 static int ovr_initialized = 0;
92 static ovrSession session = NULL;
93 static ovrGraphicsLuid luid;
94 static ovrHmdDesc hmdDesc;
95 static ovrSizei TextureSize[2];
96 static ovrTextureSwapChain textureSwapChain[2] = { NULL, NULL };
97 static GLuint fboId[2] = { 0, 0 };
98 static GLuint mirrorFBO = 0;
99 static int mirror_width;
100 static int mirror_height;
101 static ovrEyeRenderDesc eyeRenderDesc[2];
102 static ovrPosef hmdToEyeViewPose[2];
103 static ovrLayerEyeFov layer;
104 static int multisampling;
105 static long long frameIndex = 0;
106 static double observer_x = 0.0;
107 static double observer_x_normalize = 0.0;
108 static double observer_y = 0.0;
109 static double observer_y_normalize = 0.0;
110 static double observer_z = 0.0;
111 static double observer_z_normalize = 0.0;
113 int init_ovr(int _multisampling)
118 multisampling = _multisampling;
120 glewExperimental = GL_TRUE;
121 if (glewInit() != GLEW_OK) {
122 print_error("Failed to init GLEW\n");
126 result = ovr_Initialize(NULL);
127 if (OVR_FAILURE(result)) {
128 print_error("Failed to init OVR (is Oculus Rift service running?)\n");
133 result = ovr_Create(&session, &luid);
134 if (OVR_FAILURE(result)) {
135 print_error("Failed to create OVR session (is the HMD connected?)\n");
140 if (Compare(luid, GetDefaultAdapterLuid())) { // If luid that the Rift is on is not the default adapter LUID...
141 print_error("OpenGL supports only the default graphics adapter.\n");
146 hmdDesc = ovr_GetHmdDesc(session);
148 memset(&layer, 0, sizeof(layer));
149 layer.Header.Type = ovrLayerType_EyeFov;
150 layer.Header.Flags = 0;
152 /* do we need this??? seems to work without */
153 if (multisampling > 1)
154 glEnable(GL_MULTISAMPLE);
156 /* create render buffers */
157 for (eye = 0; eye < 2; eye++) {
158 TextureSize[eye] = ovr_GetFovTextureSize(session, (eye == 0) ? ovrEye_Left : ovrEye_Right, hmdDesc.DefaultEyeFov[eye], 1.0);
159 ovrTextureSwapChainDesc desc;
163 memset(&desc, 0, sizeof(desc));
164 desc.Type = ovrTexture_2D;
166 desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
167 desc.Width = TextureSize[eye].w;
168 desc.Height = TextureSize[eye].h;
170 desc.SampleCount = (multisampling > 1) ? multisampling : 1;
171 desc.StaticImage = ovrFalse;
173 result = ovr_CreateTextureSwapChainGL(session, &desc, &textureSwapChain[eye]);
174 if (OVR_FAILURE(result)) {
175 print_error("ovr_CreateTextureSwapChainGL() failed!\n");
179 ovr_GetTextureSwapChainLength(session, textureSwapChain[eye], &length);
180 for (i = 0; i < length; i++) {
181 ovr_GetTextureSwapChainBufferGL(session, textureSwapChain[eye], i, &chainTexId);
182 glBindTexture((multisampling > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, chainTexId);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
189 glGenFramebuffers(1, &(fboId[eye]));
191 eyeRenderDesc[eye] = ovr_GetRenderDesc(session, (eye == 0) ? ovrEye_Left : ovrEye_Right, hmdDesc.DefaultEyeFov[eye]);
192 hmdToEyeViewPose[eye] = eyeRenderDesc[eye].HmdToEyePose;
194 layer.ColorTexture[eye] = textureSwapChain[eye];
195 layer.Fov[eye] = eyeRenderDesc[eye].Fov;
196 layer.Viewport[eye].Pos.x = 0;
197 layer.Viewport[eye].Pos.y = 0;
198 layer.Viewport[eye].Size.w = TextureSize[eye].w;
199 layer.Viewport[eye].Size.h = TextureSize[eye].h;
202 ovrMirrorTexture mirrorTexture = NULL;
203 ovrMirrorTextureDesc desc;
204 memset(&desc, 0, sizeof(desc));
205 desc.Width = mirror_width = TextureSize[0].w;
206 desc.Height = mirror_height = TextureSize[0].h / 2;
207 desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
209 ovrMirrorOption_PostDistortion |
210 ovrMirrorOption_IncludeGuardian |
211 ovrMirrorOption_IncludeNotifications |
212 ovrMirrorOption_IncludeSystemGui;
214 /* Create mirror texture and an FBO used to copy mirror texture to back buffer */
215 result = ovr_CreateMirrorTextureWithOptionsGL(session, &desc, &mirrorTexture);
216 if (!OVR_SUCCESS(result)) {
217 print_error("ovr_CreateMirrorTextureWithOptionsGL() failed!\n");
221 /* Configure the mirror read buffer */
223 ovr_GetMirrorTextureBufferGL(session, mirrorTexture, &texId);
225 glGenFramebuffers(1, &mirrorFBO);
226 glBindFramebuffer(GL_READ_FRAMEBUFFER, mirrorFBO);
227 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
228 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
237 void begin_render_ovr(void)
241 /* Get both eye poses simultaneously, with IPD offset already included. */
242 double displayMidpointSeconds = ovr_GetPredictedDisplayTime(session, 0);
243 ovrTrackingState hmdState = ovr_GetTrackingState(session, displayMidpointSeconds, ovrTrue);
244 ovr_CalcEyePoses(hmdState.HeadPose.ThePose, hmdToEyeViewPose, layer.RenderPose);
246 result = ovr_WaitToBeginFrame(session, frameIndex);
247 if (!OVR_SUCCESS(result))
248 print_info("Failed to wait to begin frame (error %d)\n", result);
249 result = ovr_BeginFrame(session, frameIndex);
250 if (!OVR_SUCCESS(result))
251 print_info("Failed to begin frame (error %d)\n", result);
254 void begin_render_ovr_eye(int eye)
258 float yaw, pitch, roll;
261 /* set render surface */
262 ovr_GetTextureSwapChainCurrentIndex(session, textureSwapChain[eye], &curIndex);
263 ovr_GetTextureSwapChainBufferGL(session, textureSwapChain[eye], curIndex, &chainTexId);
264 glBindFramebuffer(GL_FRAMEBUFFER, fboId[eye]);
265 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, (multisampling > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, chainTexId, 0);
267 glViewport(0, 0, TextureSize[eye].w, TextureSize[eye].h);
268 glMatrixMode(GL_PROJECTION);
271 -layer.Fov[eye].LeftTan,
272 layer.Fov[eye].RightTan,
273 layer.Fov[eye].UpTan,
274 -layer.Fov[eye].DownTan,
277 ovrOrientation2yawpitchroll(layer.RenderPose[eye].Orientation, &yaw, &pitch, &roll);
278 x = layer.RenderPose[eye].Position.x;
279 y = layer.RenderPose[eye].Position.y;
280 z = layer.RenderPose[eye].Position.z;
282 /* normalize height to game's observer, if requrested by user */
284 x += observer_x_normalize;
286 y += observer_y_normalize;
288 z += observer_z_normalize;
290 glRotatef(-roll / M_PI * 180.0,0,0,1);
291 glRotatef(-pitch / M_PI * 180.0,1,0,0);
292 glRotatef(-yaw / M_PI * 180.0,0,1,0);
294 glTranslated(-x / 0.0254, -y / 0.0254, -z / 0.0254); /* convert to inch */
296 glMatrixMode(GL_MODELVIEW);
298 /* DO NOT ENABLE, since our mercenary-textures are not SRGB */
299 //glEnable(GL_FRAMEBUFFER_SRGB);
303 void end_render_ovr_eye(int eye)
305 // unset render surface
306 glBindFramebuffer(GL_FRAMEBUFFER, fboId[eye]);
307 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, (multisampling > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, 0, 0);
308 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, (multisampling > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, 0, 0);
309 glBindFramebuffer(GL_FRAMEBUFFER, 0);
311 // Commit the changes to the texture swap chain
312 ovr_CommitTextureSwapChain(session, textureSwapChain[eye]);
315 void end_render_ovr(void)
319 const ovrLayerHeader *layers[] = { &layer.Header };
320 result = ovr_EndFrame(session, frameIndex, NULL, layers, 1);
321 if (!OVR_SUCCESS(result))
322 print_info("Failed to submit frame (error %d)\n", result);
326 void render_mirror_ovr(int view_width, int view_height)
328 int view_x = 0, view_y = 0;
329 int new_width, new_height;
331 /* avoid division by zero, if one dimension is too small */
332 if (view_width < 1 || view_height < 1)
335 /* calculate a viewport that has apect of TextureSize */
336 if (view_height * mirror_width > view_width * mirror_height) {
337 new_height = view_width * mirror_height / mirror_width;
338 view_y = view_y + view_height / 2 - new_height / 2;
339 view_height = new_height;
340 } else if (view_height * mirror_width < view_width * mirror_height) {
341 new_width = view_height * mirror_width / mirror_height;
342 view_x = view_x + view_width / 2 - new_width / 2;
343 view_width = new_width;
346 /* avoid views that are too small */
347 if (view_width < 1 || view_height < 1)
350 glBindFramebuffer(GL_FRAMEBUFFER, 0);
352 glClearColor(0.0, 0.0, 0.0, 1.0);
353 glClear(GL_COLOR_BUFFER_BIT);
355 /* Blit mirror texture to back buffer */
356 glBindFramebuffer(GL_READ_FRAMEBUFFER, mirrorFBO);
357 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
358 glBlitFramebuffer(0, mirror_height, mirror_width, 0,
359 view_x, view_y, view_x + view_width, view_y + view_height,
360 GL_COLOR_BUFFER_BIT, GL_NEAREST);
361 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
364 void normalize_observer_ovr(void)
366 observer_x_normalize = -observer_x;
367 observer_y_normalize = -observer_y;
368 observer_z_normalize = -observer_z;
375 /* destroy render buffers */
376 for (eye = 0; eye < 2; eye++) {
377 if (textureSwapChain[eye]) {
378 ovr_DestroyTextureSwapChain(session, textureSwapChain[eye]);
379 textureSwapChain[eye] = NULL;
382 glDeleteFramebuffers(1, &fboId[eye]);
387 glDeleteFramebuffers(1, &mirrorFBO);
392 ovr_Destroy(session);
395 if (ovr_initialized) {