GLUT Tutorial : Initialization

本文介绍如何使用GLUT库初始化应用程序窗口,包括设置显示模式、窗口位置和大小等,并演示了绘制基本图形的方法。
In this section we're going to build the main function of our application. The main function will perform the required initializations and start the event processing loop. All the functions in GLUT have the prefix glut, and those which perform some kind of initialization have the prefix glutInit. The first thing you must do is call the function glutInit.


void glutInit(int *argc, char **argv);

Parameters:
argc - A pointer to the unmodified argc variable from the main function.
argv - A pointer to the unmodified argv variable from the main function.


After initializing GLUT itself, we're going to define our window. First we establish the window's position, i.e. its top left corner. In order to do this we use the function glutInitWindowPosition.


void glutInitWindowPosition(int x, int y);

Parameters:
x - the number of pixels from the left of the screen. -1 is the default value, meaning it is up to the window manager to decide where the window will appear. If not using the default values then you should pick a positive value, preferably one that will fit in your screen.
y - the number of pixels from the top of the screen. The comments mentioned for the x parameter also apply in here.


Note that these parameters are only a suggestion to the window manager. The window returned may be in a different position, although if you choose them wisely you'll usually get what you want. Next we'll choose the window size. In order to do this we use the function glutInitWindowSize.


void glutInitWindowSize(int width, int height);

Parameters:
width - The width of the window
height - the height of the window


Again the values for width and height are only a suggestion, so avoid choosing negative values.

 

Then you should define the display mode using the function glutInitDisplayMode.


void glutInitDisplayMode(unsigned int mode)

Parameters:
mode - specifies the display mode


The mode parameter is a Boolean combination (OR bit wise) of the possible predefined values in the GLUT library. You use mode to specify the color mode, and the number and type of buffers.

 

The predefined constants to specify the color model are:
  • GLUT_RGBA or GLUT_RGB - selects a RGBA window. This is the default color mode.
  • GLUT_INDEX - selects a color index mode.
The display mode also allows you to select either a single or double buffer window. The predefined constants for this are:
  • GLUT_SINGLE - single buffer window
  • GLUT_DOUBLE - double buffer window, required to have smooth animation.
There is more, you can specify if you want a window with a particular set of buffers. The most common are:
  • GLUT_ACCUM - The accumulation buffer
  • GLUT_STENCIL - The stencil buffer
  • GLUT_DEPTH - The depth buffer
So, suppose you want a RGB window, with single buffering, and a depth buffer. All you have to do is to OR all the respective constants in order to create the required display mode.


    
...
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT DEPTH);
...



After all the above steps, the window can be created with glutCreateWindow.


int glutCreateWindow(char *title);

Parameters:
title - sets the window title


The return value of glutCreateWindow is the window identifier. You can use this identifier later within GLUT but this is outside of the scope of this section.

 

So now here is a little bit of code to perform all the initializations:


    
#include <GL/glut.h> 

void main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("3D Tech- GLUT Tutorial");
}



Note the include statement at the beginning of the code. The required include file comes with the GLUT distribution.

 

If you run this code, you'll obtain an empty black console window but no OpenGL window. Furthermore after a few seconds the window disappears. There are two things left to do before we are ready to render something. The first is to tell GLUT what is the function responsible for the rendering.

 

Lets create an example function for the rendering. The function presented bellow will clear the color buffer and draw a triangle.


    
...
void renderScene(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
		glVertex3f(-0.5,-0.5,0.0);
		glVertex3f(0.5,0.0,0.0);
		glVertex3f(0.0,0.5,0.0);
	glEnd();
	glFlush();
}



The name of this function is up to you. However, now you must tell GLUT that it should use that function we just wrote for the rendering. This is called registering a callback. GLUT will call the function you supply whenever rendering is in order. For now lets tell GLUT that the function renderScene should be used whenever the window is reported by the window system to be damaged. GLUT has a function that takes as a parameter the name of the function to use when redrawing is needed. Note: the function you supply as a parameter is also called the first time the window is created. The syntax is as follows:


void glutDisplayFunc(void (*func)(void));

Parameters:
func - the name of the function to be called when the window needs to be redrawn. Note: it is illegal to pass NULL as the argument to this function.


One last thing missing, that is telling GLUT that we're ready to get in the application event processing loop. GLUT provides a function that gets the application in a never ending loop, always waiting for the next event to process. The GLUT function is glutMainLoop, and the syntax is as follows:


void glutMainLoop(void)

The code so far is presented bellow. Note that we've added an include statement in order to start using standard OpenGL functions, like glClear, glBegin, glVertex3f, and glEnd.

 

If you try to run this code you'll get a console window, and a few instants after the OpenGL window with a white triangle, hopefully at the desired position and with the desired size.


    
#include <GL/glut.h>


void renderScene(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
		glVertex3f(-0.5,-0.5,0.0);
		glVertex3f(0.5,0.0,0.0);
		glVertex3f(0.0,0.5,0.0);
	glEnd();
	glFlush();
}

void main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("3D Tech- GLUT Tutorial");
	glutDisplayFunc(renderScene);
	glutMainLoop();
}
数据集介绍:电力线目标检测数据集 一、基础信息 数据集名称:电力线目标检测数据集 图片数量: 训练集:2898张图片 验证集:263张图片 测试集:138张图片 总计:3299张图片 分类类别: 类别ID: 0(电力线) 标注格式: YOLO格式,包含对象标注信息,适用于目标检测任务。 数据格式:JPEG/PNG图片,来源于空中拍摄或监控视觉。 二、适用场景 电力设施监控与巡检: 数据集支持目标检测任务,帮助构建能够自动识别和定位电力线的AI模型,用于无人机或固定摄像头巡检,提升电力设施维护效率和安全性。 能源与公用事业管理: 集成至能源管理系统中,提供实时电力线检测功能,辅助进行风险 assessment 和预防性维护,优化能源分配。 计算机视觉算法研究: 支持目标检测技术在特定领域的应用研究,促进AI在能源和公用事业行业的创新与发展。 专业培训与教育: 数据集可用于电力行业培训课程,作为工程师和技术人员学习电力线检测与识别的重要资源。 三、数据集优势 标注精准可靠: 每张图片均经过专业标注,确保电力线对象的定位准确,适用于高精度模型训练。 数据多样性丰富: 包含多种环境下的电力线图片,如空中视角,覆盖不同场景条件,提升模型的泛化能力和鲁棒性。 任务适配性强: 标注格式兼容YOLO等主流深度学习框架,便于快速集成和模型开发,支持目标检测任务的直接应用。 实用价值突出: 专注于电力线检测,为智能电网、自动化巡检和能源设施监控提供关键数据支撑,具有较高的行业应用价值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值