我正在尝试使用 OpenCV 和 OpenGL 绘制一个圆圈,我必须这样做,因为我需要在纹理上绘图。
目前该图如下所示:
生成此图像并绘制它的代码如下:
class Texture
{
void setup()
{
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &uid);
glBindTexture(GL_TEXTURE_2D, uid);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
...
void setData(unsigned char * data, uint width, uint height, GLint internalformat, GLenum format)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, uid);
glTexImage2D( GL_TEXTURE_2D,
0,
internalformat,
width,
height,
0,
format,
GL_UNSIGNED_BYTE,
data);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
...
void draw(float x, float y, float scale )
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, uid);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(x, y);
glTexCoord2i(0, 1); glVertex2i(x, y + height * scale);
glTexCoord2i(1, 1); glVertex2i(x + width * scale, y + height * scale);
glTexCoord2i(1, 0); glVertex2i(x + width * scale, y);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
...
};
class App
{
Texture tex;
...
void update()
{
const int w_mask_cv = 640;
const int h_mask_cv = 480;
cv::Mat mask_cv = cv::Mat::zeros(w_mask_cv, h_mask_cv, CV_8UC1);
cv::circle(mask_cv, cv::Point(w_mask_cv/2, h_mask_cv/2), 15, cv::Scalar(255));
tex.setData(mask_cv.data, w_mask_cv, h_mask_cv, GL_LUMINANCE, GL_LUMINANCE);
}
void draw()
{
tex.draw(0, 0, 1);
}
...
};
如您所见,圆圈应该在中间并且不重复,但我不知道为什么它在水平轴上重复。
有什么建议么?