I'm using SFML for window handling and OpenGL 4.2 for 3D rendering, and I'm trying to runder a Squad shape using VBO and VAO but the problem is my software is crashing at runtime with the following error.
This problem is in my Engine::CreateSquare() function.
here is my code
Engine.h
#pragma once
#include <GL\glew.h>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\OpenGL.hpp>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <string>
#include <iostream>
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
#pragma comment(lib, "sfml-audio-d.lib")
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "opengl32.lib")
class Engine
{
public:
Engine();
~Engine();
void Init();
void Event();
void Render();
void CreateSquare();
short WindowWidth, WindowHeight, BitsPerPixle;
private:
unsigned int VAO_ID[1]; // Our Vertex Array Object
unsigned int VBO_ID[1]; // Our Vertex Buffer Object
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;
glm::mat4 ModelMatrix;
};
Engine.cpp
#include "Engine.h"
Engine::Engine()
{
WindowWidth = 800;
WindowHeight = 600;
BitsPerPixle = 32;
}
Engine::~Engine()
{
}
void Engine::Init()
{
glClearColor(0.4f, 0.6f, 0.9f, 0.0f); // Set the clear color based on Microsofts CornflowerBlue (default in XNA)
ProjectionMatrix = glm::perspective(60.0f, (float)WindowWidth / (float)WindowHeight, 0.1f, 100.f); // Create our perspective projection matrix
CreateSquare();
}
void Engine::Event()
{
}
void Engine::Render()
{
glViewport(0, 0, WindowWidth, WindowHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
ViewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.f)); // Create our view matrix which will translate us back 5 units
ModelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f)); // Create our model matrix which will halve the size of our model
glBindVertexArray(VAO_ID[0]); // Bind our Vertex Array Object
glDrawArrays(GL_TRIANGLES, 0, 6); // Draw our square
glBindVertexArray(0); // Unbind our Vertex Array Object
}
void Engine::CreateSquare()
{
float* Vertices = new float[18]; // Vertices for our square
Vertices[0] = -0.5; Vertices[1] = -0.5; Vertices[2] = 0.0; // Bottom left corner
Vertices[3] = -0.5; Vertices[4] = 0.5; Vertices[5] = 0.0; // Top left corner
Vertices[6] = 0.5; Vertices[7] = 0.5; Vertices[8] = 0.0; // Top Right corner
Vertices[9] = 0.5; Vertices[10] = -0.5; Vertices[11] = 0.0; // Bottom right corner
Vertices[12] = -0.5; Vertices[13] = -0.5; Vertices[14] = 0.0; // Bottom left corner
Vertices[15] = 0.5; Vertices[16] = 0.5; Vertices[17] = 0.0; // Top Right corner
glGenVertexArrays(1, &VAO_ID[0]); // Create our Vertex Array Object
glBindVertexArray(VAO_ID[0]); // Bind our Vertex Array Object so we can use it
glGenBuffers(1, VBO_ID); // Generate our Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]); // Bind our Vertex Buffer Object
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), Vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
glEnableVertexAttribArray(0); // Disable our Vertex Array Object
glBindVertexArray(0); // Disable our Vertex Buffer Object
delete [] Vertices; // Delete our vertices from memory
}
Main.cpp
#include "Engine.h"
int main()
{
Engine Engine;
sf::Window Window(sf::VideoMode(Engine.WindowWidth, Engine.WindowHeight, Engine.BitsPerPixle), "OpenGL Game", sf::Style::Close, sf::ContextSettings(32, 8, 16, 4, 2));
Engine.Init();
while(Window.IsOpen())
{
sf::Event Event;
while(Window.PollEvent(Event))
{
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
Window.SetActive();
Engine.Render();
Window.Display();
}
return 0;
}