source: trunk/vizservers/nanovis/Texture2D.cpp @ 884

Last change on this file since 884 was 825, checked in by vrinside, 16 years ago

Image Loader initialization

  • Add BMP loader in Nv.cpp

Point Renderer code added..

  • Put a data container and manage the containters with std::vector
  • Renderer 1) scale factor part should be taken into account
File size: 3.7 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    else
97    {
98        int comp[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
99                glTexImage2D(GL_TEXTURE_2D, 0, comp[n_components], width, height, 0, GL_RGBA, type, data);
100    }
101
102        assert(glGetError()==0);
103        return id;
104}
105
106
107void Texture2D::activate()
108{
109  glBindTexture(GL_TEXTURE_2D, id);
110  glEnable(GL_TEXTURE_2D);
111}
112
113
114void Texture2D::deactivate()
115{
116  glDisable(GL_TEXTURE_2D);             
117}
118
119
120Texture2D::~Texture2D()
121{
122  glDeleteTextures(1, &id);
123}
124
125
126void Texture2D::check_max_size(){
127  GLint max = 0;
128  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
129       
130  fprintf(stderr, "max texture size: %d\n", max);
131}
132
133void Texture2D::check_max_unit(){
134  int max;
135  glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
136
137  fprintf(stderr, "max texture units: %d.\n", max);
138}
139
Note: See TracBrowser for help on using the repository browser.