Tag Archives: ProgrammerAH

Solution to the failure of creating bitmap with multithreaded OpenGL

Multithreaded OpenGL failed to create a bitmap solution

July 08, 2010

[b]
[b]
[b]

Recently, I have been working on OpenGL multithreading. The problem I have encountered is that using OpenGL Glgentextures to draw bitmaps in multithreading is not effective. I searched on the Internet for three days and asked a lot of netizens and technical groups, but without exception, I did not get the answer.

Finally, I found relevant information when I searched the Internet yesterday. The solutions are as follows. If you also encounter this problem, I hope I can help you.
Translated from:
[b] [/ b] [b] http://zhidao.baidu.com/question/77393242.html [/ b]

You call it in your thread

wglMakeCurrent to connect the HDC to the currently available HGLRC. Remember, OpenGL’s HGLRC can only be used in one thread at a time, so it is thread-safe. You must call wglMakeCurrent before the thread terminates (NULL, NULL).

if this doesn’t work in your version

suggests you use it when entering threads

wglCreateContext (HDC);

wglMakeCurrent (HDC, HGLRC);

thread exit before use

wglMakeCurrent (NULL, NULL);

wglDeleteContext (HGLRC);

This is usually fine

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

This is fine if you create this control dynamically in your thread

if you are using message thread creation is not line, you can’t use in you create your own thread

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

the reason I said above, OPENGL is thread-safe, its rendering context cannot be used in multiple threads at the same time; In addition: to develop OpenGL procedures, if you use the MFC framework, it is recommended that you first understand MFC; If it is pure SDK development, it is necessary to focus on the study of OpenGL to the running environment requirements. Maybe you have a higher version of VS that provides a TIMER control. This is a TIMER control that sends a WM_TIMER message to your window every once in a while. If you are not familiar with Timer, I suggest you look into the Windows messaging mechanism first; Remember, OpenGL is not for beginners; There’s no basis, and even if you write code, you don’t know why it all works the way it does; And multithreading programming is a difficult point, not only to solve the synchronization problem, debugging is also very difficult, often encounter some puzzling problems. Just like your question, this problem is caused by your lack of clarity about the OpenGL implementation requirements.
Translated from:
[b] [/ b] [b] http://blog.sina.com.cn/s/blog_4bb59dc40100fa57.html [/ b]

wglMakeCurrent:

function sets OpenGL’s current thread (thread dependent) rendering environment. All subsequent OpenGL calls from this thread are drawn on the device identified by this HDC. You can also use the wglMakeCurrent function to change the current rendering environment of the calling thread so that it is no longer in the current rendering environment.

note:

HDC parameter must be drawn on an OpenGL enabled surface. It does not need to be the HDC that wGLCreateContext passed in when it created HGLRC, but it must be the same device and have the same pixel format. GDI-converted HDCs will not be supported by the rendering environment. The current render environment will use the HDC device environment until it is no longer the current render environment.

Before switching to the new render environment, OpenGL will wash out all render environments prior to the current calling thread.

A thread can have a rendering environment. So a multithreaded process can have multiple rendering environments. A thread must set up a current rendering environment before calling any OpenGL function. Otherwise all OpenGL calls will be ignored.

The render environment can only belong to one thread at a time. You cannot make a render environment belong to multiple threads at the same time.

An application can generate multiple drawings from different current rendering environments in different threads, and it supports device environments where each thread has its own rendering environment.

If an error occurs, the wglMakeCurrent function will make the render environment not the current render environment of the thread before returning.
Translated from:
[b] [/ b] [b] http://topic.csdn.net/u/20080408/17/54b76b63-1c46-4340-80ef-2b5bc4859f0c.html [/ b]

the original is because the OpenGL Context can only be one thread has at the same time, in the creation of OpenGL window was occupied by the main thread. The solution is to use the function wglMakecurrent (HDC HDC,HGLRC HGLRC), which can associate the OpenGL Context with the thread calling the function: add wglMakecurrent (NULL,NULL) to the main thread, unassociate it, and then use wglMakecurrent (HDC,HGLRC) at the beginning of the child thread to get the Context.

I am in a story about a multithreaded OpenGL English information, you may have seen the original mainly solve with two threads at the same time operating OpenGL: a rendering, create a display list. The way he uses it is that the render thread gets the Context at render time, releases it when it’s done, and another thread takes up the Context… And so on.

I reckon I can even use wglMakeCurrent switching, implement multiple threads at the same time to drawing on a window! Not exactly at the same time, of course, but in turns. This is not very efficient, though, except to prevent the user interface thread from being blocked by too much graphing.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

