What i´m trying to do:
I have 2 textures, which i want to blend together on a plane (GL_Quad) using my own fragment shader. Later on i want to implement bump mapping.
At the moment i´m using the same image for BOTH textures to test my code.
The Problem:
Both Versions of my fragment-shader should have the same result, since the textures are equal (same image). But only Version2 seems correct…
It seems that the secound texture is missing!
//VERSION1
uniform sampler2D texture;
uniform sampler2D bumpmap;
void main() {
vec4 colTexture = texture2D(texture,gl_TexCoord[0].st);
vec4 colBumpmap = texture2D(bumpmap,gl_TexCoord[1].st);
gl_FragColor = 0.1* colBumpmap + colTexture;
}
-------------------------------------------------------------------
//VERSION2
uniform sampler2D texture;
uniform sampler2D bumpmap;
void main() {
vec4 colTexture = texture2D(texture,gl_TexCoord[0].st);
vec4 colBumpmap = texture2D(bumpmap,gl_TexCoord[1].st);
gl_FragColor = colBumpmap + 0.1 * colTexture;
}
I think that the error lies in the c++ code. Here are the 3 methods that are relevant:
[ul][li]initializeShader(); []initializeTextures();[]drawPlane();[/ul][/li]
void OpenglWindow::initializeTextures(){
//1 texture
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&textureNr1);
glBindTexture(GL_TEXTURE_2D,textureNr1);
QImage image = QGLWidget::convertToGLFormat(QImage("droplets.jpg"));
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB ,image.width(),image.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,image.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//2 texture (same code, same image, only for GL_TEXTURE1)
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&textureNr2);
glBindTexture(GL_TEXTURE_2D,textureNr2);
QImage image2 = QGLWidget::convertToGLFormat(QImage("droplets.jpg"));
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB ,image2.width(),image2.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,image2.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
void OpenglWindow::initializeShader(){
//1. create new fragmentshader
GLuint fragmentHandle = glCreateShader(GL_FRAGMENT_SHADER);
//2. read shader program from file
QFile fragmentFile("shader.frag");
if (!fragmentFile.open(QIODevice::ReadOnly | QIODevice::Text))
exit(1);
QTextStream in(&fragmentFile);
QString fragSourceCode = in.readAll();
fragmentFile.close();
QByteArray ba = fragSourceCode.toLocal8Bit();
printf("size %d
",ba.size());
const char* src = ba.data();
glShaderSource(fragmentHandle, 1,&src ,NULL);
//3. compile fragmentshader
glCompileShader(fragmentHandle);
int compiled;
glGetShaderiv(fragmentHandle, GL_COMPILE_STATUS, &compiled);
if (!compiled){
printf("compile error
");
}
//4. create program and attach the fragmentshader to it
GLuint prog = glCreateProgram();
glAttachShader(prog,fragmentHandle);
//5. link programm
glLinkProgram(prog);
int linked;
glGetProgramiv(prog, GL_LINK_STATUS, &linked);
if (!linked){
printf("linker error
");
}
//6. init shader uniforms
int textureLoc = glGetUniformLocation(prog, "texture");
glUniform1i(textureLoc, GL_TEXTURE0);
int bumpmapLoc = glGetUniformLocation(prog, "bumpmap");
glUniform1i(bumpmapLoc, GL_TEXTURE1);
//use the prog
glUseProgram(prog);
}
void OpenglWindow::drawPlane(){
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0,0,1); glMultiTexCoord2f(GL_TEXTURE1,0,1); glVertex3f(-1,1,0);
glMultiTexCoord2f(GL_TEXTURE0,0,0); glMultiTexCoord2f(GL_TEXTURE1,0,0); glVertex3f(-1,-1,0);
glMultiTexCoord2f(GL_TEXTURE0,1,0); glMultiTexCoord2f(GL_TEXTURE1,1,0); glVertex3f(1,-1,0);
glMultiTexCoord2f(GL_TEXTURE0,1,1); glMultiTexCoord2f(GL_TEXTURE1,1,1); glVertex3f(1,1,0);
glEnd();
}
Thx for your help!
Hurgh