Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix GL_INVALID_ENUM error in function call?

Tags:

opengl

#include <typeinfo>
#include "stdafx.h"

MainChar::MainChar(){ vboID = 0; vboIndexID = 0; glGetError(); }

MainChar::~MainChar(){}

void MainChar::MainChar_VBO_Func()
{
int error = glGetError();
if (error == GL_INVALID_ENUM){ std::cout << "Error before I even started!!! : " << error << "\n"; };

std::vector<GLdouble> mapVector{
-15.000000, -15.000000, 115.000000, -0.577349, 0.577349, 0.577349, -15.000000, -15.000000, -151.000000, -0.577349, 0.577349, -0.577349,
15.000000, -15.000000, -115.000000, -0.577349, -0.577349, -0.577349, 15.000000, -15.000000, 115.000000, -0.577349, -0.577349, 0.577349,
-15.000000, 15.000000, 115.000000, 0.577349, 0.577349, -0.577349, -15.000000, 15.000000, -115.000000, 0.577349, -0.577349, -0.577349,
15.000000, 15.000000, -115.000000, 0.577349, 0.577349, 0.577349, 15.000000, 15.000000, 115.000000, 0.577349, -0.577349, 0.577349
                                };

//if (error != GL_NO_ERROR){ std::cout << "Error during mapVector decl: " << error << "\n"; }

// Setup Vertex Buffer Object
GLuint vboID = 0;
//if (error != GL_NO_ERROR){ std::cout << "Error during vboID decl: " << error << "\n"; }

glGenBuffers(1, &this->vboID);
//if (error != GL_NO_ERROR){ std::cout << "Error during GenBuffer call-VBO: " << error << "\n"; }

glBindBuffer(GL_ARRAY_BUFFER, this->vboID);
//if (error != GL_NO_ERROR){ std::cout << "Error during BindBuffer call-VBO: " << error << "\n"; }

glBufferData(GL_ARRAY_BUFFER, mapVector.size()*sizeof(GLdouble), &mapVector[0], GL_STATIC_DRAW);
//if (error != GL_NO_ERROR){ std::cout << "Error during BufferData call-VBO: " << error << "\n"; }

}

GLuint MainChar::MainChar_VBO_IndexBuffer_Func()
{

/* Index Buffer Initialization */

/* Index Buffer Data */

std::vector<GLuint> indexBuffer{
5, 1, 6, 2, 2, 3, 1, 4, 6, 2, 7, 5, 3, 6, 2, 3, 7, 5, 8, 7, 4, 8, 3, 6,
8, 7, 5, 1, 1, 4, 4, 8, 1, 4, 2, 3, 3, 6, 4, 8, 8, 7, 7, 5, 6, 2, 5, 1
};

// Set-up Index Buffer Object

    int error = glGetError();

glGenBuffers(1, &this->vboIndexID);
    if (error != GL_NO_ERROR){ std::cout << "Error during GenBuffer call-INDEX: " << error << "\n"; }

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vboIndexID);
if (error != GL_NO_ERROR){ std::cout << "Error during BindBuffer call-INDEX: " << error << "\n"; }

glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.size()*sizeof(GLuint), &indexBuffer[0], GL_STATIC_DRAW);
if (error != GL_NO_ERROR){ std::cout << "Error during BufferData call-INDEX: " << error << "\n"; }

return indexBuffer.size();

The MainChar_VBO_Func() is called right after a check of glGetError to make sure no issues occur right beforehand, and the moment the func is called, the error is logged. I don't even pass an enum value when it log the error. Is it possibly not finding the gl context? GLEW is def initiated, so I am unsure what the problem is.

The following is code which precedes and immediately follows the func call in question:

int main(int argc, char *argv[])
{

//INIT SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);

/*TODO: Check that we have OpenGL */
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||(displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {}

SDL_GL_CreateContext(displayWindow);
//SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

glewInit();
int error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during glewInit call: " << error << "\n"; };

Display_InitGL();
Display_SetViewport(800, 600);
if (error != GL_NO_ERROR){ std::cout << "Error during Display Set Viewport Issue: " << error << "\n"; };


// SET UP TEST OBJ
MainChar *player = new MainChar();
player->MainChar_VBO_Func();
unsigned int size = player->MainChar_VBO_IndexBuffer_Func();
float count = 0.0;
// END SET UP OF TEST OBJ


// Create VAO. Don't forget to enable all necessary states because the VAO starts with default state, cleaning all states prev called to do so.
GLuint VaoId;
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_ELEMENT_ARRAY_BUFFER);
// End VAO init
like image 397
SmugDoodleBug Avatar asked Sep 14 '26 15:09

SmugDoodleBug


1 Answers

  • Do error = glGetError(); after each other call to the GL API for obvious reasons
  • Note, that glGetError() may return multiple error enumerators, so you'd need to call this function in a loop until it returns GL_NO_ERROR or the errors will remain. Clearing errors this way before calling another GL function is possibly also necessary (one does usually not want to clear errors after every single GL call). However, I encountered sort of a bug lately, which caused an endless loop because glGetError() continuously returned the same error.
  • Attach the gDEBugger, to avoid error check clutter
  • If available (at runtime), use ARB_debug_output in a debug mode of your program, for the same reason.
like image 154
Sam Avatar answered Sep 18 '26 11:09

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!