source: trunk/packages/vizservers/nanovis/Texture2D.cpp @ 3177

Last change on this file since 3177 was 3177, checked in by mmc, 12 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

  • Property svn:eol-style set to native
File size: 3.5 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-2012  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
17#include "Texture2D.h"
18#include "Trace.h"
19#include <stdio.h>
20#include <assert.h>
21#include <math.h>
22
23#include "config.h"
24
25Texture2D::Texture2D() :
26    _width(0),
27    _height(0),
28    _numComponents(3),
29    _glResourceAllocated(false),
30    _id(0),
31    _type(GL_FLOAT),
32    _interpType(GL_LINEAR),
33    _wrapS(GL_CLAMP_TO_EDGE),
34    _wrapT(GL_CLAMP_TO_EDGE)
35{}
36
37Texture2D::Texture2D(int width, int height,
38                     GLuint type, GLuint interp,
39                     int numComponents, void *data) :
40    _width(width),
41    _height(height),
42    _numComponents(numComponents),
43    _glResourceAllocated(false),
44    _id(0),
45    _type(type),
46    _interpType(interp),
47    _wrapS(GL_CLAMP_TO_EDGE),
48    _wrapT(GL_CLAMP_TO_EDGE)
49{
50    if (data != NULL)
51        initialize(data);
52}
53
54Texture2D::~Texture2D()
55{
56    glDeleteTextures(1, &_id);
57}
58
59GLuint Texture2D::initialize(void *data)
60{
61    if (_glResourceAllocated)
62        glDeleteTextures(1, &_id);
63
64    glGenTextures(1, &_id);
65
66    update(data);
67
68    _glResourceAllocated = true;
69    return _id;
70}
71
72void Texture2D::update(void *data)
73{
74    static GLuint floatFormats[] = { -1, GL_LUMINANCE32F_ARB, GL_LUMINANCE_ALPHA32F_ARB, GL_RGB32F_ARB, GL_RGBA32F_ARB };
75    static GLuint halfFloatFormats[] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
76    static GLuint basicFormats[] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
77
78    glBindTexture(GL_TEXTURE_2D, _id);
79
80    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
81
82    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrapS);
83    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrapT);
84
85    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _interpType);
86    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _interpType);
87
88    //to do: add handling to more formats
89    GLuint *targetFormats;
90#ifdef HAVE_FLOAT_TEXTURES
91    if (_type == GL_FLOAT) {
92# ifdef USE_HALF_FLOAT
93        targetFormats = halfFloatFormats;
94# else
95        targetFormats = floatFormats;
96# endif
97    } else {
98#endif
99        targetFormats = basicFormats;
100#ifdef HAVE_FLOAT_TEXTURES
101    }
102#endif
103
104    glTexImage2D(GL_TEXTURE_2D, 0, targetFormats[_numComponents],
105                 _width, _height, 0,
106                 basicFormats[_numComponents], _type, data);
107
108    assert(glGetError() == 0);
109}
110
111void Texture2D::activate()
112{
113    glBindTexture(GL_TEXTURE_2D, _id);
114    glEnable(GL_TEXTURE_2D);
115}
116
117void Texture2D::deactivate()
118{
119    glDisable(GL_TEXTURE_2D);           
120}
121
122void Texture2D::setWrapS(GLuint wrapMode)
123{
124    _wrapS = wrapMode;
125}
126
127void Texture2D::setWrapT(GLuint wrapMode)
128{
129    _wrapT = wrapMode;
130}
131
132void Texture2D::checkMaxSize()
133{
134    GLint max = 0;
135    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
136       
137    TRACE("max texture size: %d\n", max);
138}
139
140void Texture2D::checkMaxUnit()
141{
142    int max = 0;
143    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
144
145    TRACE("max texture units: %d.\n", max);
146}
Note: See TracBrowser for help on using the repository browser.