Reputation:
Recently, I have been transitioning from LWJGL (which I am experienced with) to C++'s OpenGL API. I fallowed this tutorial since I could not get my vbo classes to work correctly. The code is below:
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <stdlib.h>
#include <stdio.h>
using namespace std;
GLFWwindow* window;
void init()
{
}
static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
void render()
{
glViewport(0, 0, 640, 480);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDeleteBuffers(1, &vertexbuffer);
glfwSwapBuffers(window);
glfwPollEvents();
}
static void errorCallback(int error, const char* desc)
{
fputs(desc, stderr);
}
static void keyCallback(GLFWwindow* window, int key, int scanCode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void initWindow()
{
glfwSetErrorCallback(errorCallback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(640, 480, "Hello, world!", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, keyCallback);
glewExperimental = true;
if (glewInit() != GLEW_OK)
exit(EXIT_FAILURE);
}
void destroyWindow()
{
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
int main(int argCount, char **args)
{
initWindow();
init();
while (!glfwWindowShouldClose(window))
render();
destroyWindow();
}
I have read through it over and over and looked around the internet. However, other posts I've read are clearer mistakes. I am probably making a really obvious mistake here, and I know how inefficient the fact that I create a buffer every frame is, however that is just to test and see if I can get it to work. I would appreciate if somebody could tell me what I am doing wrong here!
Upvotes: 1
Views: 1131
Reputation: 45362
You are using an OpenGL 3.3 core profile. In such a context, the use of Vertex Array Objects (VAO) is mandatory. The same is true for program objects. You have to specify your own shaders and cannot make use of the fixed function pipleline. The glLoadIdentity
call refers to the deprecated matrix stack functionality which is also removed in core profiles.
Your glDrawArrays
call should generate a GL_INVALID_OPERATION
because you have no program object and no VAO bound at the time of the draw call. I generally recommend to add GL error checks at least for debug builds. If the GL version you are targeting supports it, I recommend using a debug context instead. It will make spotting errors during development much easier.
Also note that none of these issues a related to the switch from java with LWJGL to C++. If you used a legacy context / core profile with java before, you could also do so in C++ (although I strongly advise you to learn modern GL instead), and you would have seen the same issues if you used a modern core profile context with LWJGL.
Upvotes: 2