简体   繁体   中英

render an image on the sphere using opengl

I want to render an image on the sphere with any type(eg jpg tif bmp). so I try the opencv and opengl to do this work. but Iam new for 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.

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;

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);
}

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:

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM