I been following a tutorial to create a simple program which displays a window with an unshaded triangle drawn in it. To do this I am using the glew library and the GLFW library however after adding in the following code:
do{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glVertexAttribPointer(0, 3,GL_FLOAT, GL_FALSE, 0, (VOID*)0);
glDrawArrays(GL_TRIANGLES,0,3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
} while (glfwWindowShouldClose(window) == false);
I got this error (full message not shown):
undefined reference to `glDrawArrays'
I think this could be because of a problem with how my project is linked however other functions from glew are being called without errors which is what confuses me. I'm using NetBeans for my IDE and MinGW64 as my compiler on a 64 bit windows 10 installation. The project includes the following places:
../../../../../Program Files/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/include/GLFW
../../../../../Program Files/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/include
../../../../../Program Files/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/include/GL
And the library files I use are located at these locations:
../../../../../Program Files/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/bin/glew32.dll
../../../../../Program Files/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/lib/glfw3.dll
Related
I'm sorry if this is a duplicate question but I've looked through StackOverflow and Google and haven't come up with an answer yet.
I'm using Code::Blocks and SDL and trying to get GLEW working with openGL.
The program code is:
#include <Windows.h>
#include <SDL.h>
#define GLEW_STATIC
#include <glew.h>
int main(int argc, char *argv[])
{
//initialize SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
SDL_GL_DeleteContext(context);
SDL_Quit();
return 0;
}
When I build it, it returns:
undefined reference to 'glewExperimental'
undefined reference to 'glewInit#0'
undefined reference to '__glewGenBuffers'
It's not returning an error finding glew.h, so I assume it's a problem with the linker finding the .lib files.
I've added the folder where the .lib files are under Project > Build Options > Search Directories > Linker and added 'glew32s', 'glew32' (I've tried switching the precedence on glew32s and glew32), and 'opengl32' under Linker Settings > Link Libraries, and added '-lglew32s', '-lglew32' (again tried switching the precedence and removing one or the other), and '-lopengl32' under Linker Settings > Other linker options.
I even tried adding the .lib files, .h files, and .dll files to the project folder and adding search directories to the project folder. It still returns the same error.
I'm using the most recent version of glew (1.13.0) on a 64-bit OS. I've tried linking to the 64-bit and 32-bit versions (because I'd read most compilers compile in 32-bit by default for compatibility). Am I linking to the wrong files?
Thanks for reading.
This is the environment I currently use: Eclipse-Luna, C++11 on Linux Mint -Rebecca.
When I try to use modern OpenGL like with VAOs or VBOs I get Compiler Errors such that methods could not be resolved.
For Example:
GLuint VaoID; //GLuint is working
glGenVertexArrays(1, &VaoID);
or:
GLuint VboID;
glGenBuffers(1, &VboID);
glBindBuffer(GL_ARRAY_BUFFER, VboID);
glBufferData(GL_ARRAY_BUFFER, vbo_size, data, usage);
I checked the GL/gl.h, GL/glext.h and noticed that I have only got OpenGL 1.x methods in there.
So I checked my OpenGL version glxinfo|grep "OpenGL". Which seems to be fine:
glxinfo|grep "OpenGL"
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Haswell Mobile
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.1.3
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 10.1.3
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
Trying to install or update the packages again only states that everything is up-to-date.
sudo apt-get install freeglut3 freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libglu1-mesa-dev libgl1-mesa-glx libgl1-mesa-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'libglew1.5-dev' for regex 'libglew1.5'
Note, selecting 'libglew-dev' instead of 'libglew1.5-dev'
Note, selecting 'libglew-dev' instead of 'libglew1.5-dev'
freeglut3 is already the newest version.
freeglut3-dev is already the newest version.
libglew-dev is already the newest version.
libglu1-mesa is already the newest version.
libglu1-mesa-dev is already the newest version.
libgl1-mesa-dev is already the newest version.
libgl1-mesa-glx is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 150 not upgraded.
So is there a way of fixing this without manually messing with the include directory?
And if there isn't how do I get an up to date header fitting my OpenGL version?
I checked the GL/gl.h, GL/glex.h and noticed that I have only got
OpenGL 1.x methods in there.
For GL/gl.h, that is actually how it is supposed to be. If you want to use OpenGL in a platform-independent manner, you can only rely on GL 1.1 being exported by the GL lib, and should also not assume anything more in the headers. GL/glext.h should actually contain something more. But by default, it will not provide function declarations for the newer functions. A recent version of that file can always be obtained from the OpenGL website, btw.
For everything beyond GL 1.1, you should use GL's extension mechanism, which basically means that you have to query the function pointers for each and every GL function >= GL 1.2 at run time. The glext.h header provides the function pointer type declartations, and enum constants, and additional data types. So this basically looks like this for each extension (and new core functionality are considered "extensions" in this context):
#ifndef GL_ARB_vertex_buffer_object
#define GL_ARB_vertex_buffer_object 1
// new types
typedef ptrdiff_t GLsizeiptrARB;
typedef ptrdiff_t GLintptrARB;
// constants for GLenum values
#define GL_BUFFER_SIZE_ARB 0x8764
#define GL_BUFFER_USAGE_ARB 0x8765
#define GL_ARRAY_BUFFER_ARB 0x8892
// ...
// function pointer tpes for every function
typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
// ...
#ifdef GL_GLEXT_PROTOTYPES
// function declatations
GLAPI void APIENTRY glBindBufferARB (GLenum target, GLuint buffer);
GLAPI void APIENTRY glDeleteBuffersARB (GLsizei n, const GLuint *buffers);
// ...
#endif
#endif /* GL_ARB_vertex_buffer_object */
So the function declarations are only effective if GL_GLEXT_PROTOTYPES has been defined. But you shouldn't do that. It will only work if the GL lib happens to export these symbols, and that is not required on most platforms.
Usually, one does not want to load hundreds of GL function pointers manually. There are a couple of OpenGL loading libraries which hanlde all this for you under the hood. And GLEW - which you already installed for some reason - is one of those, so you probably want to use it. Note that GLEW has some issues on its own, notably it is somewhat broken when used in conjunction with modern core profile OpenGL contexts, but it can still be used.
I forgot to mention that I had already included GL/glew.h in my headers. But that didn't fix the compile errors. I checked the file glew.h with eclipse and it shows several error messages. The topmost line marked with an error looks like this:
#error glext.h included before glew.h
Apparently order of includes matters here. After some fiddling the problem boiled down to a very simple change of code. Here is what I had:
#include <GLFW/glfw3.h>
#include <GL/glew.h>
Solution looks like this:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
So I just swapped those two lines and it worked. Thanks for your suggestions again.
Also I removed the GL/gl.h completely.
I'm relatively new to c++, so bear with a little.
I have a class with the constructor:
Window(int width, int height, const std::string& title);
As defined in the header file.
I then have the code:
#include "window.h"
int main(int argc, char** argv) {
new Window(800, 600, "Elysian Engine");
}
in Main.
When building, I am getting the error "undefined reference to 'Window(int, int, std::string const&)'" Which I do not understand, as I thought I am correctly importing it and everything. I understand this to be a linking error, but I'm not sure why.
Thanks!
--- EDIT ---
The code for window.cpp:
#include "window.h"
#include <SDL2/SDL.h>
#include <SDL/SDL.h>
#include <GL/glew.h>
Window::Window(int width, int height, const std::string& title) :
width(width),
height(height),
title(title),
isCloseRequested(false) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
}
}
Window::~Window() {
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
}
Because your code does not appear to be causing the issue, I will address the IDE.
When I first started using Code::Blocks, I ran into the "undefined reference" problem quite a bit. There are multiple ways that I solved this.
Exit Code::Blocks and re-open it. This solved my issues more times than I would care to count. It's similar to finally realizing that an executable is acting strangely because you needed to run make clean before compiling; sometimes, you are compiling an old version of code without realizing it.
Delete window.h and window.cpp from the project and re-add them. This is similar to the solution above. Although this has worked for me before, I couldn't figure out exactly why it worked.
Include the full path of window.h. Instead of #include "window.h", try #include "/path/goes/here/window.h". It is possible that your current location (as specified in your #include statement) is incorrect.
Check the compiler that is selected for the project. When I used Code::Blocks, I typically used gcc. However, there was a case where I was receiving all sorts of errors (upon attempting compilation) that I either did not understand or could not resolve, only to learn that I had accidentally selected some gcc variant from the compiler selection list that I did not even have installed.
Create a new project. Move your source files out of the project folder that is created by Code::Blocks, and then remove that project folder. Launch Code::Blocks, create a new project, and add your source files to the new project.
When all else fails and you absolutely must use Code::Blocks, reinstall the full package. I had to do this my first time using Code::Blocks (which was also my first time using an IDE). I realized that I had a bare-bones installation of the IDE, which was limiting what I could do (including compiling). Naturally, I cannot access the Code::Blocks website right now, otherwise I would offer a link here as well. In this case, I think it is safe to say that the full-featured stable version will be the largest (in MB) you can find on Code::Blocks' Downloads page. After you have reinstalled, create a new project and add your files back to it.
I eventually moved to using writing code in an IDE (for the linting!), and taking care of a makefile on my own. It has been awhile since I've used Code::Blocks, but each of the above bullets represents solutions/work-arounds that have helped me for your specific problem (or, in general) while I was using Code::Blocks.
I'm trying to get the Unofficial OpenGL SDK Libraries to work and started using the glutil library for the matrixStack functionality. I compiled the source using Visual studio 2010 and moved the libraries to the minGW folder located at my Code::Blocks folder. I moved the header files to the include folder over there as well.
Everything should be in place now and should be compiled for my OS so the following code should work just fine.
// Get rotation matrix
//float rotValue = (glutGet(GLUT_ELAPSED_TIME)) / 10;
glm::mat3 rotMatrix = RotateAxis(0.0f, 0.0f, 1.0f, 0.0f);
// Build final matrix from rotMatrix
glm::mat4 finalMatrix(rotMatrix);
finalMatrix[3].x = camX;
finalMatrix[3].y = camY;
finalMatrix[3].z = camZ - 1.0f;
finalMatrix[3].w = 1.0f;
glutil::MatrixStack stack(finalMatrix);
glutil::PushStack push(stack);
// Object 1
glBindVertexArray(vaoObject); // Advantage of using vao's is that you only have to do all the vertex attribute enabling and buffer stuff once.
glUniformMatrix4fv(modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(stack.Top()));
glDrawElements(GL_TRIANGLES, ARRAY_COUNT(indexData), GL_UNSIGNED_SHORT, 0);
// Object 2
stack.Translate(glm::vec3(0.0f, 0.5f, 0.0f)); // THIS IS WHERE THINGS GO WRONG
finalMatrix[3].z = camZ - 2.00f;
glUniformMatrix4fv(modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(stack.Top()));
glDrawElementsBaseVertex(GL_TRIANGLES, ARRAY_COUNT(indexData), GL_UNSIGNED_SHORT, 0, 36/2);
The call to stack.Translate generates the following error: undefined reference to 'glutil::MatrixStack::Translate(glm::detail::tvec3 const&)' which is quite odd since without the translate call the constructors for the MatrixStack and PushStacks work just fine which is odd.
I tried adding -glutil to the linker but didn't solve my problem.
Header includes (related to openGL):
// Open GL and GLUT
#include <gl/glew.h>
#include <gl/glut.h>
// Open GL Libraries
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glutil/glutil.h>
I'm still not sure how to find out the correct names to link the libraries to since the OpenGL SDK didn't show this information on their website so I'm not sure if -glutil is correct. I'm guessing the problem has something to do with the linking stage.
Are you using C++ code compiled by VC++ under MinGW? You know that ABI's of those compilers are different? Even the name mangling scheme is different, so that could explain why it cannot be linked. Chack if unmangled version of function name from mingw linker (should be somewhere in error report) maches the one exported from glutil - using eg. DLL Export Viewer - or is glutil statically linked? If it doesn't match, you probably need to use the same compiler for both library and your code.
I downloaded SDL 1.2.14
on Windows 7
and I have Mobility Radeon X1800 driver installed.
I'm using Microsoft Visual C++ 2010 Express.
I added the SDL include and library directories in the "VC++ Directories"
I added the following Additional Dependencies:
opengl32.lib;
glu32.lib;
SDL.lib;
SDLmain.lib;
I added the SDL.dll to my program folder
I didn't add any opengl directories!
#include "SDL.h"
#include "SDL_opengl.h"
bool running = true;
int main(int argc, char* args[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen = SDL_SetVideoMode(640,480,32,SDL_OPENGL);
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 640/480, 1.0, 200.0);
while(running) {
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // Swich to the drawing perspective
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5f, 0.5f, 0.0f);
glVertex3f(-1.0f, 1.5f, 0.0f);
glVertex3f(-1.5f, 0.5f, 0.0f);
glEnd();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
This program draws a simple triangle.
I include 2 header files above and my Opengl code just works!
I don't know if my triangle is done on a the GPU or CPU. And what openGL version I'm using?
I mean i heard that Microsoft don't update there opengl files any longer and that they use CPU implementation of OpenGL 1.1 or something.
How do I know what version of OpenGL I'm using? And can I check at run time?
How do I know if I'm using a CPU or GPU implementation? And can I check at run time?
Thanks for look at my problem.
call glGetString
Here is Microsoft's documentation for glGetString. It just repeats the SGI doc and tells you the function is found in gl.h and opengl32.lib.
Actually when you install your video card driver it "replaces" the opengl existing in your machine, so you will be using that version.
Multiple versions of OpenGL are present at the same time, and which one is used depends on the HDC used to initialize OpenGL. For example, applications running in the local login session can get hardware-accelerated GL while those running in a remote desktop session get the CPU-based implementation ( Ben Voigt )
The currently header and lib that comes with Visual Studio only has OpenGL 1.1 in it, so to access more modern stuff you need to call the wglGetProcAddress to get pointers to the new functions.
Here you can find more information: http://www.opengl.org/wiki/Getting_started