繁体   English   中英

我不知道是什么导致了错误 (C3074)

[英]I don't know what causing the error (C3074)

它说“无法使用带括号的初始化程序初始化数组”,我不确定我是否理解......那么,有人可以解释是什么原因造成的,为什么以及如何修复它? 请? (顺便说一句,我对 memory c++ 有点陌生,所以请温柔一点?)我想要完成的是由 obj 文件生成网格。 老实说,我并不擅长。 非常抱歉!

Mesh.h ----------------------------------------------- ---------------

    #pragma once
#include <GL/glew.h>
#include <glm/glm.hpp>

#include <vector>

#include "../Shader/Shader.h"

struct Vertex
{
    float Position[3];
    float Uv[2];
    float Normal[3];
};

class Mesh
{
private:
    unsigned int VAO, VBO; // Vertex Array Object   ID
    unsigned int TextureID; // Texture ID
    unsigned int size;

    const char* objFilePath;
    const char* textureFilePath;

    Shader& shader;

public:
    Mesh(const char* objFilePath, const char* textureFilePath, Shader& shader);

    void draw();
};

Mesh.cpp ------------------------------------------------------------ ---------------

    #include "Mesh.h"

void fillVerticesWithData(std::vector<float> data, std::vector<Vertex> info);

Mesh::Mesh(const char* objFilePath, const char* textureFilePath, Shader& shader)
    : objFilePath(objFilePath), textureFilePath(textureFilePath), shader(shader)
{
    {
        // Load Vertices from File!
        std::vector<Vertex> vertices;

        {
            std::vector<float[3]> filePos;    // Position(s)
            std::vector<float[2]> fileUV;     // Uv(s)
            std::vector<float[3]> fileNorm;   // Normal(s)

            std::ifstream file;
            file.open(objFilePath);

            std::string line;
            while (std::getline(file, line))
            {
                std::string text;
                std::istringstream iss(line);

                iss >> text;

                // Easy part!
                if (text == "v")
                {
                    float currectPos[3];

                    iss >> currectPos[0];
                    iss >> currectPos[1];
                    iss >> currectPos[2];

                    filePos.push_back(currectPos);
                }
                if (text == "vt")
                {
                    float currectUV[2];

                    iss >> currectUV[0];
                    iss >> currectUV[1];

                    fileUV.push_back(currectUV);
                }
                if (text == "vn")
                {
                    float currectNorm[3];

                    iss >> currectNorm[0];
                    iss >> currectNorm[1];
                    iss >> currectNorm[2];

                    fileNorm.push_back(currectNorm);
                }


                // Last part, hard part!
                if (text == "f")
                {
                    int x, y, z;
                    int x2, y2, z2;
                    int x3, y3, z3;
                    char e;

                    // First Vertex!
                    iss >> x;
                    iss >> e;
                    iss >> y;
                    iss >> e;
                    iss >> z;
                    iss >> x2;
                    iss >> e;
                    iss >> y2;
                    iss >> e;
                    iss >> z2;
                    iss >> x3;
                    iss >> e;
                    iss >> y3;
                    iss >> e;
                    iss >> z3;

                    Vertex temp_Vertex;
                    for (int i = 0; i < 3; i++)
                    {
                        temp_Vertex.Position[i] = filePos.at(x - 1)[i];
                        temp_Vertex.Normal[i] = fileNorm.at(y - 1)[i];
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        temp_Vertex.Uv[i] = fileUV.at(z - 1)[i];
                    }

                    vertices.push_back(temp_Vertex);

                    std::cout << "Vertex 1 -----------" << std::endl;
                    std::cout << "Position: " << temp_Vertex.Position[0] << " " << temp_Vertex.Position[1] << " " << temp_Vertex.Position[2] << std::endl;
                    std::cout << "UV: " << temp_Vertex.Uv[0] << " " << temp_Vertex.Uv[1] << std::endl;
                    std::cout << "Normal: " << temp_Vertex.Normal[0] << " " << temp_Vertex.Normal[1] << temp_Vertex.Normal[2] << std::endl;

                    // Second Vertex!
                    Vertex temp_Vertex2;
                    for (int i = 0; i < 3; i++)
                    {
                        temp_Vertex2.Position[i] = filePos.at(x2 - 1)[i];
                        temp_Vertex2.Normal[i] = fileNorm.at(y2 - 1)[i];
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        temp_Vertex2.Uv[i] = fileUV.at(z2 - 1)[i];
                    }

                    vertices.push_back(temp_Vertex2);

                    std::cout << "Vertex 1 -----------" << std::endl;
                    std::cout << "Position: " << temp_Vertex2.Position[0] << " " << temp_Vertex2.Position[1] << " " << temp_Vertex2.Position[2] << std::endl;
                    std::cout << "UV: " << temp_Vertex2.Uv[0] << " " << temp_Vertex2.Uv[1] << std::endl;
                    std::cout << "Normal: " << temp_Vertex2.Normal[0] << " " << temp_Vertex2.Normal[1] << temp_Vertex2.Normal[2] << std::endl;

                    // Third Vertex
                    Vertex temp_Vertex3;
                    for (int i = 0; i < 3; i++)
                    {
                        temp_Vertex3.Position[i] = filePos.at(x3 - 1)[i];
                        temp_Vertex3.Normal[i] = fileNorm.at(y3 - 1)[i];
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        temp_Vertex3.Uv[i] = fileUV.at(z3 - 1)[i];
                    }

                    vertices.push_back(temp_Vertex3);

                    std::cout << "Vertex 1 -----------" << std::endl;
                    std::cout << "Position: " << temp_Vertex3.Position[0] << " " << temp_Vertex3.Position[1] << " " << temp_Vertex3.Position[2] << std::endl;
                    std::cout << "UV: " << temp_Vertex3.Uv[0] << " " << temp_Vertex3.Uv[1] << std::endl;
                    std::cout << "Normal: " << temp_Vertex3.Normal[0] << " " << temp_Vertex3.Normal[1] << temp_Vertex3.Normal[2] << std::endl;

                    // Face!!! :O
                    // This one here I am having trouble with
                    // How do I read it?
                }
            }
        }

        // Load Texture from File!

        // Last step:
        std::vector<float> vertexArray;

        fillVerticesWithData(vertexArray, vertices);

        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);

        glBindVertexArray(VAO);

        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexArray.size() * sizeof(float), &vertexArray[0], GL_STATIC_DRAW);

        // Position Pointer:
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)3);
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)5);
        glEnableVertexAttribArray(3);

        // Unbinding everything:
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

        size = vertexArray.size();
    }
}

