Experiment 1 Introduction to OpenGL Graphics Programming (visualstudio2010)

insert image description here

1. Purpose of the experiment

1. Understand and master the installation of OpenGL.
2. Master a simple OpenGL-based C++ program structure.
3. Master the win32 program framework.
4. Master the drawing of some basic graphics in OpenGL.

2. Experimental environment

Hardware requirements:
PC, mainstream configuration, preferably independent graphics card, memory 512M or more.
Software environment:
Operating system: Windows 7/Windows8
Language development tools: Microsoft Visual studio 2010, Visual C++.
Program framework:
win32 application

3. Experimental content and requirements

Requirements: Copy the effect screenshots generated by all steps of the experiment to the experiment report document for future reference, and attach the corresponding code. WORD document naming method: student number name-experiment serial number-experiment name.
Content: (Note: for printing, set the background black to transparent color)
1. Prepare the OpenGL library file. As shown in the figure below:
insert image description here
(1) Glut32.dll path %system root%\ SysWOW64. As shown below:
insert image description here

(2)Glut32.lib in PATH\lib。
insert image description here

(3) Glut.h in PATH\Include. As shown below:
insert image description here

system root is the installation path of windows7 or windows8.
path is the installation path of Visual studio 2010.
2. Create a project file and run the sample program my_first_program.cpp to watch the result.
1) Start Microsoft Visual Studio 2010, click "File" -> "New" -> "Project" in the menu bar, as shown in the figure below: 2) In the "New Project" dialog box, select the Win32 project of
insert image description here
Visual C++, and then enter the project name ( For example, lab1-basis), select the save location of the project (), as shown in the figure below, and click the "OK" button.
insert image description here
3) Replace the C++ source file (lab1-basis.cpp) in the "source file" of the project file with the following sample program: the result of
insert image description here
running the above sample program is to create a window named "hello", as shown in the figure below:
insert image description here
3. Carefully read the above sample program, understand the function of each function, and modify the window title to display "my first OpenGL program". The modified code and its running results are as follows: (1) Modify the code
:

char *argv[]={
    
    "我的第一个OpenGL程序"," "};  
glutCreateWindow("我的第一个OpenGL程序"); 

(2) Running results:
insert image description here
4. Window settings
By default, the window position appears in the upper left corner of the screen, with a size of 300*300.
Requirements:
1) Modify the window position so that it is in the center of the screen
Modify the code as follows:

int cx = glutGet(GLUT_SCREEN_WIDTH);  
int cy = glutGet(GLUT_SCREEN_HEIGHT);  
glutInitWindowPosition((cx-500)/2,(cy-500)/2); 

The running results are as follows:
insert image description here
2) Change the window size to the entire screen size
The following figure shows the code modification and running results:
insert image description here
3) Modify the window size to other sizes
Modify the code:

glutInitWindowSize(350,350);

Running results:
insert image description here
5. Background color setting
By default, the background color is black
1) Set the window background to white, as shown in the figure below:
Code modification:

glClearColor(1.0f,1.0f,1.0f,1.0f);

Running results:
insert image description here
1) Set the window background to another color, as shown in the figure below:
Code modification:

glClearColor(7.0,-7.0,5.0,1.0f);

Running result: (purple background)
insert image description here
6. Basic graphic drawing
Rectangle drawing:
1) Add "glRectf(0,0,1,1);" after the statement "glClear(GL_COLOR_BUFFER_BIT);" of the display drawing function, and run the program to view the effect , the code modification and running results are as shown in the figure below: 2) Modify the diagonal
insert image description here
coordinates of the rectangle to see what changes and problems there are.
(Bottom left to top right) The horizontal and vertical coordinates of the two points (and the center point of the window is the origin, which is equivalent to establishing a two-dimensional coordinate system with it as the origin) Modify the code
:

glRectf(-0.8f,-0.8f,0.5f,0.5f);

Running results:
insert image description here
3) According to the given function, try to draw basic graphics such as straight line and triangle
(1) Straight line:
code modification:

