[953] | 1 | |
---|
| 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 <stdio.h> |
---|
| 19 | #include <assert.h> |
---|
| 20 | #include <math.h> |
---|
| 21 | |
---|
| 22 | #include "config.h" |
---|
| 23 | |
---|
| 24 | Texture2D::Texture2D(){} |
---|
| 25 | |
---|
| 26 | Texture2D::Texture2D(int width, int height, GLuint type=GL_FLOAT, |
---|
| 27 | GLuint interp=GL_LINEAR, int n=4, float* data = 0) |
---|
| 28 | { |
---|
| 29 | assert(type == GL_UNSIGNED_BYTE || |
---|
| 30 | type == GL_FLOAT || |
---|
| 31 | type ==GL_UNSIGNED_INT); |
---|
| 32 | assert(interp == GL_LINEAR || interp == GL_NEAREST); |
---|
| 33 | |
---|
| 34 | this->width = width; |
---|
| 35 | this->height = height; |
---|
| 36 | this->type = type; |
---|
| 37 | this->interp_type = interp; |
---|
| 38 | this->n_components = n; |
---|
| 39 | |
---|
| 40 | this->id = 0; |
---|
| 41 | |
---|
| 42 | if(data != 0) |
---|
| 43 | initialize(data); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | GLuint |
---|
| 47 | Texture2D::initialize(float *data) |
---|
| 48 | { |
---|
| 49 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
| 50 | |
---|
| 51 | glGenTextures(1, &id); |
---|
| 52 | glBindTexture(GL_TEXTURE_2D, id); |
---|
[1053] | 53 | assert(id != (GLuint)-1); |
---|
[953] | 54 | |
---|
| 55 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
| 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
| 57 | |
---|
| 58 | if(interp_type == GL_LINEAR){ |
---|
| 59 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 60 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
| 61 | } else { |
---|
| 62 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
| 63 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | //to do: add handling to more formats |
---|
| 67 | if (type==GL_FLOAT) { |
---|
| 68 | switch(n_components){ |
---|
| 69 | #ifdef NV40 |
---|
| 70 | case 1: |
---|
| 71 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, width, height, |
---|
| 72 | 0, GL_LUMINANCE, GL_FLOAT, data); |
---|
| 73 | break; |
---|
| 74 | case 2: |
---|
| 75 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, |
---|
| 76 | height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data); |
---|
| 77 | break; |
---|
| 78 | case 3: |
---|
| 79 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, width, height, 0, |
---|
| 80 | GL_RGB, GL_FLOAT, data); |
---|
| 81 | break; |
---|
| 82 | case 4: |
---|
| 83 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, |
---|
| 84 | GL_RGBA, GL_FLOAT, data); |
---|
| 85 | break; |
---|
| 86 | #else |
---|
| 87 | case 1: |
---|
| 88 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, |
---|
| 89 | GL_LUMINANCE, GL_FLOAT, data); |
---|
| 90 | break; |
---|
| 91 | case 2: |
---|
| 92 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height, |
---|
| 93 | 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data); |
---|
| 94 | break; |
---|
| 95 | case 3: |
---|
| 96 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, |
---|
| 97 | GL_FLOAT, data); |
---|
| 98 | break; |
---|
| 99 | case 4: |
---|
| 100 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, |
---|
| 101 | GL_FLOAT, data); |
---|
| 102 | break; |
---|
| 103 | #endif |
---|
| 104 | default: |
---|
| 105 | break; |
---|
| 106 | } |
---|
| 107 | } else { |
---|
| 108 | int comp[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA }; |
---|
| 109 | glTexImage2D(GL_TEXTURE_2D, 0, comp[n_components], width, height, 0, |
---|
| 110 | GL_RGBA, type, data); |
---|
| 111 | } |
---|
| 112 | assert(glGetError()==0); |
---|
| 113 | return id; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | void |
---|
| 118 | Texture2D::activate() |
---|
| 119 | { |
---|
| 120 | glBindTexture(GL_TEXTURE_2D, id); |
---|
| 121 | glEnable(GL_TEXTURE_2D); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | void |
---|
| 126 | Texture2D::deactivate() |
---|
| 127 | { |
---|
| 128 | glDisable(GL_TEXTURE_2D); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | Texture2D::~Texture2D() |
---|
| 133 | { |
---|
| 134 | glDeleteTextures(1, &id); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | void |
---|
| 139 | Texture2D::check_max_size(){ |
---|
| 140 | GLint max = 0; |
---|
| 141 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); |
---|
| 142 | |
---|
| 143 | fprintf(stderr, "max texture size: %d\n", max); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void |
---|
| 147 | Texture2D::check_max_unit(){ |
---|
| 148 | int max; |
---|
| 149 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max); |
---|
| 150 | |
---|
| 151 | fprintf(stderr, "max texture units: %d.\n", max); |
---|
| 152 | } |
---|
| 153 | |
---|