So, im just starting GL with sfml. When i try to pushGLStates to draw a simple circle i get GL_INVALID_OPERATION in current state in the console.
My textured quad i try to draw afterwards comes out fine.
the problems starts when i bind my first vertexarray at line 43
//OpenGL
#include <gl\glew.h>
#include <gl\freeglut.h>
#include <glm\glm.hpp>
#include <glm\ext.hpp>
//SFML
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML\OpenGL.hpp>
//Std
#include <string>
#include <iostream>
//Stuff
#include "ShaderManager.hpp"
#include "Convertion.hpp"
//test
#include "Cube.hpp"
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL");
GLenum GlewInitResult;
GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult) {
fprintf(
stderr,
"ERROR: %s\n",
glewGetErrorString(GlewInitResult)
);
exit(EXIT_FAILURE);
}
sf::Clock clock;
GLuint program = ShaderManager::LoadShaders("Simple_vertex.glsl", "Simple_fragment.glsl");
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
Cube* cube = new Cube();
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, cube->getVertices().size() * sizeof(float), &cube->getVertices()[0], GL_STATIC_DRAW);
static const GLfloat g_uv_buffer_data[] = {
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0 };
// This will identify our vertex buffer
GLuint UVbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &UVbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, UVbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
// load resources, initialize the OpenGL states, ...
glm::mat4 Projection = glm::perspective(75.0f, 4.0f/3.0f, 0.1f, 100.0f);
glm::mat4 View = glm::lookAt(glm::vec3(0,0,-10), glm::vec3(0,0,0), glm::vec3(0,1,0)); // pos, looking at, upvector
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * View * Model;
GLuint MatrixID = glGetUniformLocation(program, "MVP");
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
glEnable(GL_TEXTURE_2D);
GLuint texture = 0;
{
sf::Image image;
if (!image.loadFromFile("lolcat.png"))
return EXIT_FAILURE;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.getSize().x, image.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
glBindTexture(GL_TEXTURE_2D, texture);
GLuint TextureID = glGetUniformLocation(program, "textureSampler");
glUniform1i(TextureID, 0);
sf::CircleShape shape(50);
shape.setFillColor(sf::Color(100,250,20));
// run the main loop
bool running = true;
while (running)
{
// handle events
sf::Event Event;
while (window.pollEvent(Event))
{
if (Event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (Event.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, Event.size.width, Event.size.height);
}
}
// clear the buffers
glClear(GL_DEPTH_BUFFER_BIT);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// UV
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, UVbuffer);
glVertexAttribPointer(
1, // attribute 0. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glUseProgram(program);
glUniformMatrix4fv(MatrixID,1,GL_FALSE, &MVP[0][0]);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 6); // Starting from vertex 0; 3 vertices total -> 1 triangle
glUseProgram(0);
// end the current frame (internally swaps the front and back buffers)
window.pushGLStates();
window.draw(shape);
window.popGLStates();
window.display();
}
// release resources...
return 0;
}
Any help would be appriciated :-)