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

时间:2020-06-20 13:53:25

标签: c++ opengl

它说“无法使用带括号的初始值设定项来初始化数组”,我不确定我是否能理解这一点……那么,有人可以解释造成它的原因,原因和解决方法吗?请? (顺便说一句,我对内存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]);
        }
    }
}

无论如何,即使您正在阅读,也感谢您查看本帖子/问题。 感谢您能获得的任何帮助! (而且我很长时间以来一直在加载这个网格物体,我只想完成它)

希望您今天过得愉快! (或者晚上,哈哈):)

2 个答案:

答案 0 :(得分:3)

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

我建议使用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;

此外,如果绑定了命名的缓冲区对象,则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));

答案 1 :(得分:0)

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

您还可以使用指向三个浮点数的指针。