I am trying to render a video texture and a revolving 3D cube on it. Till now I have following…
- A light weight dll which captures video and call backs “registered” method with video buffer, so that we can paint/do whatever in our main window.
- 3D cube rendering on blank background.
My problem : not able to render the video buffer on the background of 3D cube.
–Creating video texture as
void CreateVideoTexture(int w, int h, int tW, int tH)
{
width = w;
height = h;
texWidth = tW;
texHeight = tH;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
unsigned char* pixels = new unsigned char[texWidth*texHeight];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
IsAnyGLError = glGetError();
delete pixels;
}
–Updating video texture
void UpdateVideoTexture(unsigned char* pixels)
{
if (!pixels) return;
if (texId == 0)
{
// printf("Error: texture not created!!!
");
return;
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glDisable(GL_TEXTURE_2D);
}
–Render the video texture
void RenderVideoTexture()
{
static GLubyte front[] = {0,1,2,3};
static GLfloat vertices[] = {-50.0, -50.0, 0.0,
50.0, -50.0, 0.0,
50.0, 50.0, 0.0,
-50.0, 50.0, 0.0};
static GLfloat xTex = width / (float)texWidth;
static GLfloat yTex = height / (float)texHeight;
// y-flipped
//static GLfloat texCoords[] = {0.0, 0.0, xTex, 0.0, xTex, yTex, 0.0, yTex};
// Non y-flipped
static GLfloat texCoords[] = {0.0, yTex, xTex, yTex, xTex, 0.0, 0.0, 0.0};
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof( -50.0, 50.0, -50.0, 50.0, -50.0, 50.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor4f(1.0f,1.0f,1.0f,1.0f); // This is important! Reset color for texture rendering.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texId);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, front);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
Conclusion:
- I can see each statements in CreateVideoTexture is getting executed properly and glGetError() returns 0
- I can see updated buffer as unsigned char * in my callback function, and from there I call UpdateVideoTexture() and then glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); and then RenderVideoTexture();
All these steps get executed properly, and I could not find any error by putting glGetError() after each statements. Still I could not see the video on background.
Would appreciate if anyone can give me any clue on whats happening here. Many thanks.