YARP
Yet Another Robot Platform
TextureStatic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "TextureStatic.h"
20 #include "GLDebug.h"
21 #include "OVRHeadsetLogComponent.h"
22 
23 #include <yarp/os/LogStream.h>
24 
25 #include <OVR_CAPI_GL.h>
26 
27 TextureStatic::TextureStatic(ovrSession session, const TextureStatic::Image &img) :
28  session(session),
29  textureSwapChain(nullptr),
30  width(img.width),
31  height(img.height),
32  bytes_per_pixel(img.bytes_per_pixel),
33  // see http://stackoverflow.com/questions/27294882/glteximage2d-fails-with-error-1282-using-pbo-bad-texture-resolution
34  padding((4 - (width * bytes_per_pixel) % 4) % 4),
35  rowSize(width * bytes_per_pixel + padding),
36  bufferSize(rowSize * height),
37  ptr((GLubyte*)img.pixel_data)
38 {
39  createTexture();
40 }
41 
43 {
44  deleteTexture();
45 }
46 
47 void TextureStatic::createTexture()
48 {
49  ovrTextureSwapChainDesc desc = {};
50  desc.Type = ovrTexture_2D;
51  desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
52  desc.ArraySize = 1;
53  desc.Width = width;
54  desc.Height = height;
55  desc.MipLevels = 1;
56  desc.SampleCount = 1;
57  desc.StaticImage = ovrTrue;
58  desc.MiscFlags = ovrTextureMisc_None;
59  desc.BindFlags = ovrTextureBind_None;
60 
61  if (ovr_CreateTextureSwapChainGL(session, &desc, &textureSwapChain) != ovrSuccess) {
62  yCFatal(OVRHEADSET) << "Failed to create texture swap chain";
63  }
64 
65  ovr_GetTextureSwapChainBufferGL(session, textureSwapChain, -1, &chainTexId);
67 
68  glBindTexture(GL_TEXTURE_2D, chainTexId);
69  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
70  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
72  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
73  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
75 
76  glBindTexture(GL_TEXTURE_2D, 0);
77 
78  ovr_CommitTextureSwapChain(session, textureSwapChain);
80 }
81 
82 void TextureStatic::deleteTexture()
83 {
84  ovr_DestroyTextureSwapChain(session, textureSwapChain);
85 }
LogStream.h
OVRHeadsetLogComponent.h
OVRHEADSET
const yarp::os::LogComponent & OVRHEADSET()
Definition: OVRHeadsetLogComponent.cpp:11
TextureStatic::width
unsigned int width
Definition: TextureStatic.h:42
checkGlErrorMacro
#define checkGlErrorMacro
Definition: GLDebug.h:32
TextureStatic.h
TextureStatic::textureSwapChain
ovrTextureSwapChain textureSwapChain
Definition: TextureStatic.h:40
TextureStatic::chainTexId
GLuint chainTexId
Definition: TextureStatic.h:49
TextureStatic::ptr
GLubyte * ptr
Definition: TextureStatic.h:52
TextureStatic::TextureStatic
TextureStatic(ovrSession session, const TextureStatic::Image &img)
Definition: TextureStatic.cpp:27
TextureStatic::~TextureStatic
~TextureStatic()
Definition: TextureStatic.cpp:42
TextureStatic::session
ovrSession session
Definition: TextureStatic.h:39
GLDebug.h
TextureStatic::Image
Definition: TextureStatic.h:29
TextureStatic::height
unsigned int height
Definition: TextureStatic.h:43
yCFatal
#define yCFatal(component,...)
Definition: LogComponent.h:168