= glgentextuples (1, (UINT*)& M_nTexID = m_nTexID = m_nTexID = m_nTexID = m_nTexID = m_nTexID

the original is in the child thread of hDC and hRC, and then the child thread running through return hDC and hRC, don’t know what I’m saying you understand don’t understand, don’t understand can you give me a message. Ha ha

Error in header file when calling OpenGL to open obj file in vs2013: unable to open include file: “GL / glut. H”: no such file or directories

In vs2013 cut open the obj file header files with opengl environment content error (fatal error C1083: cannot open include file: “gl/glut. H” : No to the file or directory), find the relevant information on the Internet to find the solution, now again to deepen impression, attach the original web site: http://blog.csdn.net/bigloomy/article/details/6226530
The operation is as follows:
1. Download the GLUT Toolkit. https://download.csdn.net/download/lzp1113/10303259
2, after decompression will get 5 file, containing GlU32. Lib glut32. DLL glut32. Lib glut. DLL glut. H glut. Lib
3, let the glut of decompression. H into the C:/Program Files/Microsoft Visual Studio 9.0/VC/include folder.
4. Put the Glut. lib and Glut32.lib Files in the C:/Program Files/Microsoft Visual Studio 9.0/VC/lib folder where the static function libraries are located.
5. Put the extracted Glut. DLL and Glut32.dll in the system32 folder under the operating system directory.
6. Run it again, and if it still fails, change the name of the header file to #include <; gl/glut.h> For the # include & lt; glut.h> . After the fifth step is finished, the error is probably due to the wrong address of the header file include. Generally, it can be finished by following the sixth step.

Netinet / in. H Linux / in. H problem

* IPv6hdr struct * IPv6hdr struct * IPv6hdr struct * IPv6hdr struct * IPv6hdr struct * IPv6hdr struct * IPv6hdr struct * IPv6hdr struct * IPv6hdr struct
It is assumed that it directly includes <; linux/ipv6.h> Result compilation error, duplicate definition The problem is that the project uses the libnetfilter_conntrack.h connection trace header, This file contains <<; netinet/in.h> , while IPv6.h contains <; linux/in.h> Both files basically define the same thing Linux is for the kernel and Netinet is for user space, so in user space I call <; netinet/ipv6.h> This file contains Netinet. h so there is no problem.

Git push you are not allowed to upload merges

If you download a project from the remote branch master, you will modify the project and commit it. If someone else commits a project from the remote branch master, the change can be different. If you pull the same project, you will have a problem. You are not allowed to upload merges when you pull the code and merge it by default. You are not allowed to upload merges when you push the code. Gerrit does not allow local merge submissions by default.

Solution:
Object: you
Git rebase master git rebase master
If there is a conflict, resolve the conflict manually (enter the conflict file to resolve it) and then
Git rebase – continue
Then, in a push

Vs2015 + OpenGL configuration method

VS2015+OpenGL configuration method
Refer to VS2015 installation +OpenGL environment configuration and test
A few points to note:

    There is no need to install the full functionality of the VS. The platform must be changed to x64before performing step (4) of the OpenGL environment configuration. The FreeGlut.dll file should be placed on the same level as the x64 folder and source.cpp file

Vs compiling OpenGL project, the solution of unable to open the source file “GL / glaux. H” appears

The problem is as follows:

The reason:
Lack of header files and libraries to compile OpenGL;
Solutions:
1. Download OpenGL header files and libraries;
Download address: https://download.csdn.net/download/ssagnn23/2560229
2. Put the file in the specified directory;
(1) Copy GLUX32.DLL, GLUT32.DLL and GLUX.DLL to C:/WINDOWS/system32. The system32 directory should already have OpenGL32.DLL and GLU32.DLL.
(2) Copy Glut. H, Glut. H, Glut. H, Glut. H to
C:/Program Files/Microsoft Visual Studio X.0/VC/include/ GL
Or you might
C:\Program Files (x86)\Microsoft SDKs\Windows\vX.XA\Include\gl
Or you might
C:\Program Files (x86)\Windows Kits\X.X\Include\um\gl

(3) Copy glaux-lib, glu32. LIB, glut32. LIB, opengl32. LIB, Glut.lib, Glut.lib, Glut.lib, Glut.lib, Glut.lib, Glut.lib, Glut.lib
[32Bit]C:/Program Files/Microsoft Visual Studio X.0/VC/lib [32Bit]C:/Program Files/Microsoft Visual Studio X.0/VC/lib
Or you might
C:\Program Files (x86)\Microsoft SDKs\Windows\vX.XA\Lib
Or you might
C:\Program Files (x86)\Windows Kits\X.X\Lib\XXX
 
 
 
Other questions 1:

 
Solution:
1. Open the Property Pages dialog box for the project.
2. Click the Linker folder.
3. Click the Command Line property page.
4. Copy /SAFESEH:NO into the “Additional Options” box and click Apply

Other Question # 2:
1 & gt; Glaux. Lib (tk.obj) : error LNK2019: Cannot resolve external symbol _sscanf, which is referenced in the function _GetRegistrySysColors@8
1> Glaux. Lib (tk.obj) : error LNK2019: Unresolvable external symbol _vsprintf, which is referenced in the function _PrintMessage
VS2015 compilation will cause this problem, the solution is in the project properties ->; The linker – & gt; Input – & gt; Add a dependency legacy_stdio_defines.lib to the attached dependency;

Reproduced in: https://www.cnblogs.com/21tcy/p/9771340.html

MPI cannot be found in C / C + +


Before I do that I import the MPI location as

I thought it was weird that I had already imported it manually and then I realized that the inclusion directory should be stored in the include directory and found by the library directory
 
I fixed it and it worked out perfectly.

Configuration of OpenGL development environment under Windows environment, win10 + vs2019 + glfw + glad

GLFW library configuration 1. The first step, look up a lot of tutorials are selected directly on the web site to download GLFW source code, and then use CMake program compiled, but I don’t know why I always make mistakes during compilation, so finally give up this way, choose directly on the website to download the 64 – bit Windows precompiled binary 】, the diagram below

After you download it, copy the included file and the corresponding version of the library (because mine is vs2019) lib-vc2019 and place it in a fixed location (create a new folder to put both of them in it), such as my location under the path C: Program Files\OpenGL
2. The second step is the configuration of GLAD. Go to the GLAD online service page and select the corresponding options after entering it, as shown in the picture

You will get a Glad. Zip file. Download it and unzip it. Copy the two Files Glad and Khr in the include folder and place them in the C:\Program Files\OpenGL\ Include folder in the path of our first step

Copy the SRC folder to the path C: Program Files\OpenGL, as shown in the figure

3. Next, open VS2019 and create a new blank item. Click “Other Window” in “View” in the menu bar to find “Property Manager”, as shown in the figure below

Double click the Debug | x64 “folder” Microsoft. Cpp. X64. User], click on the vc + + directories add directory contains 】 【 in 】 directory, add, the following figure, OpenCV everyone here don’t tube, just add the OpenGL.