void display(void)
{
    
    

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    //设置清屏颜色 
    glClear(GL_COLOR_BUFFER_BIT);           //刷新颜色缓存区
    glBegin(GL_LINES);  
    glVertex2f(0,0);  
    glVertex2f(0.8,0.8);  
    glEnd();  
    glFlush();                                //用于刷新命令队列和缓存区,使所有尚未被执行的OpenGL命令得到执行

}

Running result:
insert image description here
(3) Triangle:
code modification:

void display(void)
{
    
    

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    //设置清屏颜色 
    glClear(GL_COLOR_BUFFER_BIT);           //刷新颜色缓存区
    glBegin(GL_TRIANGLES);  
    glVertex2f(0,0);  
    glVertex2f(0.5,0.5);  
    glVertex2f(0.0,0.8);  
    glEnd();  
    glFlush();                           //用于刷新命令队列和缓存区,使所有尚未被执行的OpenGL命令得到执行
}

Running results:
insert image description here
7. Setting of drawing color
1) Change the drawn graphics to red
Code modification:

glColor3f(1.0f, 0.0f, 0.0f);                 //设置为红色
glRectf(-0.1f, -0.1f, -0.2f, -0.2f);        //矩形

insert image description here
2) Set the drawn basic primitives to different colors
Code modification:

glColor3f(0.5f, 0.6f, 0.2f);                 //设置为草绿色
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f,0.0f);
    glVertex2f(0.2f, 0.2f);
    glVertex2f(0.0f, 0.2f);
    glEnd();

operation result:

insert image description here

8. Draw geometric figures
1) Change the title bar, add student number and name.
2) Draw more than two basic geometric shapes.
3) Set three colors.
Code modification:

#include <windows.h>
#include <gl/glut.h>
void display(void)
{
    
    

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    //设置清屏颜色 
    glClear(GL_COLOR_BUFFER_BIT);           //刷新颜色缓存区
    glColor3f(1.0f, 0.0f, 0.0f);                 //设置为红色
    glRectf(-0.1f, -0.1f, -0.2f, -0.2f);        //矩形
    //绘制直线
    glColor3f(0.0f, 1.0f, 0.0f);                 //设置为绿色
    glBegin(GL_LINES);
    glVertex2f(0.0f,0.0f);
    glVertex2f(0.3f, -0.2f);
    glEnd();
    //画三角形,x1、y1、x2、y2、x3、y3为三角形顶点坐标
    glColor3f(0.5f, 0.6f, 0.2f);                 //设置为草绿色
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f,0.0f);
    glVertex2f(0.2f, 0.2f);
    glVertex2f(0.0f, 0.2f);
    glEnd();
    glFlush();                              //用于刷新命令队列和缓存区,使所有尚未被执行的OpenGL命令得到执行
}
int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR  lpCmdLine,int   nCmdShow)
{
    
    
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
	char *argv[] = {
    
     "hello"," " };
	int argc = 2;
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(350,350);
	glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-640)/2,
                       (glutGet(GLUT_SCREEN_HEIGHT)-480)/2);
	glutCreateWindow("学号 姓名");
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}

operation result:
insert image description here

4. Thinking questions

1. Where is the default window on the screen? What should I do if I want to change the position of the window on the screen? How to resize the window?

Answer: The default window is in the upper left corner of the screen. To change the position of the window, use the glutInitWindowPosition function, passing in the coordinates of the upper left corner of the target window position; to adjust the window size, use the glutInitWindowSize function, passing in the length and width of the window.

2. Where is the default drawing coordinate origin in the window in this experiment?

A: In the upper right corner of the window.

3. How to modify the background color and drawing color? What are the requirements for the order and position of the drawing colors?

Answer: Use the glClearColor function to modify the background color. This function is placed in display() and placed in front of the glClear() statement. Drawing color modification uses glColor3f function before glBegin().

4. For "#include <gl/glut.h>", which folder should the header file be placed in?

This answer: It should be placed in D:\vs2010\VC\include\gl\ (write according to your own storage location)

Guess you like

Origin blog.csdn.net/weixin_52030647/article/details/130674002