#include <iostream>
#include <GLFW/glfw3.h>
#include <math.h>
const GLchar* vertex120 = R"END(
#version 120
uniform mat4 matrix;
attribute vec4 inColor;
attribute vec4 inPosition;
varying vec4 outColor;
void main()
{
outColor = inColor;
gl_Position = matrix * inPosition;
}
)END";
int main() {
GLFWwindow* window;
if (!glfwInit()) {
std::cout << "Init error";
exit(-1);
}
window = glfwCreateWindow(800, 800, "Hello", 0, 0);
if (!window) {
std::cout << "Window creation error";
glfwTerminate();
exit(-1);
}
glfwMakeContextCurrent(window);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertex120, 0);
glCompileShader(vertexShader);
GLint compilationStatus;
glGetShaderiv(vertexShader, GL_COMPLIE_STATUS, &compilationStatus);
if (compilationStatus == GL_FALSE) {
GLchar message[256];
glGetShaderInfoLog(vertexShader, sizeof(message), 0, &message[0]);
std::cout << message;
exit(-1);
}
while (!glfwWindowShouldClose(window)) {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
I am following a tutorial on using opengl, everything was working out until the above code where I get undefined errors for GLchar*, glCreateShader, glShaderSource. Everything else works so I wasn't able to figure out whats wrong please help.
OpenGL is implemented by the graphics driver as a bunch of functions implemented, not as a standalone library, therefore cannot be simply linked to your project. To call a function you have to ask OpenGL for a pointer to that function and you have to do that with all the functions you will call.
You can do that by functions such as GetProcAddress... but it depends on the platform you're developing for. to get the function pointer for all that functions it's an exhausting work, for that, there is some libraries like GLEW/GLAD to do that at runtime from the driver for each OpenGL function.
so this is what exactly happens with your code, your program does not know what is this glCreateShader, to fix this you only need the magic line in your code which is
#include <gl/glew.h>
#include <GLFW/glfw3.h>
note that you need to include GLEW before GLFW. and your program will recognize the functions.
also you have to initialize GLEW after you create the context, you can do it by:
glewExperimental = GL_TRUE; //to avoid using the deprecated GL functions
if (glewInit() != GLEW_OK) {
std::cerr << "GLEW::Error : failed to initialize GLEW"<< std::endl;
}
And don't forget the library linking part. I found here a tutorial to install GLEW with visual studio http://www.mcihanozer.com/tips/setting-up-libraries/setting-up-glew-for-visual-studio/ and to download GLEW http://glew.sourceforge.net/
PS: to use GLEW as dynamic library (DLL) may raise some problems, if that the case you can use the static version, and that is by typing #define GLEW_STATIC at the beginning of your code and at the linking put the static version of the library. this is just an example with GLEW which is a bit older than GLAD, if you wish to use GLAD it is also a great option.