简体   繁体   English

OpenGL半球纹理映射

[英]OpenGL Hemisphere Texture Mapping

I need to have a hemisphere in opengl. 我需要在opengl中使用半球。 I found a drawSphere function which I modified to draw half the lats (which ends up drawing half of the sphere) which is what I wanted. 我发现了一个drawSphere函数,我修改了它来绘制一半的拉特(最终绘制了球体的一半),这就是我想要的。 It does this correctly. 它正确地做到了这一点

However, I don't know what i should do with glTexCoordf to get the textures to map properly onto this half sphere. 但是,我不知道我应该用glTexCoordf做什么来让纹理正确映射到这半球上。 I'm really not great with opengl, and I've tried countless variations but I just can't get the textures to appear properly on it. 对于opengl,我真的不太好,而且我尝试了无数的变化,但我无法让纹理正确地显示在它上面。

void drawHemisphere(double r, int lats, int longs) 
{
    int i, j;
    int halfLats = lats / 2; 
    for(i = 0; i <= halfLats; i++) 
    {
        double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);
        double z0 = sin(lat0);
        double zr0 = cos(lat0);

        double lat1 = M_PI * (-0.5 + (double) i / lats);
        double z1 = sin(lat1);
        double zr1 = cos(lat1);

        glBegin(GL_QUAD_STRIP);
        for(j = 0; j <= longs; j++)
        {
            double lng = 2 * M_PI * (double) (j - 1) / longs;
            double x = cos(lng);
            double y = sin(lng);

            // glTexCoordf()
            glNormal3f(x * zr0, y * zr0, z0);
            glVertex3f(x * zr0, y * zr0, z0);       

            // glTexCoordf()
            glNormal3f(x * zr1, y * zr1, z1);
            glVertex3f(x * zr1, y * zr1, z1);
        }
        glEnd();
    }
}

Does anyone have any idea of what values I should be putting in? 有没有人知道我应该投入什么价值? Or what I need to calculate for it? 或者我需要为它计算什么?

Thanks! 谢谢!

Basically, you shouldn't need anything fancy there. 基本上,你不应该需要任何花哨的东西。 The texture coordinate space ranges from zero to one. 纹理坐标空间的范围从0到1。 So pick some intermediate values for the vertices in between. 因此,为两者之间的顶点选择一些中间值。 I can't explain it more thoroughly without image, so the best I can do is to point You to this article: UV mapping , it's a good starting point. 如果没有图像,我无法更彻底地解释它,所以我能做的最好的就是指向本文: UV贴图 ,这是一个很好的起点。 Hope this helps as a starter. 希望这有助于作为首发。

Here's my guess: 这是我的猜测:

{
    double lng = 2 * M_PI * (double) (j - 1) / longs;
    double x = cos(lng);
    double y = sin(lng);

    double s1, s2, t;
    s1 = ((double) i) / halfLats;
    s2 = ((double) i + 1) / halfLats;
    t = ((double) j) / longs;

    glTexCoord2d(s1, t);
    glNormal3d(x * zr0, y * zr0, z0);
    glVertex3d(x * zr0, y * zr0, z0);

    glTexCoord2d(s2, t);
    glNormal3d(x * zr1, y * zr1, z1);
    glVertex3d(x * zr1, y * zr1, z1);
}

Remember to properly set texture in OpenGL. 请记住在OpenGL中正确设置纹理。 An example call with texture: 一个带纹理的示例调用:

    glActiveTexture(GL_TEXTURE0);
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);

    // texture must be bound & enabled
    texture.bind();
    texture.enable();
    drawHemisphere(1, 40, 40);
    texture.disable();

I used Java + JOGL to test it, so it's not one-to-one C++ solution, but conceptually it should be the same. 我使用Java + JOGL来测试它,所以它不是一对一的C ++解决方案,但从概念上讲它应该是相同的。 At least You have proper glTexCoord2d() calls. 至少你有适当的glTexCoord2d()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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