dimanche 28 juin 2015

render an image on the sphere using opengl

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.

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

Aucun commentaire:

Enregistrer un commentaire