OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
OpenGLSample.cpp
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 // Internal Includes
28 #include <osvr/ClientKit/Display.h>
29 #include "SDL2Helpers.h"
30 #include "OpenGLCube.h"
31 
32 // Library/third-party includes
33 #include <SDL.h>
34 #include <SDL_opengl.h>
35 
36 // Standard includes
37 #include <iostream>
38 
39 static auto const WIDTH = 1920;
40 static auto const HEIGHT = 1080;
41 
42 // Forward declarations of rendering functions defined below.
44 void renderScene();
45 
46 int main(int argc, char *argv[]) {
47  namespace SDL = osvr::SDL2;
48 
49  // Open SDL
50  SDL::Lib lib;
51 
52  // Use OpenGL 2.1
53  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
54  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
55 
56  // Create a window
57  auto window = SDL::createWindow("OSVR", SDL_WINDOWPOS_UNDEFINED,
58  SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT,
59  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
60  if (!window) {
61  std::cerr << "Could not create window: " << SDL_GetError() << std::endl;
62  return -1;
63  }
64 
65  // Create an OpenGL context and make it current.
66  SDL::GLContext glctx(window.get());
67 
68  // Turn on V-SYNC
69  SDL_GL_SetSwapInterval(1);
70 
71  // Start OSVR and get OSVR display config
72  osvr::clientkit::ClientContext ctx("com.osvr.example.SDLOpenGL");
73  osvr::clientkit::DisplayConfig display(ctx);
74  if (!display.valid()) {
75  std::cerr << "\nCould not get display config (server probably not "
76  "running or not behaving), exiting."
77  << std::endl;
78  return -1;
79  }
80 
81  std::cout << "Waiting for the display to fully start up, including "
82  "receiving initial pose update..."
83  << std::endl;
84  while (!display.checkStartup()) {
85  ctx.update();
86  }
87  std::cout << "OK, display startup status is good!" << std::endl;
88 
89  // Event handler
90  SDL_Event e;
91 #ifndef __ANDROID__ // Don't want to pop up the on-screen keyboard
92  SDL::TextInput textinput;
93 #endif
94  bool quit = false;
95  while (!quit) {
96  // Handle all queued events
97  while (SDL_PollEvent(&e)) {
98  switch (e.type) {
99  case SDL_QUIT:
100  // Handle some system-wide quit event
101  quit = true;
102  break;
103  case SDL_KEYDOWN:
104  if (SDL_SCANCODE_ESCAPE == e.key.keysym.scancode) {
105  // Handle pressing ESC
106  quit = true;
107  }
108  break;
109  }
110  if (e.type == SDL_QUIT) {
111  quit = true;
112  }
113  }
114 
115  // Update OSVR
116  ctx.update();
117 
118  // Render
119  render(display);
120 
121  // Swap buffers
122  SDL_GL_SwapWindow(window.get());
123  }
124 
125  return 0;
126 }
127 
132 void renderScene() { draw_cube(1.0); }
133 
140 
141  // Clear the screen to black and clear depth
142  glClearColor(0, 0, 0, 1.0f);
143  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144 
146  disp.forEachEye([](osvr::clientkit::Eye eye) {
147 
149  double viewMat[OSVR_MATRIX_SIZE];
151  viewMat);
154  glMatrixMode(GL_MODELVIEW);
155  glLoadIdentity();
156  glMultMatrixd(viewMat);
157 
160  eye.forEachSurface([](osvr::clientkit::Surface surface) {
161  auto viewport = surface.getRelativeViewport();
162  glViewport(static_cast<GLint>(viewport.left),
163  static_cast<GLint>(viewport.bottom),
164  static_cast<GLsizei>(viewport.width),
165  static_cast<GLsizei>(viewport.height));
166 
169  double zNear = 0.1;
170  double zFar = 100;
171  double projMat[OSVR_MATRIX_SIZE];
172  surface.getProjectionMatrix(
175  projMat);
176 
177  glMatrixMode(GL_PROJECTION);
178  glLoadIdentity();
179  glMultMatrixd(projMat);
180 
183  glMatrixMode(GL_MODELVIEW);
184 
186  renderScene();
187  });
188  });
189 
191 }
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.
Matrix transforms column vectors (default)
Column-major memory order (default)
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
void render(osvr::clientkit::DisplayConfig &disp)
The "wrapper" for rendering to a device described by OSVR.
Header.
Matrix takes vectors from a right-handed coordinate system (default)
Class wrapping OSVR_DisplayConfig objects, optionally managing shared ownership.
Definition: Display.h:409
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)
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