source: nanovis/trunk/Texture2D.cpp @ 4809

Last change on this file since 4809 was 3611, checked in by ldelgass, 12 years ago

Use nv namespace for classes in nanovis rather than prefixing class names with
Nv (still need to convert shader classes).

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
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 *
10 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
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
22#include "Texture2D.h"
23#include "Trace.h"
24
25using namespace nv;
26
27Texture2D::Texture2D() :
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)
37{}
38
39Texture2D::Texture2D(int width, int height,
40                     GLuint type, GLuint interp,
41                     int numComponents, void *data) :
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)
51{
52    if (data != NULL)
53        initialize(data);
54}
55
56Texture2D::~Texture2D()
57{
58    glDeleteTextures(1, &_id);
59}
60
61GLuint Texture2D::initialize(void *data)
62{
63    if (_glResourceAllocated)
64        glDeleteTextures(1, &_id);
65
66    glGenTextures(1, &_id);
67
68    update(data);
69
70    _glResourceAllocated = true;
71    return _id;
72}
73
74void Texture2D::update(void *data)
75{
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
80    };
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
91    glBindTexture(GL_TEXTURE_2D, _id);
92
93    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94
95    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrapS);
96    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrapT);
97
98    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _interpType);
99    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _interpType);
100
101    //to do: add handling to more formats
102    GLuint *targetFormats;
103#ifdef HAVE_FLOAT_TEXTURES
104    if (_type == GL_FLOAT) {
105# ifdef USE_HALF_FLOAT
106        targetFormats = halfFloatFormats;
107# else
108        targetFormats = floatFormats;
109# endif
110    } else {
111#endif
112        targetFormats = basicFormats;
113#ifdef HAVE_FLOAT_TEXTURES
114    }
115#endif
116
117    glTexImage2D(GL_TEXTURE_2D, 0, targetFormats[_numComponents],
118                 _width, _height, 0,
119                 basicFormats[_numComponents], _type, data);
120
121    assert(glGetError() == 0);
122}
123
124void Texture2D::activate()
125{
126    glBindTexture(GL_TEXTURE_2D, _id);
127    glEnable(GL_TEXTURE_2D);
128}
129
130void Texture2D::deactivate()
131{
132    glDisable(GL_TEXTURE_2D);           
133}
134
135void Texture2D::setWrapS(GLuint wrapMode)
136{
137    _wrapS = wrapMode;
138}
139
140void Texture2D::setWrapT(GLuint wrapMode)
141{
142    _wrapT = wrapMode;
143}
144
145void Texture2D::checkMaxSize()
146{
147    GLint max = 0;
148    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
149       
150    TRACE("max texture size: %d", max);
151}
152
153void Texture2D::checkMaxUnit()
154{
155    int max = 0;
156    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
157
158    TRACE("max texture units: %d", max);
159}
Note: See TracBrowser for help on using the repository browser.