PDA

View Full Version : Don't we need glew?



cuongth
03-31-2016, 04:01 AM
I don't use GLEW, after compiled it shows triangle


//#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <stdio.h>
#include <cstdlib>

// Shader sources
const GLchar* vertexSource =
"#version 150 core\n"
"in vec2 position;"
"void main()"
"{"
" gl_Position = vec4(position, 0.0, 1.0);"
"}";
const GLchar* fragmentSource =
"#version 150 core\n"
"out vec4 outColor;"
"void main()"
"{"
" outColor = vec4(1.0, 1.0, 1.0, 1.0);"
"}";

int main()
{
// Load GLFW and Create a Window
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr); // Windowed
if (window == nullptr) {
fprintf(stderr, "Failed to Create OpenGL Context");
return EXIT_FAILURE;
} else {
fprintf(stdout, "Window [ok]");
}
// Create Context
glfwMakeContextCurrent(window);

// Initialize GLEW
// glewExperimental = GL_TRUE;
// glewInit();

// Create Vertex Array Object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers(1, &vbo);

GLfloat vertices[] = {
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f };

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

// Create and compile the vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);

// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);

// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);

// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

// Rendering Loop
while (glfwWindowShouldClose(window) == false) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);

// Clear the screen to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

// Draw a triangle from the 3 vertices
glDrawArrays(GL_TRIANGLES, 0, 3);

glfwSwapBuffers(window);
glfwPollEvents();

}
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);

glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);

glfwTerminate();
return EXIT_SUCCESS;
}

Compile

g++ -o triangle1 triangle1.cpp -std=c++11 -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -DGL_GLEXT_PROTOTYPES

When I use GLEW (uncommented lines: 1st, 45th, 46th) and compile by

g++ -o triangle1 triangle1.cpp -std=c++11 -lGLEW -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -DGL_GLEXT_PROTOTYPES
When executing triaingle1, there's Segmentation fault with backtrace

Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00007fd606c5e26d in XQueryExtension () from /usr/lib64/libX11.so.6
[Current thread is 1 (Thread 0x7fd607890740 (LWP 2833))]
(gdb) bt
#0 0x00007fd606c5e26d in XQueryExtension () from /usr/lib64/libX11.so.6
#1 0x00007fd606c51672 in XInitExtension () from /usr/lib64/libX11.so.6
#2 0x00007fd606f88781 in __glXInitialize () from /usr/lib64/libGL.so.1
#3 0x00007fd606f841b1 in glXQueryVersion () from /usr/lib64/libGL.so.1
#4 0x00007fd60746e5e4 in glxewContextInit () from /usr/lib64/libGLEW.so.1.10
#5 0x00000000004013e5 in main () at triangle1.cpp:46

My openGL version is 3.2. I don't need to find address of the function glGenBuffers, just use it! It doesn't look like what I've learned from some online tutorials. Could you help me to figure out the reason?