source: trunk/gui/vizservers/nanovis/Texture2D.cpp @ 723

Last change on this file since 723 was 432, checked in by qiaow, 18 years ago

Added some comments

File size: 3.5 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 * Texture2D.h: 2d texture class
4 *
5 * ======================================================================
6 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
7 *           Purdue Rendering and Perceptualization Lab (PURPL)
8 *
9 *  Copyright (c) 2004-2006  Purdue Research Foundation
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15
16#include "Texture2D.h"
17#include <stdio.h>
18#include <assert.h>
19#include <math.h>
20
21#include "config.h"
22
23Texture2D::Texture2D(){}
24
25Texture2D::Texture2D(int width, int height, GLuint type=GL_FLOAT, GLuint interp=GL_LINEAR, int n=4, float* data = 0)
26{
27        assert(type == GL_UNSIGNED_BYTE || type == GL_FLOAT|| type ==GL_UNSIGNED_INT);
28        assert(interp == GL_LINEAR || interp == GL_NEAREST);
29       
30        this->width = width;
31        this->height = height;
32        this->type = type;
33        this->interp_type = interp;
34        this->n_components = n;
35
36        this->id = 0;
37
38        if(data != 0)
39          initialize(data);
40}
41
42GLuint Texture2D::initialize(float *data)
43{
44        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
45
46        glGenTextures(1, &id);
47        glBindTexture(GL_TEXTURE_2D, id);
48        assert(id!=-1);
49
50        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
51        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
52       
53        if(interp_type == GL_LINEAR){
54          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
55          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
56        }
57        else{
58          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
59          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
60        }
61
62        //to do: add handling to more formats
63        if(type==GL_FLOAT){
64          switch(n_components){
65            #ifdef NV40
66                case 1:
67                        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, width, height,  0, GL_LUMINANCE, GL_FLOAT, data);
68                        break;
69                case 2:
70                        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
71                        break;
72                case 3:
73                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, width, height, 0, GL_RGB, GL_FLOAT, data);
74                        break;
75                case 4:
76                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, data);
77                        break;
78            #else
79                case 1:
80                        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_FLOAT, data);
81                        break;
82                case 2:
83                        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
84                        break;
85                case 3:
86                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, data);
87                        break;
88                case 4:
89                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, data);
90                        break;
91            #endif
92                default:
93                        break;
94          }
95        }
96
97        assert(glGetError()==0);
98        return id;
99}
100
101
102void Texture2D::activate()
103{
104  glBindTexture(GL_TEXTURE_2D, id);
105  glEnable(GL_TEXTURE_2D);
106}
107
108
109void Texture2D::deactivate()
110{
111  glDisable(GL_TEXTURE_2D);             
112}
113
114
115Texture2D::~Texture2D()
116{
117  glDeleteTextures(1, &id);
118}
119
120
121void Texture2D::check_max_size(){
122  GLint max = 0;
123  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
124       
125  fprintf(stderr, "max texture size: %d\n", max);
126}
127
128void Texture2D::check_max_unit(){
129  int max;
130  glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
131
132  fprintf(stderr, "max texture units: %d.\n", max);
133}
134
Note: See TracBrowser for help on using the repository browser.