RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 871198
Accepted
Юрій Писанка
Юрій Писанка
Asked:2020-08-21 07:08:08 +0000 UTC2020-08-21 07:08:08 +0000 UTC 2020-08-21 07:08:08 +0000 UTC

模型类中的网格绘图无法正常工作

  • 772

我正在尝试为自己创建一个用于存储和描绘 3D 模型的类,但这就是问题所在。我加载立方体,一切都井井有条,我加载另一个平面,立方体将被绘制而不是它。我关闭立方体平面的加载平面绘制我做错了什么?

class MeshT
{
public: 
    std::vector<int>vec1;
    int InS;
    int VerS;
    bool IStexture; //Чи має mesh текстуру
    GLuint texture; //Текстура
    GLuint VBO, VAO, EBO;  //створитии змінні для буферів
    string Log;
    MeshT(std::string urlV,std::string urlI)
    {
        IStexture = false;
    //====Просто считую всі цифри які є============ВЕРШИНИ
    GLfloat* a;
    int av;
    {
        std::vector<float>t;
        ifstream f(urlV.c_str());
        if(!f) {Log+="Error loading "; Log+=urlV.c_str(); Log+='\n'; return;}
        while (!f.eof()) {
          float n;
          f >> n;
          t.push_back(n);
        }
        f.close();
        a = new GLfloat[t.size()];
        int i = 0;
        while(i<t.size()) {a[i] = t[i]; i++;}
        av = t.size();
    }



    //====Просто считую всі цифри які є============ІНДЕКСИ
    int inv = 0;
    GLint* in;
    if(urlI.size() != 0)
    {
        {
            std::vector<GLint>t;
            ifstream f(urlI.c_str());
            if(!f) {Log+="Error loading "; Log+=urlI.c_str(); Log+='\n'; return;}
            while (!f.eof()) {
              GLint n;
              f >> n;
              t.push_back(n);
            }
            f.close();
            in = new GLint[t.size()];
            int i = 0;
            while(i<t.size()) {in[i] = t[i]; i++;}
            inv = t.size();
        }
    }








    InS = inv;
    VerS = av;

    //=================Якщо індекси присутні=================
    if(InS != 0)
    {
    glGenVertexArrays(1, &VAO); //Згенерувати буфер
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);
    // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO); //зв'язую наш буфер з Open GL
    glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat) * av, a, GL_STATIC_DRAW);  //Передати дані на відеокарту

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint)*inv, in, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(
        0,//Номер для шейдера
        3,//Розмір в одиницях
        GL_FLOAT, //Float
        GL_FALSE, //Нормалізація
        8*sizeof(float), //Крок
        (GLvoid*)0 //Змішення в буфері
        );

     glVertexAttribPointer(
        1,//Номер для шейдера
        3,//Розмір в одиницях
        GL_FLOAT, //Float
        GL_FALSE, //Нормалізація
        8*sizeof(float), //Крок
        (GLvoid*)(3*sizeof(float)) //Змішення в буфері
        );
        glEnableVertexAttribArray(1);


        glVertexAttribPointer(
        2,//Номер для шейдера
        2,//Розмір в одиницях
        GL_FLOAT, //Float
        GL_FALSE, //Нормалізація
        8*sizeof(float), //Крок
        (GLvoid*)(6*sizeof(float)) //Змішення в буфері
        );
        glEnableVertexAttribArray(2);



    glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
    }
    else
    {

        {
             //GLfloat* vertices,int vs,GLint* indices,int is   
        glGenVertexArrays(1, &VAO); //Згенерувати буфер


        // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
        glBindVertexArray(VAO);

        glBindBuffer(GL_ARRAY_BUFFER, VBO); //зв'язую наш буфер з Open GL
        glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat) * av, a, GL_STATIC_DRAW);  //Передати дані на відеокарту



           glEnableVertexAttribArray(0);
    glVertexAttribPointer(
        0,//Номер для шейдера
        3,//Розмір в одиницях
        GL_FLOAT, //Float
        GL_FALSE, //Нормалізація
        8*sizeof(float), //Крок
        (GLvoid*)0 //Змішення в буфері
        );

     glVertexAttribPointer(
        1,//Номер для шейдера
        3,//Розмір в одиницях
        GL_FLOAT, //Float
        GL_FALSE, //Нормалізація
        8*sizeof(float), //Крок
        (GLvoid*)(3*sizeof(float)) //Змішення в буфері
        );
        glEnableVertexAttribArray(1);


        glVertexAttribPointer(
        2,//Номер для шейдера
        2,//Розмір в одиницях
        GL_FLOAT, //Float
        GL_FALSE, //Нормалізація
        8*sizeof(float), //Крок
        (GLvoid*)(6*sizeof(float)) //Змішення в буфері
        );
        glEnableVertexAttribArray(2);




        glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

        glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
        }

    }



    }


    void setTexture(GLuint t)
    {
        IStexture = true;
        texture = t;
    }

    void draw()
    {
        //IMEG
        if(IStexture)
        {
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture);
        }

        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, InS, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);


        //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
        glEnableVertexAttribArray(0);
    }

    ~MeshT()
    {
        // Properly de-allocate all resources once they've outlived their purpose
        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);
        glDeleteBuffers(1, &EBO);
    }
};

加载

ShaderL test("data\\shaders\\all\\test.vs","data\\shaders\\all\\test.frag");

    MeshT CUB("data\\mesh\\Cube.vertex","data\\mesh\\Cube.index");
CUB.setTexture(LoTe("data\\texture\\1.jpg"));
cout<<"CUB: "<<CUB.Log<<endl;


MeshT plane("data\\mesh\\plane.vertex","data\\mesh\\plane.index");
cout<<"PLANE: "<<plane.Log<<endl;
plane.setTexture(CUB.texture);

绘画

caM(camera,test.shaderProgram,Wi,Hei);

    //========================(4)
        {
                    TransformU Tra1;
                    Tra1.translate(0,0,0);
                    Tra1.rotate(0,timer,0);
                    CUB.draw();
                    Tra1.active("model",test.shaderProgram);
        }

        {
                    TransformU Tra1;
                    Tra1.translate(0,-3,0);
                    Tra1.scale(5);
                    plane.draw();
                    Tra1.active("model",test.shaderProgram);
        }

完整项目 679KB

c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Asaq
    2020-08-22T16:18:45Z2020-08-22T16:18:45Z

    渲染模型后,将模型矩阵传递给着色器:

    CUB.draw();
    Tra1.active("model",test.shaderProgram);
    

    原来立方体是用平面的矩阵绘制的,平面是用立方体的矩阵绘制的。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5