Jump to content
  • Advertisement
Sign in to follow this  
Mekan

OpenGL Problem with rendering single Cube OpenGL SDL2

This topic is 854 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()
{
	glUseProgram(program);
	glBindVertexArray(vao);

	glDisable(GL_CULL_FACE);
	glPushAttrib(GL_ALL_ATTRIB_BITS);

	if (wireFrame)
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	else
		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);

	glPopAttrib();
	glBindVertexArray(0);
}

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;
							displayCube();

							//Update screen
							SDL_GL_SwapWindow(gWindow);

						}
						else
						{
							wireFrame = true;
							displayCube();

							//Update screen
							SDL_GL_SwapWindow(gWindow);
						}
						//std::cout << "wireframe value " << wireFrame << std::endl;
					}		
				}
			}
			displayCube();
			SDL_GL_SwapWindow(gWindow);
		}

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 . 

 

Thanks. 

Share this post


Link to post
Share on other sites
Advertisement

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 . 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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 -> https://github.com/ocornut/imgui/tree/master/examples/sdl_opengl3_example

 

I got the examples working fine (http://prntscr.com/bi9lzz) 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
            ImGui::NewFrame();
            // 3) most of your application code here
            ImGui::Begin("My window");
            ImGui::Text("Hello, world.");
            ImGui::End();
            MyGameUpdate(); // may use ImGui functions
            MyGameRender(); // may use ImGui functions
            // 4) render & swap video buffers
            ImGui::Render();
            SwapBuffers();
        }

Considering it,  i have created my main function  : 

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

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

	initCube();
	// Setup ImGui binding
	ImGui_ImplSdlGL3_Init(gWindow);

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

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

		// 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);
		ImGui::End();

		// 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);
		glClear(GL_COLOR_BUFFER_BIT);
		displayCube();
		ImGui::Render();
		SDL_GL_SwapWindow(gWindow);
	}

	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 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!