Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GL_TEXTURE_2D_ARRAY in OpenFrameworks
void ofApp::setup(){
textures[0].load("Rhythmus01.jpg");
textures[1].load("Rhythmus02.jpg");
textureShader.load("TextureArrayShader");
GLsizei width = 480;
GLsizei height = 360;
GLsizei layerCount = 2;
GLsizei mipLevelCount = 1;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D_ARRAY,texture);
//Allocate the storage.
glTexStorage3D(GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGB8, width, height, layerCount);
//Upload pixel data.
//The first 0 refers to the mipmap level (level 0, since there's only 1)
//The following 2 zeroes refers to the x and y offsets in case you only want to specify a subrectangle.
//The final 0 refers to the layer index offset (we start from index 0 and have 2 levels).
//Altogether you can specify a 3D box subset of the overall texture, but only one mip level at a time.
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, textures[0].getPixels());
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, textures[1].getPixels());
//Always set reasonable texture parameters
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D_ARRAY,0);
}
void ofApp::draw(){
ofBackground(50);
ofSetColor(255);
glActiveTexture(GL_TEXTURE0+texture);
glClientActiveTexture(GL_TEXTURE0+texture);
glEnable(GL_TEXTURE_2D_ARRAY);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
int w = textures[0].getWidth();
int h = textures[0].getHeight();
ofMesh m;
m.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
m.addVertex(ofVec2f(0, 0));
m.addVertex(ofVec2f(w, 0));
m.addVertex(ofVec2f(0, h));
m.addVertex(ofVec2f(w, h));
w = 1.0;
h = 1.0;
m.addTexCoord(ofVec2f(0, 0));
m.addTexCoord(ofVec2f(w, 0));
m.addTexCoord(ofVec2f(0, h));
m.addTexCoord(ofVec2f(w, h));
textureShader.begin();
textureShader.setUniformTexture("texture", GL_TEXTURE_2D_ARRAY, texture, 0);
textureShader.setUniform1i("texID", ofGetMousePressed() ? 1: 0);
m.draw();
textureShader.end();
glActiveTexture(GL_TEXTURE0+texture);
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
glDisable(GL_TEXTURE_2D_ARRAY);
glActiveTexture(GL_TEXTURE0);
}
#version 150
#define MAX_TEXTURES 2
uniform sampler2DArray textures;
uniform int texID;
in vec2 vTexCoord;
out vec4 fragColor;
void main(){
vec4 color = texture(textures, vec3(vTexCoord, texID));
fragColor = vec4(color.rgb, 1.0);
}
#version 150
#define MAX_TEXTURES 2
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 textureMatrix;
uniform mat4 normalMatrix;
uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec4 color;
in vec4 normal;
in vec2 texcoord;
out vec2 vTexCoord;
void main(){
vTexCoord = (textureMatrix*vec4(texcoord.x,texcoord.y,0,1)).xy;
gl_Position = modelViewProjectionMatrix * position;
}
@mantissa

This comment has been minimized.

Copy link
Owner Author

mantissa commented Jul 12, 2016

This example toggles between two textures loaded from ofImage. Click the mouse to toggle between texture 1 and 2.
Note that this example uses non ARB texture coordinates, which range from 0.0 to 1.0 not from 0.0 to image width/height.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.