[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[953] | 2 | /* |
---|
| 3 | * ---------------------------------------------------------------------- |
---|
| 4 | * Texture2D.h: 2d texture class |
---|
| 5 | * |
---|
| 6 | * ====================================================================== |
---|
| 7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
| 8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
| 9 | * |
---|
[3502] | 10 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
[953] | 11 | * |
---|
| 12 | * See the file "license.terms" for information on usage and |
---|
| 13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 14 | * ====================================================================== |
---|
| 15 | */ |
---|
| 16 | #include <stdio.h> |
---|
| 17 | #include <assert.h> |
---|
| 18 | #include <math.h> |
---|
| 19 | |
---|
| 20 | #include "config.h" |
---|
| 21 | |
---|
[3492] | 22 | #include "Texture2D.h" |
---|
| 23 | #include "Trace.h" |
---|
| 24 | |
---|
[4889] | 25 | using namespace nv; |
---|
| 26 | |
---|
[2831] | 27 | Texture2D::Texture2D() : |
---|
[2857] | 28 | _width(0), |
---|
| 29 | _height(0), |
---|
| 30 | _numComponents(3), |
---|
| 31 | _glResourceAllocated(false), |
---|
| 32 | _id(0), |
---|
| 33 | _type(GL_FLOAT), |
---|
| 34 | _interpType(GL_LINEAR), |
---|
| 35 | _wrapS(GL_CLAMP_TO_EDGE), |
---|
| 36 | _wrapT(GL_CLAMP_TO_EDGE) |
---|
[2831] | 37 | {} |
---|
[953] | 38 | |
---|
[2831] | 39 | Texture2D::Texture2D(int width, int height, |
---|
| 40 | GLuint type, GLuint interp, |
---|
| 41 | int numComponents, void *data) : |
---|
[2857] | 42 | _width(width), |
---|
| 43 | _height(height), |
---|
| 44 | _numComponents(numComponents), |
---|
| 45 | _glResourceAllocated(false), |
---|
| 46 | _id(0), |
---|
| 47 | _type(type), |
---|
| 48 | _interpType(interp), |
---|
| 49 | _wrapS(GL_CLAMP_TO_EDGE), |
---|
| 50 | _wrapT(GL_CLAMP_TO_EDGE) |
---|
[953] | 51 | { |
---|
[2831] | 52 | if (data != NULL) |
---|
[953] | 53 | initialize(data); |
---|
| 54 | } |
---|
| 55 | |
---|
[2831] | 56 | Texture2D::~Texture2D() |
---|
[953] | 57 | { |
---|
[2857] | 58 | glDeleteTextures(1, &_id); |
---|
[2831] | 59 | } |
---|
[953] | 60 | |
---|
[2831] | 61 | GLuint Texture2D::initialize(void *data) |
---|
| 62 | { |
---|
[2857] | 63 | if (_glResourceAllocated) |
---|
| 64 | glDeleteTextures(1, &_id); |
---|
[2831] | 65 | |
---|
[2857] | 66 | glGenTextures(1, &_id); |
---|
[2831] | 67 | |
---|
| 68 | update(data); |
---|
| 69 | |
---|
[2857] | 70 | _glResourceAllocated = true; |
---|
| 71 | return _id; |
---|
[2831] | 72 | } |
---|
| 73 | |
---|
| 74 | void Texture2D::update(void *data) |
---|
| 75 | { |
---|
[3561] | 76 | #ifdef USE_HALF_FLOAT |
---|
| 77 | static GLuint halfFloatFormats[] = { |
---|
| 78 | (GLuint)-1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, |
---|
| 79 | GL_RGB16F_ARB, GL_RGBA16F_ARB |
---|
[3559] | 80 | }; |
---|
[3561] | 81 | #elif defined(HAVE_FLOAT_TEXTURES) |
---|
| 82 | static GLuint floatFormats[] = { |
---|
| 83 | (GLuint)-1, GL_LUMINANCE32F_ARB, GL_LUMINANCE_ALPHA32F_ARB, |
---|
| 84 | GL_RGB32F_ARB, GL_RGBA32F_ARB |
---|
| 85 | }; |
---|
| 86 | #endif |
---|
| 87 | static GLuint basicFormats[] = { |
---|
| 88 | (GLuint) -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA |
---|
| 89 | }; |
---|
| 90 | |
---|
[2857] | 91 | glBindTexture(GL_TEXTURE_2D, _id); |
---|
[953] | 92 | |
---|
[2831] | 93 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
| 94 | |
---|
[2857] | 95 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrapS); |
---|
| 96 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrapT); |
---|
[953] | 97 | |
---|
[2857] | 98 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _interpType); |
---|
| 99 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _interpType); |
---|
[2831] | 100 | |
---|
[953] | 101 | //to do: add handling to more formats |
---|
[2877] | 102 | GLuint *targetFormats; |
---|
| 103 | #ifdef HAVE_FLOAT_TEXTURES |
---|
[2857] | 104 | if (_type == GL_FLOAT) { |
---|
[2877] | 105 | # ifdef USE_HALF_FLOAT |
---|
| 106 | targetFormats = halfFloatFormats; |
---|
| 107 | # else |
---|
| 108 | targetFormats = floatFormats; |
---|
| 109 | # endif |
---|
[2831] | 110 | } else { |
---|
[953] | 111 | #endif |
---|
[2877] | 112 | targetFormats = basicFormats; |
---|
| 113 | #ifdef HAVE_FLOAT_TEXTURES |
---|
[953] | 114 | } |
---|
[2831] | 115 | #endif |
---|
[2877] | 116 | |
---|
| 117 | glTexImage2D(GL_TEXTURE_2D, 0, targetFormats[_numComponents], |
---|
| 118 | _width, _height, 0, |
---|
| 119 | basicFormats[_numComponents], _type, data); |
---|
| 120 | |
---|
[2831] | 121 | assert(glGetError() == 0); |
---|
[953] | 122 | } |
---|
| 123 | |
---|
[2857] | 124 | void Texture2D::activate() |
---|
[953] | 125 | { |
---|
[2857] | 126 | glBindTexture(GL_TEXTURE_2D, _id); |
---|
[953] | 127 | glEnable(GL_TEXTURE_2D); |
---|
| 128 | } |
---|
| 129 | |
---|
[2857] | 130 | void Texture2D::deactivate() |
---|
[953] | 131 | { |
---|
| 132 | glDisable(GL_TEXTURE_2D); |
---|
| 133 | } |
---|
| 134 | |
---|
[2857] | 135 | void Texture2D::setWrapS(GLuint wrapMode) |
---|
[953] | 136 | { |
---|
[2857] | 137 | _wrapS = wrapMode; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void Texture2D::setWrapT(GLuint wrapMode) |
---|
| 141 | { |
---|
| 142 | _wrapT = wrapMode; |
---|
| 143 | } |
---|
| 144 | |
---|
[2900] | 145 | void Texture2D::checkMaxSize() |
---|
[2857] | 146 | { |
---|
[953] | 147 | GLint max = 0; |
---|
| 148 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); |
---|
| 149 | |
---|
[3452] | 150 | TRACE("max texture size: %d", max); |
---|
[953] | 151 | } |
---|
| 152 | |
---|
[2900] | 153 | void Texture2D::checkMaxUnit() |
---|
[2831] | 154 | { |
---|
| 155 | int max = 0; |
---|
[953] | 156 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max); |
---|
| 157 | |
---|
[3452] | 158 | TRACE("max texture units: %d", max); |
---|
[953] | 159 | } |
---|