使用opengl在球体上渲染图像

时间:2022-09-30 20:02:44

I want to render an image on the sphere with any type(e.g jpg tif bmp). so I try the opencv and opengl to do this work. but Iam new for opengl.

我想在任何类型的球体上渲染图像(例如jpg tif bmp)。所以我尝试使用opencv和opengl来完成这项工作。但我是opengl的新手。

below is the code i used.

下面是我使用的代码。

void createTex(Mat &img)
{
    glGenTextures(2, m_texnames);
    glBindTexture(GL_TEXTURE_2D, m_texnames[0]);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPLACE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPLACE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.cols, img.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, img.data);
}
void sphere()
{
    glEnable(GL_TEXTURE_2D);
    createTex(src);
    glBindTexture(GL_TEXTURE_2D, m_texnames[0]);
    glPushMatrix();
    gluQuadricTexture(g_text, GLU_TRUE);             
    gluQuadricDrawStyle(g_text, GLU_FILL);           
    gluSphere(g_text, 1.0, 64, 64); /*draw sun */
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);
}

virtual void onDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);
    validateCameraPosition();
    validateModelPosition();
    sphere();
    glFlush();
    glutSwapBuffers();
}

I cant post a picture for now ; eee.........eee the picture is very normal (a earth map from wiki), as below.

我暂时不能张贴照片; eee ......... eee图片很正常(来自wiki的地球地图),如下所示。

but ,there is a fault on the sphere when I change the view postion. is there a mistake in my code? and some one can tell why it occurs?

但是,当我改变视图位置时,球体上有一个错误。我的代码中有错误吗?有人可以说出它为什么会发生?

//update:2015.06.28

finally, i found the mistake. the function gluPerspective() cant take 0.0f that specifies the distance from the viewer to the near clipping plane; i think the 0.0f may disturb the project matix; so the value must be bigger than zero; below is the code dismistaked;

最后,我发现了错误。函数gluPerspective()不能取0.0f指定从观察者到近剪裁平面的距离;我认为0.0f可能会扰乱项目的基础;所以该值必须大于零;下面是代码;

virtual void onResize(int w, int h)
{
    printf("resize window: %d, %d\n", w, h);
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluPerspective(100.0,(GLfloat)w / (GLfloat)h, 0.0/*wrong*/, 20.0);
    gluPerspective(100.0, (GLfloat)w / (GLfloat)h, 0.1, 20.0);
    //glOrtho(-1, 1, -1, 1,0,20);
}

1 个解决方案

#1


0  

Not sure if this will help.. but still better than no answers.

不确定这是否会有所帮助..但仍然比没有答案更好。

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPLACE);

I don't think glTexParameterf is supposed to be used for GL_TEXTURE_WRAP_S. It better be glTextParameteri. Also, I don't thinkg GL_REPLACE is valid value for it. see this reference. I rather:

我不认为glTexParameterf应该用于GL_TEXTURE_WRAP_S。最好是glTextParameteri。另外,我认为GL_REPLACE不是有效值。看到这个参考。我宁愿:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

A function that actually uses GL_REPLACE would be glTexEnvi, when pname is GL_TEXTURE_ENV_MODE.

当pname为GL_TEXTURE_ENV_MODE时,实际使用GL_REPLACE的函数将是glTexEnvi。

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Here is a board where some people discussed on that.

这是一个董事会,有些人就此进行了讨论。

Personal note: I would stay away from glu and try to practice rendering myself, with modern OpenGL. Useful tutorial.

个人注意事项:我会远离glu并尝试使用现代OpenGL进行自我渲染。有用的教程。

#1


0  

Not sure if this will help.. but still better than no answers.

不确定这是否会有所帮助..但仍然比没有答案更好。

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPLACE);

I don't think glTexParameterf is supposed to be used for GL_TEXTURE_WRAP_S. It better be glTextParameteri. Also, I don't thinkg GL_REPLACE is valid value for it. see this reference. I rather:

我不认为glTexParameterf应该用于GL_TEXTURE_WRAP_S。最好是glTextParameteri。另外,我认为GL_REPLACE不是有效值。看到这个参考。我宁愿:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

A function that actually uses GL_REPLACE would be glTexEnvi, when pname is GL_TEXTURE_ENV_MODE.

当pname为GL_TEXTURE_ENV_MODE时,实际使用GL_REPLACE的函数将是glTexEnvi。

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Here is a board where some people discussed on that.

这是一个董事会,有些人就此进行了讨论。

Personal note: I would stay away from glu and try to practice rendering myself, with modern OpenGL. Useful tutorial.

个人注意事项:我会远离glu并尝试使用现代OpenGL进行自我渲染。有用的教程。