Bienvenue, Invité. Merci de vous connecter ou de vous inscrire.
Avez-vous perdu votre e-mail d'activation ?

Auteur Sujet: Erreur d'affichage très étrange à cause de math.h  (Lu 1910 fois)

0 Membres et 1 Invité sur ce sujet

Slash94

  • Jr. Member
  • **
  • Messages: 89
    • Voir le profil
Erreur d'affichage très étrange à cause de math.h
« le: Octobre 14, 2017, 01:28:41 pm »
Salut à tous,

Je conçois en ce moment un système de projection basé sur le Mode 7.

Lorsque je décide d'inclure la librairie <math.h>, j'ai une erreur d'affichage grotesque, il n'y a que la moitié de mon rendu qui s'affiche :



Alors que lorsque je retire "#include <math.h>", tout se redraw parfaitement..



Je ne comprends absolument pas l'erreur.. Voici mon code :

#include <math.h>
#include <iostream>

#include <SFML/Graphics.hpp>

int main()
{
    unsigned int WIDTHSCREEN = 800, HEIGHTSCREEN = 600;

    sf::RenderWindow app(sf::VideoMode(WIDTHSCREEN, HEIGHTSCREEN), "Rotozoom", sf::Style::Titlebar);
    app.setFramerateLimit(60);

    sf::RenderWindow render(sf::VideoMode(WIDTHSCREEN, HEIGHTSCREEN), "Render", sf::Style::Titlebar);
    render.setFramerateLimit(60);

    sf::Clock clock;

    std::vector<sf::VertexArray>vecLines;
    std::vector<sf::VertexArray>vecQuads;

    sf::Vector2f m_position = {100,100};
    float angle = 0.0f;

    double speedMove = 20;

    for (unsigned int i = 0 ; i < HEIGHTSCREEN ; i++)
    {
        sf::VertexArray line(sf::Lines, WIDTHSCREEN);
        for (unsigned int k = 0 ; k < WIDTHSCREEN ; k++)
            line[k].position = {0, i};
        vecLines.push_back(line);
    }

    std::vector<std::vector<unsigned int>>vecMap =
    {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}


    };

    unsigned int blocSize = 32;

    for (unsigned int y = 0 ; y < vecMap.size() ; y++)
    {
        for (unsigned int x = 0 ; x < vecMap[y].size() ; x++)
        {
            if (vecMap[y][x])
            {
                sf::VertexArray quad(sf::Quads, 4);
                quad[0].position = {x*blocSize, y*blocSize};
                quad[1].position = {x*blocSize+blocSize, y*blocSize};
                quad[2].position = {x*blocSize+blocSize, y*blocSize+blocSize};
                quad[3].position = {x*blocSize, y*blocSize+blocSize};

                for (unsigned int c = 0 ; c < 4 ; c++)
                    x%2 ? quad[c].color = sf::Color::Red : quad[c].color = sf::Color::Blue;

                vecQuads.push_back(quad);
            }
        }
    }

    double scale = 200;

    while (app.isOpen())
    {
        sf::Event event;
        while (app.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                    app.close();
                break;
                case sf::Event::KeyReleased:
                    if (event.key.code == sf::Keyboard::Escape)
                        app.close();
                break;
            }
        }

        render.clear();

        for (unsigned int y = 0 ; y < HEIGHTSCREEN ; y++)
        {
            unsigned int l = 0;
            for (unsigned int x = 0 ; x < WIDTHSCREEN ; x++)
            {
                for (unsigned int i = 0 ; i < vecQuads.size() ; i++)
                {
                    double xDistance = abs(WIDTHSCREEN/2-x);

                    if (x==vecQuads[i][0].position.x && y>=vecQuads[i][0].position.y && y<= vecQuads[i][3].position.y)
                    {
                        vecLines[y][l].position.x = WIDTHSCREEN/2;
                        vecLines[y][l].position.y = y/2;
                        vecLines[y][l].color = vecQuads[i][0].color;

                        if (x<WIDTHSCREEN/2)
                            vecLines[y][l].position.x = x - (y*xDistance/scale);
                        if (x>WIDTHSCREEN/2)
                            vecLines[y][l].position.x = x + (y*xDistance/scale);
                        l++;
                    }

                    if (x==vecQuads[i][1].position.x && y>=vecQuads[i][0].position.y && y<= vecQuads[i][2].position.y)
                    {
                        vecLines[y][l].color = vecQuads[i][1].color;
                        vecLines[y][l].position.y = y/2;
                        vecLines[y][l].position.x = WIDTHSCREEN/2;

                        if (x<WIDTHSCREEN/2)
                            vecLines[y][l].position.x = x - (y*xDistance/scale);
                        if (x>WIDTHSCREEN/2)
                            vecLines[y][l].position.x = x + (y*xDistance/scale);
                        l++;
                    }
                }
            }
        }

        app.clear();

        for (auto& vvec : vecQuads)
            app.draw(vvec);

        for (auto& vvec : vecLines)
            render.draw(vvec);

        app.display();

        render.display();
    }
}
 

Si je retire le "math.h", ça projette sans problème ma perspective, si je l'inclu, la moitié ne s'affiche plus du tout..

Merci à vous !
« Modifié: Octobre 14, 2017, 10:33:46 pm par Slash94 »

G.

  • Hero Member
  • *****
  • Messages: 1592
    • Voir le profil
Re: Erreur d'affichage très étrange à cause de math.h
« Réponse #1 le: Octobre 15, 2017, 12:02:10 pm »
Etant donné qu'abs est le seul truc de math que tu utilises je suppose que ça vient de là.
Ça marche si tu inclus plutôt <cmath>
« Modifié: Octobre 15, 2017, 12:03:47 pm par G. »

Slash94

  • Jr. Member
  • **
  • Messages: 89
    • Voir le profil
Re: Erreur d'affichage très étrange à cause de math.h
« Réponse #2 le: Octobre 15, 2017, 02:45:33 pm »
Nickel, ça marche parfaitement avec <cmath> !

Merci à toi G. :-)
« Modifié: Octobre 22, 2017, 02:23:56 pm par Slash94 »

 

anything