0
\$\begingroup\$

I'm trying to display bitmaps taken from the camera in 3d spheres in OpenGL. The problem I faced is that these images are displayed in a very low resolution. After try some things, I realize that the quality of the texture is proportional to the number of stacks/sectors of the sphere. In some tutorials, I see that it is possible to display images with good definitions in low poly spheres, like 18 or 20 stacks/sectors.

It is possible to show the texture of the sphere in a decent quality with a low number of stacks/sectors?

20 stacks/sectors

enter image description here

200 stacks/sectors

enter image description here

Code:

private float[] init(int numLatitudeLines, int numLongitudeLines, float radius, float squash) {
    float[] vertexData;
    float[] texturePoints;
    int vIndex = 0;
    int texturePos = 0;
    m_Scale = radius;
    m_Squash = squash;
    m_Stacks = numLongitudeLines;
    m_Slices = numLatitudeLines;
    vertexData = new float[3 * ((m_Slices * 2 + 2) * m_Stacks)];
    texturePoints = new float[m_Stacks * m_Slices * 4];
    int phiIdx, thetaIdx;
    float latitudeSpacing = 1.0f / (numLatitudeLines + 1.0f);
    float longitudeSpacing = 1.0f / numLongitudeLines;
    for (phiIdx = 0; phiIdx < m_Stacks; phiIdx++) {
        float phi0 = (float) Math.PI * ((float) (phiIdx + 0) * (1.0f / (float) (m_Stacks)) - 0.5f);
        float phi1 = (float) Math.PI * ((float) (phiIdx + 1) * (1.0f / (float) (m_Stacks)) - 0.5f);
        float cosPhi0 = (float) Math.cos(phi0);
        float sinPhi0 = (float) Math.sin(phi0);
        float cosPhi1 = (float) Math.cos(phi1);
        float sinPhi1 = (float) Math.sin(phi1);
        float cosTheta, sinTheta;
        for (thetaIdx = 0; thetaIdx < m_Slices; thetaIdx++) {
            float theta = (float) (-2.0f * (float) Math.PI * ((float) thetaIdx) * (1.0 / (float) (m_Slices - 1)));
            cosTheta = (float) Math.cos(theta);
            sinTheta = (float) Math.sin(theta);
            vertexData[vIndex + 0] = m_Scale * cosPhi0 * cosTheta;
            vertexData[vIndex + 1] = m_Scale * (sinPhi0 * m_Squash);
            vertexData[vIndex + 2] = m_Scale * (cosPhi0 * sinTheta);
            texturePoints[texturePos] = thetaIdx * longitudeSpacing;
            texturePoints[texturePos + 1] = 1.0f - ((phiIdx + 1) * latitudeSpacing);
            texturePos += 2;
            vertexData[vIndex + 3] = m_Scale * cosPhi1 * cosTheta;
            vertexData[vIndex + 4] = m_Scale * (sinPhi1 * m_Squash);
            vertexData[vIndex + 5] = m_Scale * (cosPhi1 * sinTheta);
            vIndex += 2 * 3;
            texturePoints[texturePos] = thetaIdx * longitudeSpacing;
            texturePoints[texturePos + 1] = 1.0f - ((phiIdx + 1) * latitudeSpacing);
            texturePos += 2;
        }
        vertexData[vIndex + 0] = vertexData[vIndex + 3] = vertexData[vIndex - 3];
        vertexData[vIndex + 1] = vertexData[vIndex + 4] = vertexData[vIndex - 2];
        vertexData[vIndex + 2] = vertexData[vIndex + 5] = vertexData[vIndex - 1];
    }

    mTextureBuffer = ByteBuffer.allocateDirect(texturePoints.length * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(texturePoints);
    mTextureBuffer.position(0);
    return vertexData;
}
\$\endgroup\$
6
  • \$\begingroup\$ Inside your inner loop, you output 2 vertices with three position coordinates each vIndex += 2 * 3, but only one pair of texture coordinates texturePos += 2. That means your vertices and UVs are going to go out of alignment. You may want to structure your loop more like this example \$\endgroup\$
    – DMGregory
    Commented Feb 3, 2022 at 14:13
  • \$\begingroup\$ Thanks @DMGregory! I implement some things from the code you refer to. now the image does not appear upside down, but it still shows only a half of the sphere... and it's necessary to use a lot of stacks/sectors to get a decent quality image. As it is not possible to add images to a comment, I will post and answer to show you \$\endgroup\$
    – Bocho
    Commented Feb 3, 2022 at 17:05
  • \$\begingroup\$ Want to edit your question to show your latest code and results? \$\endgroup\$
    – DMGregory
    Commented Feb 3, 2022 at 17:08
  • \$\begingroup\$ Done, I add an image and the part of my code I changed. thank you very much for your help, @DMGregory \$\endgroup\$
    – Bocho
    Commented Feb 3, 2022 at 17:21
  • 1
    \$\begingroup\$ You're still outputting just one UV coordinate pair for every two vertices. \$\endgroup\$
    – DMGregory
    Commented Feb 3, 2022 at 17:34

0

You must log in to answer this question.

Browse other questions tagged .