void Mesh::draw()
{
    shader.use();
    glBindVertexArray(VAO);
//  glBindTexture(GL_TEXTURE_2D, TextureID);

    // Later:
    // Draw Elements

    // For now:
    glDrawArrays(GL_TRIANGLES, 0, size);
}


// data is the data we wanna fill with information!
// The info has the information we want to put into data!
void fillVerticesWithData(std::vector<float> data, std::vector<Vertex> info)
{
    for (int i = 0; i < info.size(); i++)
    {
        // Position:
        for (int posI = 0; posI < 3; posI++)
        {
            float temp[3];

            temp[posI] = info.at(i).Position[posI];
            data.push_back(temp[posI]);
        }
        // Uv:
        for (int uvI = 0; uvI < 2; uvI++)
        {
            float temp[2];

            temp[uvI] = info.at(i).Uv[uvI];
            data.push_back(temp[uvI]);
        }
        // Normal:
        for (int norI = 0; norI < 3; norI++)
        {
            float temp[3];

            temp[norI] = info.at(i).Normal[norI];
            data.push_back(temp[norI]);
        }
    }
}

无论如何,感谢您查看此帖子/问题,即使它只是阅读。 我很感激我能得到的任何帮助! (而且我一直在努力加载这个网格东西很长一段时间,我只想完成它)

我希望你今天过得愉快,(或晚上:哈哈):)

std::vector的元素必须是可复制和可分配的。 Arrays 不可复制和分配。

我建议使用std::array

std::vector<std::array<float, 3>> filePos;    // Position(s)
std::vector<std::array<float, 2>> fileUV;     // Uv(s)
std::vector<std::array<float, 3>> fileNorm;   // Normal(s)
std::array<float, 3> currectPos;
std::array<float, 2> currectUV;
std::array<float, 3> currectNorm;

此外,如果绑定了命名缓冲区 object,则glVertexAttribPointer的最后一个参数将被视为缓冲区对象数据存储的字节偏移量:

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)3);

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(sizeof(float)*3));

glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)5);

glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(sizeof(float)*5));

您不能使用本机 c 数组作为std::vector<>的模板参数。 改用std::array<3>像这样: std::vector<std::array<float,3>>

您也可以使用指向三个浮点数的指针,这很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM