Hello everyone.
This is what I’m trying to do: I want to render somethign to texture in framebuffer, then use this texture asi uniform for another set of shaders, and they use texture’s values to compute something. I’ve tried disabling rendering to special framebuffer, so it renders directly to screen and I see output there. So I can assume when I render to special framebuffer, texture is filled. However, when I try to acces this texture in second sets of shaders, texture2D(…) always returns black color. Here’s code:
// create texture
glGenTextures(1, &texA);
glBindTexture(GL_TEXTURE_2D, texA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA,GL_UNSIGNED_BYTE, NULL);
// create framebuffer
glGenFramebuffers(1, &fbA);
glBindFramebuffer(GL_FRAMEBUFFER, fbA);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texA, 0);
// render to framebuffer
glViewport(0, 0, 32, 32);
glBindFramebuffer(GL_FRAMEBUFFER, fbA);
glEnableVertexAttribArray(vertexPosition3DLogic);
glBindBuffer(GL_ARRAY_BUFFER, LBO);
glVertexAttribPointer(vertexPosition3DLogic, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // output to screen is ok
glDisableVertexAttribArray(vertexPosition3DLogic);
glViewport(0, 0, 1024, 600);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// render to screen
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texA);
glUniform1i(inputTextureLocation, 0);
//Enable vertex position
glEnableVertexAttribArray(vertexPosition3D);
//Set vertex data
glBindBuffer( GL_ARRAY_BUFFER, VBO );
glVertexAttribPointer(vertexPosition3D, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(vertexNormal);
glBindBuffer( GL_ARRAY_BUFFER, NBO );
glVertexAttribPointer(vertexNormal, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL );
Can anyone please tell me what’s wrong? Thank you very much.
EDIT: I didn’t show useProgram(), this happens between rendering to framebuffer and rendering to screen but I’m 100% sure it’s correct.
EDIT2: It’s actually kinda working: When I run it, my object flashes with color from texture and then color changes permanently to completely transparent.