Advertisement Jump to content
Sign in to follow this  

OpenGL Problem with rendering single Cube OpenGL SDL2

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

Good morning everyone, 


I am trying to migrate a simple library from GLFW to SDL 2.0 but i am having an "unusual" , at least for me, issue. 


This is the code for displayCube :

void displayCube()


	if (wireFrame)
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	translate = glm::translate(glm::mat4(1.0), glm::vec3(-0.5, 0.0, 0.0));
	glUniformMatrix4fv(TranslateMat, 1, GL_FALSE, glm::value_ptr(translate));

	glDrawArrays(GL_TRIANGLES, 0, NumVertices);


This is the main loop : 

//While application is running
		while (!running){
			//Handle Events on queue
			while (SDL_PollEvent(&e) != 0)
				//User requests quit
				if (e.type == SDL_QUIT)
					running = true;
				if (e.type == SDL_KEYDOWN)
					SDL_Scancode keyPressed = e.key.keysym.scancode;

					if (keyPressed == SDL_SCANCODE_W){
						if (wireFrame)
							wireFrame = false;

							//Update screen

							wireFrame = true;

							//Update screen
						//std::cout << "wireframe value " << wireFrame << std::endl;

You can see the result here : 



(This happens way faster than is captured on  video)


and when you close the window i have put a small delay before destroying it and you can see from the screen shot that the cube stablelizes . 



Share this post

Link to post
Share on other sites

Hi ,
Sorry for that . I think it's fine now .

Pasting the link again just to make sure .





UPDATE/EDIT: I have solved the problem on my own . On the main Loop these 2 lines needed to be added in order to perform correctly . 

glClearColor(0.0, 0.0, 0.3, 1.0); //black color

I had forgot to clear the buffers before displayCube() was called again . 

But i couldn't find it because instead for looking for something like this i though it had to do something with the vSync . 

Edited by Mekan

Share this post

Link to post
Share on other sites

Hey , 


i am back with new troubles . 


So i am past the point of integrating SDL2 alongside with OpenGL (and a variaty of other opensource libs such as assimp/glm/glew e.t.c) and i moved forward to put ImGUI on the game too .

For starters,  what i did was create a .lib file out of ImGUI  alongside with 

imgui_impl_sdl_gl3.cpp and imgui_impl_sdl_gl3.h which are the example files for ImGUI + SDL2 + OpenGL  in github ->


I got the examples working fine ( as you can see in the screenshot and ImGUI looks really cool indeed , but now i want to render also my cube inside the window . 


According to the little documentation of ImGUi (most of it inside of imgui.cpp) this is a typical application skeleton : 

        // Application init
        ImGuiIO& io = ImGui::GetIO();
        io.DisplaySize.x = 1920.0f;
        io.DisplaySize.y = 1280.0f;
        io.IniFilename = "imgui.ini";
        io.RenderDrawListsFn = my_render_function;  // Setup a render function, or set to NULL and call GetDrawData() after Render() to access the render data.
        // TODO: Fill others settings of the io structure
        // Load texture atlas
        // There is a default font so you don't need to care about choosing a font yet
        unsigned char* pixels;
        int width, height;
        io.Fonts->GetTexDataAsRGBA32(pixels, &width, &height);
        // TODO: At this points you've got a texture pointed to by 'pixels' and you need to upload that your your graphic system
        // TODO: Store your texture pointer/identifier (whatever your engine uses) in 'io.Fonts->TexID'
        // Application main loop
        while (true)
            // 1) get low-level inputs (e.g. on Win32, GetKeyboardState(), or poll your events, etc.)
            // TODO: fill all fields of IO structure and call NewFrame
            ImGuiIO& io = ImGui::GetIO();
            io.DeltaTime = 1.0f/60.0f;
            io.MousePos = mouse_pos;
            io.MouseDown[0] = mouse_button_0;
            io.MouseDown[1] = mouse_button_1;
            io.KeysDown[i] = ...
            // 2) call NewFrame(), after this point you can use ImGui::* functions anytime
            // 3) most of your application code here
            ImGui::Begin("My window");
            ImGui::Text("Hello, world.");
            MyGameUpdate(); // may use ImGui functions
            MyGameRender(); // may use ImGui functions
            // 4) render & swap video buffers

Considering it,  i have created my main function  : 

int main(int, char**)
	int running = true;

	if (init() == false)
		std::cout << "Init failed !!!" << std::endl;

	// Setup ImGui binding

	ImVec4 clear_color = ImColor(114, 144, 154);

	// Main loop
	bool done = false;
	while (!done)
		SDL_Event event;
		while (SDL_PollEvent(&event))
			if (event.type == SDL_QUIT)
				done = true;

		// 1. Show a simple window
		// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
		ImGui::SetNextWindowSize(ImVec2(100,100), ImGuiSetCond_FirstUseEver);
		ImGui::Begin("basicCube GUI");
		static float f = 0.0f;
		ImGui::Text("Hello, world!");
		ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
		ImGui::ColorEdit3("clear color", (float*)&clear_color);
		ImGui::Button("Test button");
		ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);

		// Rendering
		glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
		glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);

	close(); //Shuts down every little thing...
	return 0;

My problem is that there is no cube in my screen , just what you saw in the screen shot above . 

If i try to render the cube alone it works fine , the problem seems to be when i mix ImGUI functions with mine .


Anybody got any ideas or has faced anything similar?


Thanks in advance . 

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!