therealcain

01-17-2018, 12:59 AM

hello, i followed this tutorial (https://learnopengl.com/#!Advanced-OpenGL/Instancing) for project i'm working on, but somehow only one cube is being drawed.

here is the class source:

class MainWindow

{

private:

GLFWwindow *window;

unsigned int VertexArrayID; // Objects

unsigned int VBO; // Vertex Buffer Object

unsigned int InstanceVBO; //Instance Vertex Buffer Object

unsigned int ProgramID; // Program

std::vector<glm::vec3> translations; // Instance Positions

unsigned int MatrixID; // Handler

glm::mat4 MVP; // Projection * View * Model

// Setting up

void Setup()

{

// GLFW Initialization

if (glfwInit() == false)

throw std::runtime_error("Failed to initialize GLFW!");

glfwWindowHint(GLFW_SAMPLES, 4); // Anti-Aliasing

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL Version

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Old OpenGL De-Support

// Window Creation

window = glfwCreateWindow(800, 800, "Wave Cube", nullptr, nullptr);

// Window Checking

if (window == nullptr)

throw std::runtime_error("GLFW Window could not be Created!");

glfwMakeContextCurrent(window); // OpenGL Context

// OpenGL Initialization

glewExperimental = true;

if (glewInit() != GLEW_OK)

throw std::runtime_error("Failed to initialize GLEW!");

glfwSetInputMode(window, GLFW_STICKY_KEYS, true); // Enable Keyboard

glEnable(GL_DEPTH_TEST);

// Setting Up Positions

for (int y = 0; y < SIZE; y += 1)

{

for (int x = 0; x < SIZE; x += 1)

{

for (int z = 0; z < SIZE; z += 1)

translations.push_back(glm::vec3(static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)));

}

}

}

void Creation()

{

glGenBuffers(1, &InstanceVBO);

glBindBuffer(GL_ARRAY_BUFFER, InstanceVBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * translations.size(), &translations[0], GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

// VAO Setting Up

glGenVertexArrays(1, &VertexArrayID);

glBindVertexArray(VertexArrayID);

// Vertex Buffer Object

glGenBuffers(1, &VBO); // Generate Buffer

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(cubeData), cubeData, GL_STATIC_DRAW); // Set Vertices

glEnableVertexAttribArray(0); // Vertices Attribute

glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, nullptr);

glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, InstanceVBO);

glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, nullptr);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glVertexAttribDivisor(1, 1);

ProgramID = LoadShaders("cube.vert", "cube.frag"); // Program Shaders

}

void Projection()

{

MatrixID = glGetUniformLocation(ProgramID, "MVP"); // Get MVP

glm::mat4 Projection = glm::perspective(glm::radians(90.f), 4.f / 3.f, 0.1f, 100.f); // Projection Matrix

glm::mat4 View = glm::lookAt(glm::vec3(4, 5, 3), // View Matrix

glm::vec3(0, 0, 0),

glm::vec3(0, 1, 0));

glm::mat4 Model = glm::scale(glm::mat4(1.f), glm::vec3(0.1f));

MVP = Projection * View * Model;

}

void Loop()

{

glClearColor(1.f, 0.f, 0.f, 1.f);

while (glfwGetKey(window, GLFW_KEY_ESCAPE) != true && glfwWindowShouldClose(window) == 0)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Buffer

glUseProgram(ProgramID);

glUniformMatrix4fv(MatrixID, 1, false, &MVP[0][0]); // Send Transformation

glBindVertexArray(VertexArrayID);

glDrawArraysInstanced(GL_TRIANGLES, 0, 12 * 3, translations.size());

glBindVertexArray(0);

glfwSwapBuffers(window);

glfwPollEvents();

}

}

public:

void run()

{

Setup();

Creation();

Projection();

Loop();

}

~MainWindow()

{

// Cleanup

glDeleteBuffers(1, &VBO);

glDeleteProgram(ProgramID);

glDeleteVertexArrays(1, &VertexArrayID);

// Close Window

glfwTerminate();

}

};

