JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
GPUtexture.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19
20// ####################################################################################################
21jevois::GPUtexture::GPUtexture(GLsizei width, GLsizei height, GLenum format, bool createFramebuffer) :
22 Width(width), Height(height), Format(format), Id(0), FramebufferId(0), RenderBufferId(0)
23{
24 GL_CHECK(glGenTextures(1, &Id));
25 GL_CHECK(glBindTexture(GL_TEXTURE_2D, Id));
26 GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, Format, Width, Height, 0, Format, GL_UNSIGNED_BYTE, nullptr));
27 GL_CHECK(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLfloat(GL_NEAREST)));
28 GL_CHECK(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLfloat(GL_NEAREST)));
29 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
30 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
31 GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
32
33 if (createFramebuffer)
34 {
35 // Optional: create framebuffer
36 GL_CHECK(glGenFramebuffers(1, &FramebufferId));
37 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, FramebufferId));
38
39 // Optional: create render buffer
40 GL_CHECK(glGenRenderbuffers(1, &RenderBufferId));
41 GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, RenderBufferId));
42 GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, Width, Height));
43 GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, RenderBufferId));
44
45 GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Id, 0));
46
47 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
48 LERROR("Framebuffer creation failed");
49 }
50
51 LDEBUG("Created " << width << 'x' << height << " texture (id=" << Id << ", fb=" << FramebufferId
52 << ", rb=" << RenderBufferId << ')');
53}
54
55// ####################################################################################################
57{
58 if (RenderBufferId) glDeleteRenderbuffers(1, &RenderBufferId);
59 if (FramebufferId) glDeleteFramebuffers(1, &FramebufferId);
60 glDeleteTextures(1, &Id);
61}
62
63// ####################################################################################################
64void jevois::GPUtexture::setPixels(void const * data)
65{
66 GL_CHECK(glBindTexture(GL_TEXTURE_2D, Id));
67
68 // Try to use the largest possible alignment to speedup the upload. We need the row length to be a multiple of the
69 // alignment since opengl will pad each row to that alignment:
70 int const rowlen = Width * (Format == GL_RGBA ? 4 : 1);
71 int align;
72 if ((rowlen & 7) == 0) align = 8;
73 else if ((rowlen & 3) == 0) align = 4;
74 else if ((rowlen & 1) == 0) align = 2;
75 else align = 1;
76
77 glPixelStorei(GL_UNPACK_ALIGNMENT, align);
78 static void * olddata = nullptr;
79
80 if (data != olddata)
81 GL_CHECK(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Width, Height, Format, GL_UNSIGNED_BYTE, data));
82 //GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, Format, Width, Height, 0, Format, GL_UNSIGNED_BYTE, data));
83 GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
84}
85
86// ####################################################################################################
87void jevois::GPUtexture::getPixels(void * data) const
88{
89 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, FramebufferId));
90 GL_CHECK(glReadPixels(0, 0, Width, Height, Format, GL_UNSIGNED_BYTE, data));
91 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
92}
93
#define GL_CHECK(stmt)
Simple macro to check for OpenGL errors.
Definition OpenGL.H:77
void setPixels(void const *data)
Copy pixel data from memory location to the texture.
Definition GPUtexture.C:64
GLenum const Format
Definition GPUtexture.H:51
void getPixels(void *data) const
Copy pixel data from the texture to already-allocated memory location.
Definition GPUtexture.C:87
GLsizei const Width
Definition GPUtexture.H:49
GLsizei const Height
Definition GPUtexture.H:50
~GPUtexture()
Destructor, frees the texture in OpenGL.
Definition GPUtexture.C:56
GPUtexture(GLsizei width, GLsizei height, GLenum format, bool createFramebuffer)
Constructor.
Definition GPUtexture.C:21
#define LDEBUG(msg)
Convenience macro for users to print out console or syslog messages, DEBUG level.
Definition Log.H:173
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211