OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
OpenGLSampleJoint.cpp
Go to the documentation of this file.
1 
13 // Copyright 2015 Sensics, Inc.
14 //
15 // Licensed under the Apache License, Version 2.0 (the "License");
16 // you may not use this file except in compliance with the License.
17 // You may obtain a copy of the License at
18 //
19 // http://www.apache.org/licenses/LICENSE-2.0
20 //
21 // Unless required by applicable law or agreed to in writing, software
22 // distributed under the License is distributed on an "AS IS" BASIS,
23 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 // See the License for the specific language governing permissions and
25 // limitations under the License.
26 
27 // Internal Includes
30 #include <osvr/ClientKit/Display.h>
31 #include "SDL2Helpers.h"
32 #include "OpenGLCube.h"
33 
34 // Library/third-party includes
35 #include <SDL.h>
36 #include <SDL_opengl.h>
37 
38 // Standard includes
39 #include <iostream>
40 
41 static auto const WIDTH = 1920;
42 static auto const HEIGHT = 1080;
43 
44 // Forward declarations of rendering functions defined below.
46 void renderScene();
47 
48 int main(int argc, char *argv[]) {
49  namespace SDL = osvr::SDL2;
50 
51  // Open SDL
52  SDL::Lib lib;
53 
54  // Use OpenGL 2.1
55  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
56  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
57 
58  // Create a window
59  auto window = SDL::createWindow("OSVR", SDL_WINDOWPOS_UNDEFINED,
60  SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT,
61  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
62  if (!window) {
63  std::cerr << "Could not create window: " << SDL_GetError() << std::endl;
64  return -1;
65  }
66 
67  // Create an OpenGL context and make it current.
68  SDL::GLContext glctx(window.get());
69 
70  // Turn on V-SYNC
71  SDL_GL_SetSwapInterval(1);
72 
73  //--- BEGIN JointClientKit-specific code ---//
74  // Prepare JointClientKit options: just an auto-load and a hardware detect
75  // for this sample. (Thus, only fully-autoconfigurable systems like the HDK
76  // will work with this sample.)
80 
81  // Now start it up and get the context
82  OSVR_ClientContext rawCtx =
83  osvrJointClientInit("com.osvr.example.SDLOpenGLJoint", opts);
84  if (!rawCtx) {
85  std::cerr << "Error starting up the joint client/server" << std::endl;
86  return -1;
87  }
88 
89  // Wrap the context in the C++ API's client context object, which will take
90  // ownership of it.
92 
93  // Update it once just to get things going.
94  ctx.update();
95 
96  //--- END JointClientKit-specific code ---//
97  // Past here, everything is the same as in OpenGLSample.cpp
98  // Start OSVR and get OSVR display config
99  osvr::clientkit::DisplayConfig display(ctx);
100  if (!display.valid()) {
101  std::cerr << "\nCould not get display config, exiting." << std::endl;
102  return -1;
103  }
104 
105  std::cout << "Waiting for the display to fully start up, including "
106  "receiving initial pose update..."
107  << std::endl;
108  while (!display.checkStartup()) {
109  ctx.update();
110  }
111  std::cout << "OK, display startup status is good!" << std::endl;
112 
113  // Event handler
114  SDL_Event e;
115 #ifndef __ANDROID__ // Don't want to pop up the on-screen keyboard
116  SDL::TextInput textinput;
117 #endif
118  bool quit = false;
119  while (!quit) {
120  // Handle all queued events
121  while (SDL_PollEvent(&e)) {
122  switch (e.type) {
123  case SDL_QUIT:
124  // Handle some system-wide quit event
125  quit = true;
126  break;
127  case SDL_KEYDOWN:
128  if (SDL_SCANCODE_ESCAPE == e.key.keysym.scancode) {
129  // Handle pressing ESC
130  quit = true;
131  }
132  break;
133  }
134  if (e.type == SDL_QUIT) {
135  quit = true;
136  }
137  }
138 
139  // Update OSVR
140  ctx.update();
141 
142  // Render
143  render(display);
144 
145  // Swap buffers
146  SDL_GL_SwapWindow(window.get());
147  }
148 
149  return 0;
150 }
151 
156 void renderScene() { draw_cube(1.0); }
157 
164 
165  // Clear the screen to black and clear depth
166  glClearColor(0, 0, 0, 1.0f);
167  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
168 
170  disp.forEachEye([](osvr::clientkit::Eye eye) {
171 
173  double viewMat[OSVR_MATRIX_SIZE];
175  viewMat);
178  glMatrixMode(GL_MODELVIEW);
179  glLoadIdentity();
180  glMultMatrixd(viewMat);
181 
184  eye.forEachSurface([](osvr::clientkit::Surface surface) {
185  auto viewport = surface.getRelativeViewport();
186  glViewport(static_cast<GLint>(viewport.left),
187  static_cast<GLint>(viewport.bottom),
188  static_cast<GLsizei>(viewport.width),
189  static_cast<GLsizei>(viewport.height));
190 
193  double zNear = 0.1;
194  double zFar = 100;
195  double projMat[OSVR_MATRIX_SIZE];
196  surface.getProjectionMatrix(
199  projMat);
200 
201  glMatrixMode(GL_PROJECTION);
202  glLoadIdentity();
203  glMultMatrixd(projMat);
204 
207  glMatrixMode(GL_MODELVIEW);
208 
210  renderScene();
211  });
212  });
213 
215 }
void draw_cube(double radius)
Fixed-function pipeline OpenGL code to draw a cube.
Definition: OpenGLCube.h:29
void forEachSurface(F OSVR_CLIENTKIT_FUNCTOR_REF functor)
Definition: Display.h:308
Constant for the number of elements in the matrices we use - 4x4.
Client context object: Create and keep one in your application. Handles lifetime management and provi...
Definition: Context_decl.h:57
Header.
Wrapper for a viewer, eye, and surface bound to a display config. DOES NOT provide lifetime managemen...
Definition: Display.h:117
bool getViewMatrix(OSVR_MatrixConventions flags, double mat[OSVR_MATRIX_SIZE])
Attempt to get the view matrix.
Definition: Display.h:290
Header containing some helper code for using SDL2 in a C++11 environment.
OSVR_JointClientOpts osvrJointClientCreateOptions()
Creates an empty OSVR_JointClientOpts.
Matrix transforms column vectors (default)
Column-major memory order (default)
void render(osvr::clientkit::DisplayConfig &disp)
The "wrapper" for rendering to a device described by OSVR.
OSVR_ClientContext osvrJointClientInit(const char applicationIdentifier[], OSVR_JointClientOpts opts)
Initialize the library, starting up a "joint" context that also contains a server.
void forEachEye(F OSVR_CLIENTKIT_FUNCTOR_REF functor)
Definition: Display.h:533
void getProjectionMatrix(double near, double far, OSVR_MatrixConventions flags, double matrix[OSVR_MATRIX_SIZE]) const
Gets the projection matrix.
Definition: Display.h:143
Header.
Wrapper for a viewer and eye bound to a display config. DOES NOT provide lifetime management for the ...
Definition: Display.h:248
struct OSVR_JointClientContextOptsObject * OSVR_JointClientOpts
Opaque typedef for options used when starting up a joint client context. Serves as a queue for config...
struct OSVR_ClientContextObject * OSVR_ClientContext
Opaque handle that should be retained by your application. You need only and exactly one...
Header.
OSVR_ReturnCode osvrJointClientOptionsTriggerHardwareDetect(OSVR_JointClientOpts opts)
Queues up a trigger for hardware detection.
Matrix takes vectors from a right-handed coordinate system (default)
OSVR_ReturnCode osvrJointClientOptionsAutoloadPlugins(OSVR_JointClientOpts opts)
Queues up the autoloading of plugins. May only be called once per options object. ...
void renderScene()
A simple dummy "draw" function - note that drawing occurs in "room space" by default. (that is, in this example, the modelview matrix when this function is called is initialized such that it transforms from world space to view space)
Class wrapping OSVR_DisplayConfig objects, optionally managing shared ownership.
Definition: Display.h:409
Matrix maps the near and far planes to signed Z values (in the range [-1, 1]) (default) ...
RelativeViewport getRelativeViewport() const
Gets the video-input-relative viewport corresponding to this surface.
Definition: Display.h:127