我爲OpenGL 3.3創建了一個Mesh類,當我使用非默認構造函數創建類時,它在我創建對象時創建頂點時正常工作。
但是,我現在想要通過將它們放入一個向量中來動態創建多個對象,所以我必須添加一個默認構造函數,以便使用與其他構造函數相同的函數來設置緩衝區數據。但它不起作用。據我所知,不是因爲它在向量中,而是它與構造函數有關,或者與稍後創建緩衝區數據有關。我真的不太確定。使用默認構造函數調用的網格類不工作OpenGL C++
這是我的課程。 (當我創建工作我調用構造函數與參數,當它不工作我構造不帶參數的網格,稱之爲「changeMesh」功能的目)
mesh.h
#ifndef MESH_H
#define MESH_H
#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
class mesh
{
public:
mesh();
mesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles, GLuint shaderProgram);
~mesh();
void changeMesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles, GLuint shaderProgram);
void render();
void Translate(glm::vec3 addVector);
void Rotate(glm::vec3 rotVector, GLfloat angle);
protected:
private:
GLuint vertexArrayObject, vertexBuffer, elementBuffer, shaderProgram;
std::vector<GLfloat> vertices;
std::vector<GLuint> indices;
glm::mat4 transform;
void setUpMesh();
void bindVertices();
};
#endif // MESH_H
mesh.cpp
#include "../include/mesh.h"
mesh::mesh(std::vector<GLfloat> vertices, std::vector<GLuint> indices, GLuint shaderProgram)
{
this->shaderProgram = shaderProgram;
this->vertices = vertices;
this->indices = indices;
setUpMesh();
}
mesh::mesh(){
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
glGenBuffers(1, &vertexBuffer);
glGenBuffers(1, &elementBuffer);
}
mesh::~mesh()
{
glDeleteBuffers(1, &elementBuffer);
glDeleteBuffers(1, &vertexBuffer);
glDeleteVertexArrays(1, &vertexArrayObject);
}
void mesh::changeMesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles, GLuint shaderProgram){
this->shaderProgram = shaderProgram;
this->vertices = vertices;
this->indices = indices;
bindVertices();
}
void mesh::setUpMesh(){
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
glGenBuffers(1, &vertexBuffer);
glGenBuffers(1, &elementBuffer);
bindVertices();
glBindVertexArray(0);
}
void mesh::bindVertices(){
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(GLfloat), this->vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), this->indices.data(), GL_STATIC_DRAW);
GLint amountDataPerVert = 5;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, amountDataPerVert*sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, amountDataPerVert*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
glBindVertexArray(0);
}
void mesh::render(){
glBindVertexArray(vertexArrayObject);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "transform"), 1, GL_FALSE, glm::value_ptr(transform));
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void mesh::Translate(glm::vec3 addVector){
transform = glm::translate(transform, addVector);
}
void mesh::Rotate(glm::vec3 rotVector, GLfloat angle){
transform = glm::rotate(transform, glm::radians(angle), rotVector);
}
很可能是因爲你在'mesh'中聲明的任何成員變量都不能被默認構造。考慮使用默認構造函數的成員初始值設定項列表。 – 2015-03-08 17:20:11