YARP
Yet Another Robot Platform
TextureLogo.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 "TextureLogo.h"
20 #include "GLDebug.h"
21 #include "OVRHeadsetLogComponent.h"
22 
23 #include <yarp/os/LogStream.h>
24 #include <yarp/os/Time.h>
25 
26 #include <OVR_CAPI_GL.h>
27 
28 #include "img-yarp-robot-64.h"
29 #include "img-crosshairs.h"
30 
31 TextureLogo::TextureLogo(ovrSession session) :
32  session(session),
33  textureSwapChain(nullptr),
34  width(yarp_logo.width),
35  height(yarp_logo.height),
36  bytes_per_pixel(yarp_logo.bytes_per_pixel),
37  // see http://stackoverflow.com/questions/27294882/glteximage2d-fails-with-error-1282-using-pbo-bad-texture-resolution
38  padding((4 - (width * bytes_per_pixel) % 4) % 4),
39  rowSize(width * bytes_per_pixel + padding),
40  bufferSize(rowSize * height),
41  ptr((GLubyte*)yarp_logo.pixel_data)
42 {
44  createTexture();
45 }
46 
48 {
49  deleteTexture();
50 }
51 
52 void TextureLogo::createTexture()
53 {
54  ovrTextureSwapChainDesc desc = {};
55  desc.Type = ovrTexture_2D;
56  desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
57  desc.ArraySize = 1;
58  desc.Width = width;
59  desc.Height = height;
60  desc.MipLevels = 1;
61  desc.SampleCount = 1;
62  desc.StaticImage = ovrTrue;
63  desc.MiscFlags = ovrTextureMisc_None;
64  desc.BindFlags = ovrTextureBind_None;
65 
66  if (ovr_CreateTextureSwapChainGL(session, &desc, &textureSwapChain) != ovrSuccess) {
67  yCFatal(OVRHEADSET) << "Failed to create texture swap chain";
68  }
69 
70  ovr_GetTextureSwapChainBufferGL(session, textureSwapChain, -1, &chainTexId);
72 
73  glBindTexture(GL_TEXTURE_2D, chainTexId);
74  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
75  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
77  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
78  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
80 
81  glBindTexture(GL_TEXTURE_2D, 0);
82 
83  ovr_CommitTextureSwapChain(session, textureSwapChain);
85 }
86 
87 void TextureLogo::deleteTexture()
88 {
89  ovr_DestroyTextureSwapChain(session, textureSwapChain);
90 }
91 
92 
93 
94 
96  session(session),
97  textureSwapChain(nullptr),
98  width(crosshairs.width),
99  height(crosshairs.height),
100  bytes_per_pixel(crosshairs.bytes_per_pixel),
101  // see http://stackoverflow.com/questions/27294882/glteximage2d-fails-with-error-1282-using-pbo-bad-texture-resolution
102  padding((4 - (width * bytes_per_pixel) % 4) % 4),
103  rowSize(width * bytes_per_pixel + padding),
104  bufferSize(rowSize * height),
105  ptr((GLubyte*)crosshairs.pixel_data)
106 {
108  createTexture();
109 }
110 
112 {
113  deleteTexture();
114 }
115 
116 void TextureCrosshairs::createTexture()
117 {
118  ovrTextureSwapChainDesc desc = {};
119  desc.Type = ovrTexture_2D;
120  desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
121  desc.ArraySize = 1;
122  desc.Width = width;
123  desc.Height = height;
124  desc.MipLevels = 1;
125  desc.SampleCount = 1;
126  desc.StaticImage = ovrTrue;
127  desc.MiscFlags = ovrTextureMisc_None;
128  desc.BindFlags = ovrTextureBind_None;
129 
130  if (ovr_CreateTextureSwapChainGL(session, &desc, &textureSwapChain) != ovrSuccess) {
131  yCFatal(OVRHEADSET) << "Failed to create texture swap chain";
132  }
133 
134  ovr_GetTextureSwapChainBufferGL(session, textureSwapChain, -1, &chainTexId);
136 
137  glBindTexture(GL_TEXTURE_2D, chainTexId);
138  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
140  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
141  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
142  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
144 
145  glBindTexture(GL_TEXTURE_2D, 0);
146 
147  ovr_CommitTextureSwapChain(session, textureSwapChain);
149 }
150 
151 void TextureCrosshairs::deleteTexture()
152 {
153  ovr_DestroyTextureSwapChain(session, textureSwapChain);
154 }
LogStream.h
OVRHeadsetLogComponent.h
TextureCrosshairs::chainTexId
GLuint chainTexId
Definition: TextureLogo.h:79
TextureCrosshairs::TextureCrosshairs
TextureCrosshairs(ovrSession session)
Definition: TextureLogo.cpp:95
OVRHEADSET
const yarp::os::LogComponent & OVRHEADSET()
Definition: OVRHeadsetLogComponent.cpp:11
TextureLogo::chainTexId
GLuint chainTexId
Definition: TextureLogo.h:51
TextureLogo::~TextureLogo
~TextureLogo()
Definition: TextureLogo.cpp:47
checkGlErrorMacro
#define checkGlErrorMacro
Definition: GLDebug.h:32
TextureCrosshairs::textureSwapChain
ovrTextureSwapChain textureSwapChain
Definition: TextureLogo.h:70
TextureCrosshairs::session
ovrSession session
Definition: TextureLogo.h:69
TextureCrosshairs::ptr
GLubyte * ptr
Definition: TextureLogo.h:82
img-yarp-robot-64.h
TextureLogo.h
TextureLogo::textureSwapChain
ovrTextureSwapChain textureSwapChain
Definition: TextureLogo.h:42
crosshairs
const TextureStatic::Image crosshairs
Definition: img-crosshairs.h:25
TextureLogo::height
unsigned int height
Definition: TextureLogo.h:45
TextureCrosshairs::width
unsigned int width
Definition: TextureLogo.h:72
TextureLogo::ptr
GLubyte * ptr
Definition: TextureLogo.h:54
TextureLogo::TextureLogo
TextureLogo(ovrSession session)
Definition: TextureLogo.cpp:31
img-crosshairs.h
TextureLogo::session
ovrSession session
Definition: TextureLogo.h:41
TextureCrosshairs::height
unsigned int height
Definition: TextureLogo.h:73
TextureCrosshairs::~TextureCrosshairs
~TextureCrosshairs()
Definition: TextureLogo.cpp:111
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
Time.h
GLDebug.h
TextureLogo::width
unsigned int width
Definition: TextureLogo.h:44
yarp_logo
const TextureStatic::Image yarp_logo
Definition: img-yarp-robot-64.h:25
yCFatal
#define yCFatal(component,...)
Definition: LogComponent.h:168