forked from vrui-vr/arsandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureTracker.cpp
More file actions
95 lines (76 loc) · 2.88 KB
/
Copy pathTextureTracker.cpp
File metadata and controls
95 lines (76 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/***********************************************************************
TextureTracker - Helper class to track which texture objects are
currently bound to which texture targets on the OpenGL context's texture
units.
Copyright (c) 2023 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "TextureTracker.h"
#include <stdexcept>
#include <GL/gl.h>
#include <GL/Extensions/GLARBMultitexture.h>
#include <GL/Extensions/GLARBVertexProgram.h>
/*******************************
Methods of class TextureTracker:
*******************************/
void TextureTracker::initExtensions(void)
{
GLARBMultitexture::initExtension();
GLARBVertexProgram::initExtension();
}
TextureTracker::TextureTracker(void)
:numUnits(0),bindings(0),
numActiveUnits(0),nextUnit(0)
{
/* Query the number of available texture units in the current OpenGL context: */
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB,&numUnits);
/* Allocate the bindings array: */
bindings=new Binding[numUnits];
}
TextureTracker::~TextureTracker(void)
{
/* Unbind all bound textures: */
for(GLint i=0;i<numActiveUnits;++i)
{
glActiveTextureARB(GL_TEXTURE0_ARB+i);
glBindTexture(bindings[i].target,0U);
}
/* Reset the active texture unit: */
glActiveTextureARB(GL_TEXTURE0_ARB);
/* Release the bindings array: */
delete[] bindings;
}
void TextureTracker::reset(void)
{
/* Start binding from the first texture unit again: */
nextUnit=0;
}
GLint TextureTracker::bindTexture(GLenum target,GLuint texture)
{
GLint result=nextUnit;
/* Check if there is another texture unit available: */
if(nextUnit>=numUnits)
throw std::runtime_error("TextureTracker::bindTexture: No more available texture units");
/* Bind the texture to the next texture unit: */
glActiveTextureARB(GL_TEXTURE0_ARB+nextUnit);
glBindTexture(target,texture);
bindings[nextUnit].target=target;
bindings[nextUnit].texture=texture;
++nextUnit;
/* Update the number of active texture units: */
if(numActiveUnits<nextUnit)
numActiveUnits=nextUnit;
/* Return the used texture unit and move to the next one: */
return result;
}