I'm having some trouble using AntTweakBar with modern opengl (dynamic pipeline) and glfw3. I'm using some shaders and i'm pretty sure the problem is something about the camera or the shaders. The keyboard shortcuts are working but I don't have the fancy window I should have :(
Here is the source
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#if __cplusplus <= 199711L
#define nullptr NULL
#endif
#include <cmath>
#include <AntTweakBar.h>
const GLchar *vxShaderSrc = R"(
#version 150 core
in vec2 position;
in vec4 color;
uniform vec2 offset;
out vec4 Color;
void main()
{
Color = color;
gl_Position = vec4(position+offset, 0.0, 1.0);
}
)";
const GLchar *fragShaderSrc = R"(
#version 150 core
in vec4 Color;
out vec4 outColor;
void main()
{
outColor = Color;
}
)";
inline void TwEventMouseButtonGLFW3(GLFWwindow* window, int button, int action, int mods)
{TwEventMouseButtonGLFW(button, action);}
inline void TwEventMousePosGLFW3(GLFWwindow* window, double xpos, double ypos)
{TwMouseMotion(int(xpos), int(ypos));}
inline void TwEventMouseWheelGLFW3(GLFWwindow* window, double xoffset, double yoffset)
{TwEventMouseWheelGLFW(yoffset);}
inline void TwEventKeyGLFW3(GLFWwindow* window, int key, int scancode, int action, int mods)
{TwEventKeyGLFW(key, action);}
inline void TwEventCharGLFW3(GLFWwindow* window, int codepoint)
{TwEventCharGLFW(codepoint, GLFW_PRESS);}
inline void TwWindowSizeGLFW3(GLFWwindow* window, int width, int height)
{TwWindowSize(width, height);}
int main()
{
if (!glfwInit()) {
std::cerr<<"Error initializing glfw...\n";
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
#ifdef __APPLE__ // TODO is it ok to use it on Windows and Linux?
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
TwBar *bar;
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr); // Windowed
if (!window) {
std::cerr<<"Error creating window...\n";
glfwTerminate();
return 2;
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); // can be GLFW_CURSOR_HIDDEN
// Initialize AntTweakBar
TwInit(TW_OPENGL, NULL);
// Create a tweak bar
bar = TwNewBar("TweakBar");
TwWindowSize(800, 600);
int wire = 0;
float bgColor[] = { 0.1f, 0.2f, 0.4f };
TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar.
// Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire,
" label='Wireframe mode' key=w help='Toggle wireframe display mode.' ");
// Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");
// Set GLFW event callbacks
// - Redirect window size changes to the callback function WindowSizeCB
glfwSetWindowSizeCallback(window, (GLFWwindowposfun)TwWindowSizeGLFW3);
glfwSetMouseButtonCallback(window, (GLFWmousebuttonfun)TwEventMouseButtonGLFW3);
glfwSetCursorPosCallback(window, (GLFWcursorposfun)TwEventMousePosGLFW3);
glfwSetScrollCallback(window, (GLFWscrollfun)TwEventMouseWheelGLFW3);
glfwSetKeyCallback(window, (GLFWkeyfun)TwEventKeyGLFW3);
glfwSetCharCallback(window, (GLFWcharfun)TwEventCharGLFW3);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_NO_ERROR) {
std::cerr<<"Error initializing GLEW...\n";
glfwTerminate();
return 1;
}
GLuint vao;
glGenVertexArrays(1, &vao);
std::cout << "Created vertex array with id " << vao << "\n";
glBindVertexArray(vao); // save the calls of vertexattribpointer and others, MUST BE before the EBO
GLuint vbo;
glGenBuffers(1, &vbo); // Generate 1 buffer
std::cout << "Created Arrray with id " << vbo << "\n";
glBindBuffer(GL_ARRAY_BUFFER, vbo);
GLfloat vertices[] = {
-0.5f, -0.5f, 1.f, 1.f, 1.0f, 1.f,
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.f,
0.5f, -0.5f, 0.0f, 0.f, 1.0f, 1.f,
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.f,
// some extra for elements testing
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 1.f, // Vertex 1: Red
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.f, // Vertex 2: Green
-0.25f, -0.5f, 0.0f, 0.f, 1.0f, 1.f // Vertex 3: Blue
};
// size is actually sizeof(float)*vertices.length
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// elemets buffer object allows to use the same vertex multiple times
GLuint elements[] = {
0, 1, 2, 3
};
GLuint ebo;
glGenBuffers(1, &ebo);
std::cout << "Created elements with id " << ebo << "\n";
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
// create shaders
GLint status; // for error checking
char buffer[512];
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vxShaderSrc, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
std::cerr<<"Error in shader: "<<buffer<<"\n";
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragShaderSrc, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
glGetShaderInfoLog(fragmentShader, 512, NULL, buffer);
std::cerr<<"Error in shader: "<<buffer<<"\n";
}
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
// bind the output
// this is the actual output by default
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram); // called everytime somethign change for the shader
glUseProgram(shaderProgram);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), 0);
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE,
6*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
GLint uniPos = glGetUniformLocation(shaderProgram, "offset");
while(!glfwWindowShouldClose(window))
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glPolygonMode( GL_FRONT_AND_BACK, wire?GL_LINE:GL_FILL );
float time = (float)glfwGetTime();
// Clear the screen to black
glClearColor(bgColor[0], bgColor[1], bgColor[2], 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniform2f(uniPos, cos(time*4.f)*0.5f, sin(time*4.f)*0.5f);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, 0); // we can share vertex and we specify indexes
glUniform2f(uniPos, 0.f, 0.f);
// Draw tweak bars
//glUseProgram(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
TwDraw();
glUseProgram(shaderProgram);
glBindVertexArray(vao);
//glBindBuffer(GL_ARRAY_BUFFER, vbo);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glfwSwapBuffers(window);
glfwPollEvents();
}
// clean up
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
TwTerminate();
glfwTerminate();
return 0;
}
You can compile the code with
g++ src/main.cpp -std=c++11 -DGLEW_STATIC -lGLEW -lglfw3 -framework OpenGL -lAntTweakBar
or
mingw32-g++.exe main.cpp -std=c++11 -DGLEW_STATIC -lglew32 -lglfw3 -lglu32 -lopengl32 -lgdi32 -lAntTweakBar
Adapt as needed
I found the solution: Since I'm using CORE:
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
I need to change this TwInit(TW_OPENGL, NULL);
to TwInit(TW_OPENGL_CORE, NULL);
I had to patch the homebrew AntTweakBar version to make this work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With