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

Last change on this file since 3502 was 3502, checked in by ldelgass, 7 years ago

Add basic VTK structured points reader to nanovis, update copyright dates.

  • 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-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
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", 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", max);
146}
Note: See TracBrowser for help on using the repository browser.