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

Last change on this file since 2831 was 2831, checked in by ldelgass, 9 years ago

Refactor texture classes, misc. cleanups, cut down on header pollution -- still
need to fix header deps in Makefile.in

  • Property svn:eol-style set to native
File size: 3.1 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-2006  Purdue Research Foundation
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    gl_resource_allocated(false),
27    id(0)
28{}
29
30Texture2D::Texture2D(int width, int height,
31                     GLuint type, GLuint interp,
32                     int numComponents, void *data) :
33    gl_resource_allocated(false),
34    id(0)
35{
36    this->width = width;
37    this->height = height;
38    this->type = type;
39    this->interp_type = interp;
40    this->n_components = numComponents;
41
42    if (data != NULL)
43        initialize(data);
44}
45
46Texture2D::~Texture2D()
47{
48    glDeleteTextures(1, &id);
49}
50
51GLuint Texture2D::initialize(void *data)
52{
53    if (gl_resource_allocated)
54        glDeleteTextures(1, &id);
55
56    glGenTextures(1, &id);
57
58    update(data);
59
60    gl_resource_allocated = true;
61    return id;
62}
63
64void Texture2D::update(void *data)
65{
66    glBindTexture(GL_TEXTURE_2D, id);
67
68    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
69
70    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
71    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
72
73    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interp_type);
74    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interp_type);
75
76    //to do: add handling to more formats
77#ifdef NV40
78    if (type == GL_FLOAT) {
79        GLuint targetFormat[5] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
80        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
81        glTexImage2D(GL_TEXTURE_2D, 0, targetFormat[n_components], width, height, 0,
82                     format[n_components], type, data);
83    } else {
84#endif
85        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
86        glTexImage2D(GL_TEXTURE_2D, 0, format[n_components], width, height, 0,
87                     format[n_components], type, data);
88#ifdef NV40
89    }
90#endif
91    assert(glGetError() == 0);
92
93    gl_resource_allocated = true;
94}
95
96void
97Texture2D::activate()
98{
99    glBindTexture(GL_TEXTURE_2D, id);
100    glEnable(GL_TEXTURE_2D);
101}
102
103void
104Texture2D::deactivate()
105{
106    glDisable(GL_TEXTURE_2D);           
107}
108
109void
110Texture2D::check_max_size()
111{
112    GLint max = 0;
113    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
114       
115    TRACE("max texture size: %d\n", max);
116}
117
118void
119Texture2D::check_max_unit()
120{
121    int max = 0;
122    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
123
124    TRACE("max texture units: %d.\n", max);
125}
Note: See TracBrowser for help on using the repository browser.