Several errors and solutions in openGL-computer graphics work

error one

Error phenomenon: Press the corresponding buttons i and o to move the camera on the x-axis and y-axis, but press the corresponding button p to move the camera on the z-axis.

Error reason: In order to move the camera, three global variables x, y, z are set for the parameters of gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0). But void myKeyboard( unsigned char key, int x, int y ) uses the two variable names x and y when passing parameters, so when the function calls

case 'i': case 'I':
x += 1; break;

In such a statement related to parameter x and related to parameter y, the values ​​of x and y have not changed.

Solution: In order to move the camera, three global variables x, y, and z are set and renamed as xx, yy, and zz, and the places where these three variables are used are also modified accordingly.

How to find the cause of the error: x, y, z are three parameters that are set and used together. Two of them cannot be used normally, but the third one can, so there should be differences. First, I looked at the code using these three parameters, the format was the same, and no difference was found. Then expand the scope of the inspection code to view the entire function using these three parameters. Start with the function of the keyboard event written, and then see the variable name of the parameter passed by the myKeyboard function, and suddenly realize.

 

error two

Error phenomenon: There is an error in the color of the image on the texture map, for example, what should be red has turned into blue, and the tone of the entire image has changed.

Cause of the error: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels); the GL_BGR_EXT parameter in the function reports an error, initially changed to GL_RGB, and passed the compilation. However, the bmp storage is not in the order of RGB but in the order of BGR. It should be the GL_BGR_EXT parameter here. The GL_BGR_EXT parameter here reports an error because there is no header file glext.h, and this parameter cannot be processed.

Solution: Change GL_RGB back to GL_BGR_EXT and add the header file #include <GL/glext.h>.

How to find the cause of the error: I couldn't start at first, but later I thought that other people's stones can attack jade. You might as well look at other people's texture map code, and found that someone else used the glext.h header file and GL_BGR_EXT parameters. After trying to modify, the picture Return to normal color. Later, I checked the relevant information, only to know that the bmp storage is not in the order of RGB but in the order of BGR. The glext.h header file has a function to change the order of BGR to the order of RGB.

 

error three

Error phenomenon: When the skybox texture is pasted with the bottom and the back, it is found that there is a common edge, but the two pictures are not connected together. The background color is black at the intersection of the bottom and the back.

Cause of the error: The viewing angle is 30 degrees and the zFar parameter setting of gluPerspective() is not large enough, so that part of the edges and corners are not displayed in the view.

Solution: Modify the position of the image and the size of zFar to appropriate values.

How to find the cause of the error: At first, I thought it was the wrong coordinate setting in the function glVertex3f of my texture, but I checked it again and again, and I couldn't figure it out. At this time, there are hundreds of lines of code. I thought that a certain code was wrongly written. I didn’t see it after reading it. I felt that there was too much code to find the wrong code location. So I rebuilt a project and only wrote the code related to the texture. When gluPerspective() function, I suddenly thought of the principle of the perspective view as shown below

The trapezoidal column in the figure is the size of the view, and the area beyond the trapezoidal column is not visible. And the cube that I once wrote will be cut off when it changes its position. I thought that the zFar parameter of gluPerspective() and the position coordinates of the texture are not reasonable enough to make the intersection between the bottom surface and the back. The edge cannot be displayed, so I modified its parameters size, and found that the texture display is normal.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324377105&siteId=291194637