VBO and VAO problem with OpenGL 4.2

Started by
3 comments, last by MrDarkKnight 12 years, 3 months ago
Hello guys
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.


ddddoi.png


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;
}
Advertisement
could you please post the call stack, and what was the last line (in your code) that has been executed?

could you please post the call stack, and what was the last line (in your code) that has been executed?


ok here is the call stack

00000000()
> OpenGL Game.exe!Engine::CreateSquare() Line 55 + 0x15 bytes
OpenGL Game.exe!Engine::Init() Line 21
OpenGL Game.exe!main() Line 11
OpenGL Game.exe!__tmainCRTStartup() Line 555 + 0x19 bytes
OpenGL Game.exe!mainCRTStartup() Line 371
kernel32.dll!7570339a()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77869ef2()
ntdll.dll!77869ec5()



void Engine::CreateSquare()
{
.........

Vertices[15] = 0.5; Vertices[16] = 0.5; Vertices[17] = 0.0; // Top Right corner

//the software stops at this line of code
glGenVertexArrays(1, &VAO_ID[0]); // Create our Vertex Array Object
.......
}
Hi,

Perhaps you forgot to initialize glew, before creating any resource?
(When I comment this out in some working code of mine it throws me the exact same exception.)

if(glewInit() != GLEW_OK)
cout << "GLEW init failed!" << endl;


Cheers!

Hi,

Perhaps you forgot to initialize glew, before creating any resource?
(When I comment this out in some working code of mine it throws me the exact same exception.)

if(glewInit() != GLEW_OK)
cout << "GLEW init failed!" << endl;


Cheers!


thank you so much.
it works perfectly :D

This topic is closed to new replies.

Advertisement