I've been sitting on it for a whole day with no success, i don't know what am i doing wrong...

here is the class source:

class MainWindow

{

private:

GLFWwindow *window;

unsigned int VertexArrayID; // Objects

unsigned int VBO; // Vertex Buffer Object

unsigned int InstanceVBO; //Instance Vertex Buffer Object

unsigned int ProgramID; // Program

std::vector<glm::vec3> translations; // Instance Positions

unsigned int MatrixID; // Handler

glm::mat4 MVP; // Projection * View * Model

// Setting up

void Setup()

{

// GLFW Initialization

if (glfwInit() == false)

throw std::runtime_error("Failed to initialize GLFW!");

glfwWindowHint(GLFW_SAMPLES, 4); // Anti-Aliasing

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL Version

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Old OpenGL De-Support

// Window Creation

window = glfwCreateWindow(800, 800, "Wave Cube", nullptr, nullptr);

// Window Checking

if (window == nullptr)

throw std::runtime_error("GLFW Window could not be Created!");

glfwMakeContextCurrent(window); // OpenGL Context

// OpenGL Initialization

glewExperimental = true;

if (glewInit() != GLEW_OK)

throw std::runtime_error("Failed to initialize GLEW!");

glfwSetInputMode(window, GLFW_STICKY_KEYS, true); // Enable Keyboard

glEnable(GL_DEPTH_TEST);

// Setting Up Positions

for (int y = 0; y < SIZE; y += 1)

{

for (int x = 0; x < SIZE; x += 1)

{

for (int z = 0; z < SIZE; z += 1)

translations.push_back(glm::vec3(static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)));

}

}

}

void Creation()

{

glGenBuffers(1, &InstanceVBO);

glBindBuffer(GL_ARRAY_BUFFER, InstanceVBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * translations.size(), &translations[0], GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

// VAO Setting Up

glGenVertexArrays(1, &VertexArrayID);

glBindVertexArray(VertexArrayID);

// Vertex Buffer Object

glGenBuffers(1, &VBO); // Generate Buffer

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(cubeData), cubeData, GL_STATIC_DRAW); // Set Vertices

glEnableVertexAttribArray(0); // Vertices Attribute

glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, nullptr);

glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, InstanceVBO);

glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, nullptr);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glVertexAttribDivisor(1, 1);

ProgramID = LoadShaders("cube.vert", "cube.frag"); // Program Shaders

}

void Projection()

{

MatrixID = glGetUniformLocation(ProgramID, "MVP"); // Get MVP

glm::mat4 Projection = glm::perspective(glm::radians(90.f), 4.f / 3.f, 0.1f, 100.f); // Projection Matrix

glm::mat4 View = glm::lookAt(glm::vec3(4, 5, 3), // View Matrix

glm::vec3(0, 0, 0),

glm::vec3(0, 1, 0));

glm::mat4 Model = glm::scale(glm::mat4(1.f), glm::vec3(0.1f));

MVP = Projection * View * Model;

}

void Loop()

{

glClearColor(1.f, 0.f, 0.f, 1.f);

while (glfwGetKey(window, GLFW_KEY_ESCAPE) != true && glfwWindowShouldClose(window) == 0)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Buffer

glUseProgram(ProgramID);

glUniformMatrix4fv(MatrixID, 1, false, &MVP[0][0]); // Send Transformation

glBindVertexArray(VertexArrayID);

glDrawArraysInstanced(GL_TRIANGLES, 0, 12 * 3, translations.size());

glBindVertexArray(0);

glfwSwapBuffers(window);

glfwPollEvents();

}

}

public:

void run()

{

Setup();

Creation();

Projection();

Loop();

}

~MainWindow()

{

// Cleanup

glDeleteBuffers(1, &VBO);

glDeleteProgram(ProgramID);

glDeleteVertexArrays(1, &VertexArrayID);

// Close Window

glfwTerminate();

}

};

I've been sitting on it for a whole day with no success, i don't know what am i doing wrong...