我正在尝试为自己创建一个用于存储和描绘 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);
}
渲染模型后,将模型矩阵传递给着色器:
原来立方体是用平面的矩阵绘制的,平面是用立方体的矩阵绘制的。