Reputation: 77
It says "an array cannot be initialized with a parenthesized initializer", which I'm not quiet sure I understand... So, can someone explain what is causing it, why and how I fix it? Please? (By the way, I'm sort of new to memory c++, so be gentle please?) What I'm trying to accomplish is for a mesh to be generated by an obj file. And to be honest I'm not that great at it. So sorry!
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]);
}
}
}
Anyway, thanks for checking out this post/question, even if it just reading. I appreciate any help I can get! (And I've been working on loading this mesh thing for a long time and I just want to be done with it)
I hope you're having a nice day! (Or night, lol) :)
Upvotes: 0
Views: 256
Reputation: 974
You can't use a native c array as the template argument for std::vector<>
.
Use a std::array<3>
instead
Like so: std::vector<std::array<float,3>>
You can also use a pointer to three float that's fine.
Upvotes: 0
Reputation: 211277
The elements of a std::vector
have to be copyable and assignable. Arrays are not copyable and assignable.
I recommend to use 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;
Furthermore, if a named buffer object is bound, then the last parameter of glVertexAttribPointer
is treated as a byte offset into the buffer object's data store:
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));
Upvotes: 3