hello,
i´m still learning the basics of the glsl, so i´m a bit confused when you have to pass a variable to the shader it self, in this case i want to load like 2 textures and using the same program (vertex and pixel shader) attach 1 texture to a cube and the other one to the floor, i don´t know if that´s possible or not, do i have to compile a diferent shader program to attach a texture to each object?!
//Function for loading the textures
// Using auxDIBImageLoad's Own Error-Handler!
int LoadGLTextures(){ // Load Bitmaps And Convert To Textures
bool status=true; // Status Indicator
AUX_RGBImageRec *Image=NULL; // Create Storage Space For The Texture
char *alpha=NULL;
GLuint texture[3];
// Load The Tile-Bitmap For Base-Texture
if (Image=auxDIBImageLoad("Data/crate.bmp")) {
// Storage For 3 Textures
glGenTextures(3, texture); // Create Three Textures
// Create MipMapped Texture
glActiveTexture(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, Image->sizeX, Image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
glClientActiveTextureARB(GL_TEXTURE0_ARB);
}
else status=false;
if (Image) { // If Texture Exists
if (Image->data) delete Image->data; // If Texture Image Exists
delete Image;
Image=NULL;
}
// Load The Tile-Bitmap For Base-Texture
if (Image=auxDIBImageLoad("Data/leaf.bmp")) {
glActiveTexture(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, Image->sizeX, Image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
glClientActiveTextureARB(GL_TEXTURE1_ARB);
}
else status=false;
if (Image) { // If Texture Exists
if (Image->data) delete Image->data; // If Texture Image Exists
delete Image;
Image=NULL;
}
return status;
}
// Ser Up Cube And Floor
// Cube → texture[0] Floor ->texture[1]
void doCube (void) {
int i;
glPushMatrix();
glTranslatef(0, 0, 5);
//glScalef(1,1,1);
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, +1.0f);
for (i=0; i<4; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, data[5*i],data[5*i+1]);
glVertex3f(data[5*i+2],data[5*i+3],data[5*i+4]);
}
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
for (i=4; i<8; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, data[5*i],data[5*i+1]);
glVertex3f(data[5*i+2],data[5*i+3],data[5*i+4]);
}
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
for (i=8; i<12; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, data[5*i],data[5*i+1]);
glVertex3f(data[5*i+2],data[5*i+3],data[5*i+4]);
}
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
for (i=12; i<16; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, data[5*i],data[5*i+1]);
glVertex3f(data[5*i+2],data[5*i+3],data[5*i+4]);
}
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
for (i=16; i<20; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, data[5*i],data[5*i+1]);
glVertex3f(data[5*i+2],data[5*i+3],data[5*i+4]);
}
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
for (i=20; i<24; i++) {
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, data[5*i],data[5*i+1]);
glVertex3f(data[5*i+2],data[5*i+3],data[5*i+4]);
}
glEnd();
glPopMatrix();
}
void floor(){
glPushMatrix();
glRotatef(90,1,0,0);
glBegin(GL_POLYGON);
glNormal3f(0,1,0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, data[0],data[1]); glVertex3f(-25,0,-25);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, data[5],data[6]); glVertex3f(25,0,-25);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, data[10],data[11]);glVertex3f(25,0,25);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, data[15],data[16]);glVertex3f(-25,0,25);
glEnd();
glPopMatrix();
}
//Set Shaders Function
void setShaders() {
char *vs = NULL,*fs = NULL,*fs2 = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
f2 = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead("shade/textureSimple.vert");
fs = textFileRead("shade/textureSimple.frag");
const char * vv = vs;
const char * ff = fs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
E.printShaderInfoLog(v);
E.printShaderInfoLog(f);
E.printShaderInfoLog(f2);
p = glCreateProgram();
glAttachShader(p,v);
glAttachShader(p,f);
glLinkProgram(p);
E.printProgramInfoLog(p);
glUseProgram(p);
}
//Draw Objects And attach the textures to each object
//Ser uniform location for each texture
//to pass to fragment shader
//(tex,0) and (tex,1)
tex = glGetUniformLocation(p,"texture[0]");
glUniform1iARB(tex,0);
doCube();
tex = glGetUniformLocation(p,"texture[1]");
glUniform1iARB(tex,1);
floor();
//Vertex Shader
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
//Fragment Shader
//Receive → uniform name variable:tex location: 0 and 1
uniform sampler2D tex;
void main()
{
vec4 color = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = color;
}
Now the problem is that the cube gets the 1st texture and the floor gets black wich probably means that it can´t find the 2nd texture on the right address or extension, could someone help me out?