Jump to content
  • Advertisement
Sign in to follow this  
arasmussen

OpenGL Object Oriented Approach to OpenGL

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

I'm trying to take my game from a very long main.cpp file to several different classes. I've made a Game class to handle the creation and destruction of my GL context/window, and updating the buffer. When I try to use immediate mode, it works properly, with textures/vertices, which means I must have a valid rendering context. However, when I try to use glGenBuffers (getting the function pointer from #include <gl/GLee.h>, definitely works), it crashes.

Here's my code:

entry point + main loop


int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
bool done = false;

game = new Game(hInstance);

if (!game->CreateGLWindow())
{
delete game;
return 0;
}

while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
done=true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
if ((active && !game->DrawGLScene()) || game->isEscapePressed())
{
done=true;
}
else
{
game->SwapGLBuffers();
}

if (!game->HandleInput())
done = true;
}
}

game->KillGLWindow();
delete game;

return (msg.wParam);
}


Game init (without GL window init)


GLint Game::InitGL(GLvoid)
{
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

gltext = new GLText(hDC);
framerate = new Framerate();

LoadGLTextures("Data/dirt.bmp", &texture[0]);
LoadGLTextures("Data/grass.bmp", &texture[1]);
gltext->BuildFont();
GenerateTriangle();

return true;
}


GLvoid Game::GenerateTriangle(GLvoid)
{
vertices = new GLfloat[9];
vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = -0.1f;
vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = -0.1f;
vertices[6] = 0.5f; vertices[7] = 0.5f; vertices[8] = -0.1f;

texcoords = new float[6];
texcoords[9] = 0.0f;
texcoords[10] = 1.0f;
texcoords[11] = 1.0f;
texcoords[12] = 1.0f;
texcoords[13] = 1.0f;
texcoords[14] = 0.0f;

vbuffer = 0;
glGenBuffers(1, &vbuffer); // CRASH ON THIS LINE (in runtime)[
glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
glBufferData(GL_ARRAY_BUFFER, 15 * sizeof(GLfloat), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, 9 * sizeof(GLfloat), vertices);
glBufferSubData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), 6 * sizeof(GLfloat), texcoords);

delete [] vertices;
delete [] texcoords;
}


Note that this EXACT code works when I don't use classes, when it's coded with functional programming style, rather than object oriented. I think my problem has to do with which variables I'm passing to game and when, including hInstance, hWnd, hDC, hRC. Anyone have any ideas?

Share this post


Link to post
Share on other sites
Advertisement

texcoords = new float[6];
texcoords[9] = 0.0f;
texcoords[10] = 1.0f;
texcoords[11] = 1.0f;
texcoords[12] = 1.0f;
texcoords[13] = 1.0f;
texcoords[14] = 0.0f;


Yeah that looks just wrong. you create an array of 6 floats and you change stuff outside the bounds. thats probably messing with everything i wonder why it works when not usng classes

Share this post


Link to post
Share on other sites


texcoords = new float[6];
texcoords[9] = 0.0f;
texcoords[10] = 1.0f;
texcoords[11] = 1.0f;
texcoords[12] = 1.0f;
texcoords[13] = 1.0f;
texcoords[14] = 0.0f;


Yeah that looks just wrong. you create an array of 6 floats and you change stuff outside the bounds. thats probably messing with everything i wonder why it works when not usng classes


Lol! Copied it over wrong. Ignore that :P

(still crashing with same issue though)

Share this post


Link to post
Share on other sites

Hmm what is the debugger saying?


When I get to that function, everything is fine. Once I try to step into it, since it's already compiled in a library, it can't enter it, and it breaks the program saying "Debug assertion failed" and string != NULL.

Share this post


Link to post
Share on other sites
Make sure you're initializing OpenGL extensions in the OO version. Put a breakpoint in the function that initializes them to make sure. Check for last errors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!