6

Пишу программу, которая рисует сферу, накладывает на нее текстуру земли и та в итоге вращается вокруг своей оси.

Вот так планета выглядит в первые секунды вращения:

picture_1

Однако, в какой-то момент происходит следующее:

puctire_2

Так же проблемы возникают при вращении относительно других осей.

Код программы привожу ниже:

#include <GL/freeglut.h> 
#include "soil.h"

#include <iostream>

GLuint texture[1];
GLUquadricObj *sphere;
static GLint angle = 0;

void drawEarth()
{
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    gluQuadricTexture(sphere, GLU_TRUE);
    gluQuadricNormals(sphere, GLU_FLAT);
    gluSphere(sphere, 1, 50, 50);
    glDisable(GL_TEXTURE_2D);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glRotatef(angle, 1, 1, 0);
    drawEarth();
    glPopMatrix();
    
    glutSwapBuffers();
}

void timer(int = 0)
{
    display();
    angle += 1;
    angle %= 360;
    glutTimerFunc(10, timer, 0);
}

void reshape(int w, int h)
{
    if (h == 0) h = 1;

    float k = (float)w / h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w > h)
        glOrtho(-2 * k, 2 * k, -2, 2, -5, 10);
    else
        glOrtho(-2, 2, -2 / k, 2 / k, -5, 10);
    glViewport(0, 0, w, h);
}

bool loadTextures()
{
    texture[0] = SOIL_load_OGL_texture
    (
        "C:\\Users\\anton\\source\\repos\\demoGl\\demoGl\\earth.bmp",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
    );

    if (texture[0] == 0)
        return false;

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    return true;
}

//void initLight()
//{
//  GLfloat light0_diffuse[] = { 1.0f, 1.0f, 1.0f };           
//  GLfloat light0_direction[] = { 0.2f, 1.0f, -1.0f, 0.0f };  
//
//  glEnable(GL_LIGHTING);
//  glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
//  glEnable(GL_NORMALIZE);
//
//  glEnable(GL_LIGHT0);
//  glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
//  glLightfv(GL_LIGHT0, GL_POSITION, light0_direction);
//}

void myInit()
{
    if (!loadTextures())
    {
        std::cerr << "failed to load textures . . ." << std::endl;
        exit(EXIT_FAILURE);
    }

    glClearColor(0.0, 0.0, 0.0, 1);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2, 2, -2, 2, -5, 10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    sphere = gluNewQuadric();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    timer();
}


int main(int argc, char **argv)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInit(&argc, argv);
    glutInitWindowSize(640, 480);
    glutCreateWindow("demo");

    myInit();

    glutMainLoop();
}

Скажите, проблема в способе загрузки и указания текстуры, или быть может в ней самой?

Ссылка на пикчу: https://ibb.co/7Sm6D5q

1
  • 3
    Добавил буфер глубины, проблема исчезла)0 3 мая 2019 в 9:37

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.