Triangle does not display on window

My source code is available on github https://github.com/joy7701/My1RenderEngine . After hours of debugging i am confused.
There are some global actions in my render:

  1. Load mesh into VBO and create specification of this data into VAO
  2. Compile Shaders and create program
  3. Use program, bind VAO and then draw

But i don’t see the triangle. Where is my error?
Thanks!

Hello!
This is an excellent example problem you posted here, because you have made a mistake that every programmer must make in his life to learn from it: synchronisation in multithreading.

First of all, it’s a good idea to use multiple threads to separate work:

RenderSystem myRenderSystem; // create render system
std::thread RenderSystemLoop(&RenderSystem::StartLoop , &myRenderSystem); // start our system
std::thread LoadModel(&RenderSystem::Add3DMesh, &myRenderSystem, &MeshInfoFormatID, &MeshID, &vertices[0], sizeof(vertices), true); // load 3d mesh
//	std::cout << VertexShaderSource <<std::endl;
std::thread LoadShader(&RenderSystem::AddShader, &myRenderSystem, &VertexShaderSource, &FragmentShaderSource, &shaderProgram);
std::thread DrawData(&RenderSystem::Draw3DMesh, &myRenderSystem, &MeshInfoFormatID, &shaderProgram);
LoadShader.join();
DrawData.join();
LoadModel.join();
RenderSystemLoop.join();

It’s also nice you are using C++11. There is only one big problem: Your code does not yet contain any synchronisation like std::mutex, std::shared_mutex or std::conditional_variable.

See, when you are using DrawData, you must make sure that LoadModel and LoadShader are finished, otherwise the function throws an exception and the program crashes. This waiting for other stuff to be ready is not being done automatically for you in C++.

But there are more things you must consider when using multiple threads: 2 (or more) threads can never write to the same data or read/write from it. You must use std::mutex or std::shared_mutex to control that only one thread has write access to the data at the same time. 2 or more threads can read from the same data at the same time without problem though, it’s about the write/write and write/read that causes problems.

Synchronisation in multithreading is tricky, because you have to think a lot about your program structure and the possible combination of errors that could occur is huge. I get a different error every time I run your program because the order of which threads are executed is not defined! You have to synchronise it with the techniques I mentioned yourself.

To solve this, you must think about which variables are read or written from which threads and protect them. I wanted to show you a solution but I have to think about it as well for a moment.

Check out tutorial about synchronisation like mutex, shared_mutex and conditional_variable.

Johannes.

And another thing to consider specifically when using multiple threads with OpenGL: the current context is per-thread state, and a context can only be current to one thread at a time. So unless you create a separate context for each thread, you can only call OpenGL commands from one thread at a time.

1 Like

Thank you! I call openGL functions just from single thread. Another threads just add requests for the main thread

Okay. But as IAmNotHanni notes, you still need to ensure that the consumer thread doesn’t try to use data until the producer thread has actually generated it. This is typically done using condition variables.

1 Like

Maybe you should take time and learn all this multithreading and synchronisation first. Create some projects which do not do any rendering but do simple tasks. The time you invest into this is worth every second! Multithreading is an essential skill.

Hello! Now i am using Automatic management of locks with std::lock_guard. Using the example from your article. I created a mutex. In each class method, I created lock_guard. But the triangle is still not displayed. I updated the repository

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.