Jump to content
  • Advertisement
Sign in to follow this  
jza

OpenGL Mashing together OpenGL 2.1 and SDL 2.0

This topic is 1783 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

Hi!

 

As SDL 2.0 just came out, I was trying to create some code which would enable a modern OpenGL context along SDL. However I can get only the background color (glClearColor) to work. Interestingly, I can draw legacy OpenGL to on the same context and it appears fine.

 

So below is the code. It combines the OpenGL 2.1 port of the tutorials at opengl-tutorial org and, an OpenGL SDL example at opengl.org (linked at libsdl.org): http://www.opengl.org/wiki/Tutorial1:_Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL_(C_/_SDL)

 

It's a bit hacky and I omitted some parts but can include them if necessary. Yes, the shader code files are in the correct directory. Can you see anything obviously wrong with the stuff below?

int main(int argc, char** argv) {
	SDL_Window *MainWindow;
	SDL_GLContext MainContext;

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		ShowErrorAndDie("Unable to initialize SDL");
	}

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

	// 24bit double buffer
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

	MainWindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_HIDDEN);
	if (!MainWindow) {
		ShowErrorAndDie("Unable to create window.");
	}

	// Attach the OpenGL context to our window
	MainContext = SDL_GL_CreateContext(MainWindow);
	CheckSDLErrorAndDie("Unable to initialize OpenGL.\nPlease make sure that your OpenGL driver supports at least version 2.1.");

	SDL_ShowWindow(MainWindow);

	// Enable vsync
    SDL_GL_SetSwapInterval(1);

	if (GLEW_OK != glewInit()) {
		ShowErrorAndDie("The GLEW subsystem failed to initialize.");
		SDL_Quit();
		exit(1);
	}

    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

    // Create and compile our GLSL program from the shaders
    GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );

    // Get a handle for our buffers
    GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");

    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
         1.0f, -1.0f, 0.0f,
         0.0f,  1.0f, 0.0f,
    };

    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    while (1){

        // Clear the screen
        glClear( GL_COLOR_BUFFER_BIT );

        // Use our shader
        glUseProgram(programID);

        // 1rst attribute buffer : vertices
        glEnableVertexAttribArray(vertexPosition_modelspaceID);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
            vertexPosition_modelspaceID, // The attribute we want to configure
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );

        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

        glDisableVertexAttribArray(vertexPosition_modelspaceID);

        // Swap buffers
        SDL_GL_SwapWindow(MainWindow);

        while (SDL_PollEvent(&InputEvent)) {
            if (InputEvent.type == SDL_KEYDOWN) {
                if (InputEvent.key.keysym.sym == SDLK_ESCAPE) {
                    goto ExitLoop;
                }
            }
        }
    }
ExitLoop:
	glDeleteBuffers(1, &vertexbuffer);
	SDL_GL_DeleteContext(MainContext);
	SDL_DestroyWindow(MainWindow);
	SDL_Quit();
}
Edited by jza

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!