0

I cannot get a image from an OpenCV image to get mapped onto a 2D plane in OpenGL. Whenever I try to render the image onto the plane using the code below, I get a yellow plane instead of the actual image on it.

// My Init code
cv::Mat mat(640, 480, CV_8UC3, cv::Scalar(255, 0, 0));

GLuint tid = 0;
if (tid != 0)
{
    glDeleteTextures(1, &tid);
}

glGenTextures(1, &tid);
glBindTexture(GL_TEXTURE_2D, tid);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mat.cols, mat.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, (void*)mat.data);
glGenerateMipmap(GL_TEXTURE_2D);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);

.

// My Render Code
{
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glTranslatef(0, 0, 0);

    glBindTexture(GL_TEXTURE_2D, tid);

    GLfloat sq_vert[] = {-1, -1, 1, -1, 1, 1, -1, 1};
    glVertexPointer(2, GL_FLOAT, 0, sq_vert);
    glEnableClientState(GL_VERTEX_ARRAY);

    GLfloat sq_tex[] = {0, 0, 1, 0, 1, 1, 0, 1};
    glTexCoordPointer(2, GL_FLOAT, 0, sq_tex);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glBindTexture(GL_TEXTURE_2D, 0);

    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
}

Image

genpfault
  • 51,148
  • 11
  • 85
  • 139
Moe Bataineh
  • 1,032
  • 1
  • 12
  • 29
  • Where is the OpenCV image (`mat`) loaded or generated? The image in the question is a completely yellow image. – Rabbid76 Apr 22 '19 at 16:38
  • Hey @Rabbid76, Sorry, I had 255, 255, 0 as the default values of the OpenCV Mat but no matter what I do whether I change the defaults to 255,0,0 or 0,0,255 or even cv::imread an image from my machine, I still get a yellow texture. – Moe Bataineh Apr 22 '19 at 16:43
  • How are you planning on using the fixed-function matrix stack with OpenGL ES 2.0? – genpfault Apr 22 '19 at 16:45
  • Hey @genpfault, I'm a completely lost on what you mean. I'm not that experienced in OpenGL and have been trying to cludge up a UV mapping from tutorials I find online. – Moe Bataineh Apr 22 '19 at 16:48
  • Well, OpenGL ES 2.0 removed fixed-function matrix stack functions like `glPushMatrix()` & `glTranslatef()` in favor of generic vertex attributes so I'm not sure how your code is even compiling if you aren't using some sort of shim/compatibility library. – genpfault Apr 22 '19 at 16:57
  • @genpfault I'm so sorry! I must be using straight OpenGL and not ES. I have removed the tags. – Moe Bataineh Apr 22 '19 at 16:59
  • Aah, that'd do it then :) – genpfault Apr 22 '19 at 17:03

2 Answers2

2

I solved my issue.. I needed to run the initialize() code I had above in the same thread that I init'd the OpenGL Context.

Moe Bataineh
  • 1,032
  • 1
  • 12
  • 29
0

Make sure you reset the current color state back to RGB(1.0, 1.0, 1.0) with a glColor() call before you draw your texture, otherwise the default GL_MODULATE texture environment will multiply all your texel colors with the current color state, most likely yellow from your debug text.

Or switch to GL_DECAL which ignores the current color state entirely.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Thanks for this info! Now I have a white texture but still, no image mapped onto it. The result should be a red texture because of OpenCV mat being initialized to (255, 0, 0). I must be missing something but I don't know what.. – Moe Bataineh Apr 22 '19 at 17:29
  • @MoeBataineh: Time for a [mcve] then, though double-check that `tid` is non-zero after the `glGenTextures()` call. – genpfault Apr 22 '19 at 17:37