OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
OpenGLSampleCAPI.cpp
Go to the documentation of this file.
1 
14 // Copyright 2015 Sensics, Inc.
15 //
16 // Licensed under the Apache License, Version 2.0 (the "License");
17 // you may not use this file except in compliance with the License.
18 // You may obtain a copy of the License at
19 //
20 // http://www.apache.org/licenses/LICENSE-2.0
21 //
22 // Unless required by applicable law or agreed to in writing, software
23 // distributed under the License is distributed on an "AS IS" BASIS,
24 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 // See the License for the specific language governing permissions and
26 // limitations under the License.
27 
28 // Internal Includes
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.
45 void render(OSVR_DisplayConfig disp);
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  // Start OSVR and get OSVR display config
74  osvr::clientkit::ClientContext ctx("com.osvr.example.SDLOpenGL");
75  OSVR_DisplayConfig display;
76  auto ret = osvrClientGetDisplay(ctx.get(), &display);
77  if (ret != OSVR_RETURN_SUCCESS) {
78  std::cerr << "\nCould not get display config (server probably not "
79  "running or not behaving), exiting."
80  << std::endl;
81  return -1;
82  }
83 
84  std::cout << "Waiting for the display to fully start up, including "
85  "receiving initial pose update..."
86  << std::endl;
88  ctx.update();
89  }
90  std::cout << "OK, display startup status is good!" << std::endl;
91 
92  // Event handler
93  SDL_Event e;
94 #ifndef __ANDROID__ // Don't want to pop up the on-screen keyboard
95  SDL::TextInput textinput;
96 #endif
97  bool quit = false;
98  while (!quit) {
99  // Handle all queued events
100  while (SDL_PollEvent(&e)) {
101  switch (e.type) {
102  case SDL_QUIT:
103  // Handle some system-wide quit event
104  quit = true;
105  break;
106  case SDL_KEYDOWN:
107  if (SDL_SCANCODE_ESCAPE == e.key.keysym.scancode) {
108  // Handle pressing ESC
109  quit = true;
110  }
111  break;
112  }
113  if (e.type == SDL_QUIT) {
114  quit = true;
115  }
116  }
117 
118  // Update OSVR
119  ctx.update();
120 
121  // Render
122  render(display);
123 
124  // Swap buffers
125  SDL_GL_SwapWindow(window.get());
126  }
127 
128  return 0;
129 }
130 
135 void renderScene() { draw_cube(1.0); }
136 
144  OSVR_ViewerCount viewers;
145  osvrClientGetNumViewers(disp, &viewers);
146 
147  // Clear the screen to black and clear depth
148  glClearColor(0, 0, 0, 1.0f);
149  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
150 
151  for (OSVR_ViewerCount viewer = 0; viewer < viewers; ++viewer) {
152 
154  OSVR_EyeCount eyes;
155  osvrClientGetNumEyesForViewer(disp, viewer, &eyes);
156  for (OSVR_EyeCount eye = 0; eye < eyes; ++eye) {
157 
159  double viewMat[OSVR_MATRIX_SIZE];
161  disp, viewer, eye,
163 
166  glMatrixMode(GL_MODELVIEW);
167  glLoadIdentity();
168  glMultMatrixd(viewMat);
169 
172  OSVR_SurfaceCount surfaces;
173  osvrClientGetNumSurfacesForViewerEye(disp, viewer, eye, &surfaces);
174  for (OSVR_SurfaceCount surface = 0; surface < surfaces; ++surface) {
175 
178  OSVR_ViewportDimension bottom;
180  OSVR_ViewportDimension height;
182  disp, viewer, eye, surface, &left, &bottom, &width,
183  &height);
184 
185  glViewport(static_cast<GLint>(left), static_cast<GLint>(bottom),
186  static_cast<GLsizei>(width),
187  static_cast<GLsizei>(height));
188 
191  double zNear = 0.1;
192  double zFar = 100;
193  double projMat[OSVR_MATRIX_SIZE];
195  disp, viewer, eye, surface, zNear, zFar,
198  projMat);
199  glMatrixMode(GL_PROJECTION);
200  glLoadIdentity();
201  glMultMatrixd(projMat);
202 
205  glMatrixMode(GL_MODELVIEW);
206 
208  renderScene();
209  }
210  }
211  }
213 }
void draw_cube(double radius)
Fixed-function pipeline OpenGL code to draw a cube.
Definition: OpenGLCube.h:29
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
uint32_t OSVR_SurfaceCount
The integer type specifying the number of surfaces seen by a viewer's eye.
OSVR_ReturnCode osvrClientCheckDisplayStartup(OSVR_DisplayConfig disp)
Checks to see if a display is fully configured and ready, including having received its first pose up...
Header.
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)
Header containing some helper code for using SDL2 in a C++11 environment.
Matrix transforms column vectors (default)
Column-major memory order (default)
OSVR_ReturnCode osvrClientGetViewerEyeViewMatrixd(OSVR_DisplayConfig disp, OSVR_ViewerCount viewer, OSVR_EyeCount eye, OSVR_MatrixConventions flags, double *mat)
Get the view matrix (inverse of pose) for the given eye of a viewer in a display config - matrix of d...
OSVR_ReturnCode osvrClientGetRelativeViewportForViewerEyeSurface(OSVR_DisplayConfig disp, OSVR_ViewerCount viewer, OSVR_EyeCount eye, OSVR_SurfaceCount surface, OSVR_ViewportDimension *left, OSVR_ViewportDimension *bottom, OSVR_ViewportDimension *width, OSVR_ViewportDimension *height)
Get the dimensions/location of the viewport within the display input for a surface seen by an eye of ...
Header.
OSVR_ReturnCode osvrClientGetNumSurfacesForViewerEye(OSVR_DisplayConfig disp, OSVR_ViewerCount viewer, OSVR_EyeCount eye, OSVR_SurfaceCount *surfaces)
Each eye of each viewer in a display config has one or more surfaces (aka "screens") on which content...
OSVR_ReturnCode osvrClientGetNumEyesForViewer(OSVR_DisplayConfig disp, OSVR_ViewerCount viewer, OSVR_EyeCount *eyes)
Each viewer in a display config can have one or more "eyes" which have a substantially similar pose: ...
OSVR_ReturnCode osvrClientGetViewerEyeSurfaceProjectionMatrixd(OSVR_DisplayConfig disp, OSVR_ViewerCount viewer, OSVR_EyeCount eye, OSVR_SurfaceCount surface, double near, double far, OSVR_MatrixConventions flags, double *matrix)
Get the projection matrix for a surface seen by an eye of a viewer in a display config. (double version)
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
uint32_t OSVR_ViewerCount
The integer type specifying a number of viewers in a system.
Header.
OSVR_ReturnCode osvrClientGetDisplay(OSVR_ClientContext ctx, OSVR_DisplayConfig *disp)
Allocates a display configuration object populated with data from the OSVR system.
void render(OSVR_DisplayConfig disp)
The "wrapper" for rendering to a device described by OSVR.
Matrix takes vectors from a right-handed coordinate system (default)
OSVR_ReturnCode osvrClientGetNumViewers(OSVR_DisplayConfig disp, OSVR_ViewerCount *viewers)
A display config can have one (or theoretically more) viewers: retrieve the viewer count...
uint8_t OSVR_EyeCount
The integer type specifying the number of eyes (viewpoints) of a viewer.
int32_t OSVR_ViewportDimension
The integer type used in specification of size or location of a viewport.
Matrix maps the near and far planes to signed Z values (in the range [-1, 1]) (default) ...
struct OSVR_DisplayConfigObject * OSVR_DisplayConfig
Opaque type of a display configuration.
Definition: DisplayC.h:57