2

I am using SFML for the window and OpenGL to display a sphere with a texture. However, instead of showing the earth texture, I see a weird rainbow artifact. I am running on Windows 11 and using SFML 2.5.1 and OpenGL 3.3. here is the code

#include <SFML/OpenGL.hpp>
#include <GL/glu.h>

int main()
{
    sf::ContextSettings settings;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 4;

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL Sphere", sf::Style::Default, settings);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glShadeModel(GL_SMOOTH);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glViewport(0, 0, window.getSize().x, window.getSize().y);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)window.getSize().x / (GLfloat)window.getSize().y, 0.1f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    GLuint textureId;
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    sf::Image textureImage;
    if (!textureImage.loadFromFile("C:/Users/---/Desktop/BOLTgui/BOLT/earth/earth.jpg"))
    {
        return EXIT_FAILURE;
    }
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureImage.getSize().x, textureImage.getSize().y, 0, GL_RGB, GL_UNSIGNED_BYTE, textureImage.getPixelsPtr());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    GLUquadric* sphere = gluNewQuadric();
    gluQuadricTexture(sphere, GL_TRUE);
    gluQuadricNormals(sphere, GLU_SMOOTH);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBindTexture(GL_TEXTURE_2D, textureId);
        glPushMatrix();
        glTranslatef(0.0f, 0.0f, 0.0f);
        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
        gluSphere(sphere, 1.0f, 32, 32);
        glDisable(GL_TEXTURE_GEN_S);
        glDisable(GL_TEXTURE_GEN_T);
        glPopMatrix();
        glBindTexture(GL_TEXTURE_2D, 0);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_TEXTURE_GEN_S);
        glEnable(GL_TEXTURE_GEN_T);

        window.display();
    }

    gluDeleteQuadric(sphere);
    glDeleteTextures(1, &textureId);

    return 0;
}

keep showing a weird rainbow artifact rainbow artifact bug

instead of showing this earth texture

how can i fix this bug

  • May https://stackoverflow.com/questions/7681890/reading-jpg-texture-for-opengl-in-c or https://stackoverflow.com/questions/74837341/properly-display-images-with-opengl-if-the-image-is-loaded-by-resource-rc-file be useful? – Bob__ Mar 18 '23 at 00:59
  • @Bob__ so i got the code working but when i resize the sfml window the object keeps getting squashed and the sphere moves to the bottom left and i can not post a new question – christian gaskins Mar 18 '23 at 20:54

0 Answers0