This is my program:
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>
int main()
{
glfwWindowHint(GLFW_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_VERSION_MINOR, 4);
if (!glfwInit())
std::cerr << "Failed to initialize GLFW." << std::endl;
GLFWwindow* window;
window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
if (!window)
std::cerr << "Failed to create Window." << std::endl;
glfwMakeContextCurrent(window);
GLenum err;
if ((err = glewInit()) != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
std::cerr << err << std::endl;
}
float data[] = { -0.5, -0.5, 0.1,
0.0, 0.5, 0.1,
0.5, -0.5, 0.1 };
GLuint vaoID;
GLuint vboID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
It produces a white triangle like you might expect. BUT. when i place some of the vao and vbo code in a class like this:
#pragma once
#include <GL\glew.h>
class Model
{
public:
Model(float* data) {
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);
glBindVertexArray(0);
}
~Model();
GLuint vaoID;
GLuint vboID;
};
And the main code looks like this:
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>
#include "Model.h"
int main()
{
glfwWindowHint(GLFW_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_VERSION_MINOR, 4);
if (!glfwInit())
std::cerr << "Failed to initialize GLFW." << std::endl;
GLFWwindow* window;
window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
if (!window)
std::cerr << "Failed to create Window." << std::endl;
glfwMakeContextCurrent(window);
GLenum err;
if ((err = glewInit()) != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
std::cerr << err << std::endl;
}
float data[] = { -0.5, -0.5, 0.1,
0.0, 0.5, 0.1,
0.5, -0.5, 0.1 };
Model model(data);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(model.vaoID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, model.vboID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
It does not.
Why is this it has been foiling my projects for ages and i’m really fed up with it.