You need to load the OpenGL functions and call them through a pointer. Everything would be fine, but the teacher made the std :: function use instead of the pointer. There is a problem with it, everything seems to start, but it does not work correctly.

Minimum example:

typedef void ( * PGLCLEARCOLORPROC ) (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); std::function <void (GLclampf, GLclampf, GLclampf, GLclampf)> _glClearColor = NULL; void glClearColor ( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) { if(_glClearColor == NULL) _glClearColor = ( PGLCLEARCOLORPROC ) SDL_GL_GetProcAddress( "glClearColor" ); else _glClearColor ( red, green, blue, alpha ); } 

mygl.h

 #ifndef LAB1_HL_S11_V1_MYGL_H #define LAB1_HL_S11_V1_MYGL_H typedef unsigned int GLenum; typedef unsigned int GLbitfield; typedef unsigned int GLuint; typedef int GLint; typedef int GLsizei; typedef unsigned char GLboolean; typedef signed char GLbyte; typedef short GLshort; typedef unsigned char GLubyte; typedef unsigned short GLushort; typedef unsigned long GLulong; typedef float GLfloat; typedef float GLclampf; typedef double GLdouble; typedef double GLclampd; typedef void GLvoid; typedef char GLchar; #define GL_COLOR_BUFFER_BIT 0x00004000 typedef void ( * PGLCLEARPROC ) (GLbitfield mask); typedef void ( * PGLCLEARCOLORPROC ) (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); bool mygl_Init(const char *); void mygl_Quit(); void glClear(GLbitfield mask); void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); #endif //LAB1_HL_S11_V1_MYGL_H 

mygl.cpp

 #include "mygl.h" #include <iostream> #include <functional> #include <SDL2/SDL.h> std::function <void (GLclampf, GLclampf, GLclampf, GLclampf)> _glClearColor = NULL; std::function <void (GLbitfield)> _glClear = NULL; bool mygl_Init(const char *str) { if (SDL_GL_LoadLibrary(str) < 0) { std::cerr << "Error in load library" << std::endl; return false; } return true; } void mygl_Quit() { SDL_GL_UnloadLibrary(); } void glClearColor ( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) { if(_glClearColor == NULL) _glClearColor = ( PGLCLEARCOLORPROC ) SDL_GL_GetProcAddress( "glClearColor" ); else _glClearColor ( red, green, blue, alpha ); } void glClear (GLbitfield mask) { if( _glClear == NULL) { _glClear = (PGLCLEARPROC) SDL_GL_GetProcAddress("glClearColor"); std::cout << "glClear is NULL" << std::endl; } else _glClear (mask); } 

main.cpp

 #include <iostream> #include "mygl.h" #include <SDL2/SDL.h> int main() { SDL_Init(SDL_INIT_EVERYTHING); SDL_Event e; bool quit = false; if (!mygl_Init(NULL)) return 0; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 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_Window *window = SDL_CreateWindow("Lab 1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 480, SDL_WINDOW_OPENGL); if(!window) { std::cerr << "Error in create window" << std::endl; return 0; } SDL_GLContext glContext = SDL_GL_CreateContext(window); if(!glContext) { std::cerr << "Error in create gl context" << std::endl; return 0; } while( e.type != SDL_QUIT && !quit) { while(SDL_PollEvent(&e)) { glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); SDL_GL_SwapWindow(window); if(e.key.keysym.sym == SDLK_q && SDL_GetModState() & KMOD_CTRL) quit = true; } } SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(window); mygl_Quit(); SDL_Quit(); return 0; } 

Result of work: enter image description here

Closed due to the fact that the question’s essence is not clear to the participants from Vlad from Moscow , cheops , αλεχολυτ , aleksandr barakin , HamSter 19 Oct '16 at 6:35 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Give a minimally verifiable example of the problem. No one will understand your code sheets. - Vlad from Moscow
  • @VladfromMoscow added - Vüsal Guseynov
  • 3
    minimal - this means you have to throw out the code until the problem stops playing. - Abyx

1 answer 1

Hello.

So far from possible errors I see incorrect receipt of the pointer to the glClear function. For some reason you are doing:

 _glClear = (PGLCLEARPROC) SDL_GL_GetProcAddress("glClearColor"); 

and should be most likely:

 _glClear = (PGLCLEARPROC) SDL_GL_GetProcAddress("glClear"); 

It is also better to refuse to check for NULL (strange, why not nullptr ?).

There is a good validation operator: http://en.cppreference.com/w/cpp/utility/functional/function/operator_bool