Configuration engineering library catalog, is also a double click the Debug | x64 】 in the folder. Microsoft. Cpp x64. User 】 【, click on the vc + + library directory 】 【 in 】 to add, as shown, also don’t tube of OpenCV.

Link library configuration, is also a double click the Debug | x64 】 in the folder. Microsoft. Cpp x64. User 】 【, click on the “input” of the “connector”, is added in the “additional dependencies” library Files and library Files in the C: \ Program Files \ OpenGL \ lib – vc2019, copy and paste the name to the “additional dependencies”, as shown in figure

At this point, our OpenGL environment is configured, but each time we create a new project, we need to add the glad.c file under the path C: Program Files\OpenGL\ SRC to the source of our project, and that’s it.
4. Test
The code is as follows:

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int main() {
	glfwInit();
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
	//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
	if (window == NULL) {
		cout << "Failed to create GLFW window" << endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	glViewport(0, 0, 800, 600);

	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	while (!glfwWindowShouldClose(window)) {
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

The results show that the configuration was successful.

 

The solution of console flash in C + +

When you write a C ++ program in VS, you will find that when you run the program, the console will flash by and you will not see the results of the program. Therefore, you need to find a way to make the screen or console pause.
 

    method one: increase the input statement at the end of program, this will require the user to input before the end of the program runs, the console will stay there, specific code as shown in the figure below
    alternative Rich two alternatives: in C: printf (” press any key to continue…” );
    getchar(); 12 In C++ : Cout<; <" Press any key to continue..." ;
    cin.clear();
    cin.sync();
    cin.get(); 1234 plus cin. The clear (), cin. The sync () these two words, is to empty the cache area, let cin. The get () to receive your real keyboard input method
    2: add the statement at the end of the program: system (” pause “); Things to Avoid in C/C++ — system(“pause”) is not recommended because:
    is not portable: only for DOS or Windows, not Linux, etc.
    consumes system resources: Calling the system command system() to “suspend the program” is overkill.
    must add a header file: stdlib.h or cstdlib. There are many header files in C ++ that already contain this file, so it is sometimes possible to use this function without the header stdlib.h, but it is better to add it for safety reasons. Method 3: To modify the project configuration, right-click on the project, select Properties from the right-click menu, and then select “Configuration Properties” from the list on the left of the pop-up dialog box –>; ‘Linker’ –>; “System”, then in the list on the right, in the first “subsystem” value, select “Console” (/SUBSUSTEM:CONSOLE) “as shown in the figure below

Solution of flash screen caused by OpenGL in MFC environment

Since OpenGL has its own background refresh function, glClear, Windows should not be allowed to refresh a window background. Otherwise, when the window needs to be redrawn, Windows automatically sends WM_ERASEBKGND first, and the default handler uses a white background brush. When the background color used by OpenGL is not white, there is a flicker of white in the drawing. This is especially true when doing animation.

Activate the WM_ERASEBKGND message handler in the MFC program to disable message processing of the parent window class, and simply return a TRUE. Return CView::OnEraseBkgnd(PDC). Now change this to return TRUE. This will solve the flash screen problem.

[solution] visual studio will exit automatically after debugging the program

1. Set breakpoint
Second, the system (” pause “)
(1) in the return 0; stem ("pause"); .
② Not using turn 0; stem ("pause"); ;
note: in the ACM or leetcode upload code to join this sentence complains;
3. Select the console program when creating a new project
If an empty project is selected, there will be an automatic exit problem.
Change the project configuration properties
opens the project properties and changes the subsystem to the console. (essentially the same as solution 3)