当前位置:网站首页>Coordinate transformation learning of OpenGL learning notes
Coordinate transformation learning of OpenGL learning notes
2022-06-21 11:14:00 【clever101】
author : Zhu Jincan
source :clever101 The column
GDI、Qt and OpenGL The drawing difference between the three
Begin to learn OpenGL Step over some potholes . Because I've learned GDI and Qt mapping .GDI and Qt Drawings are more intuitive . As shown in the following figure GDI Draw a line from (0,0) To (100,100) The straight line of :
The drawing code is as follows :
HPEN g_hPen = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
g_hPen = CreatePen(PS_SOLID,2,RGB(255,0,0));
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add here to use hdc Any drawing code for ...
SelectPen(hdc, g_hPen);
// from (0,0) To (100,100) Draw a straight line
::MoveToEx(hdc, 0, 0, NULL);
::LineTo(hdc, 100, 100);
// With (100, 100) Draw a radius for the center of the circle 4 The circle of
::Ellipse(hdc,96,96,104,104);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
{
DeletePen(g_hPen);
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
You can see GDI Coordinates in drawing (0,0) It's in the upper left corner of the window . Let's try again Qt mapping ,Qt The rendering of the drawing is as follows :
The drawing code is as follows :
void QtDraw::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QPen(Qt::red, 4));// Set brush form
painter.drawLine(0, 0, 100, 100);// from (0,0) To (100,100) Draw a straight line
painter.drawEllipse(100, 100, 4, 4);// With (100,100) For the center of a circle , The radius is 4 A circle
}
You can see Qt Coordinates in drawing (0,0) It's in the upper left corner of the window . Let's see OpenGL The graph function of . On the surface ,OpenGL The code for drawing straight lines is also very simple , as follows :
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(100, 100);
glEnd();
But the above code can not draw from the window (0,0) To (100,100) The straight line of . That is to say glVertex2f The parameter of the function does not accept the coordinates of the window coordinate system . This is with GDI and Qt There is a fundamental difference .
OpenGL Coordinate conversion process
We know from the last section that glVertex2f The parameter of the function does not accept the coordinates of the window coordinate system . So here comes the question ,glVertex2f In which coordinate system is the parameter value of the function ? After studying, we found that ,glVertex2f The parameter value of the function is in the coordinates of the object . Here comes the question , How is the object coordinate converted to the window coordinate system ? After reading 《OpenGL Programming Guide 》, The discovery process is as follows :
Here comes the question , If we want to use OpenGL Draw a line from... On the window (0,0) To (100,100) The straight line of , What should be done ? According to the above process , That is to say, we should (0,0) and (100,100) Perform an inverse transformation , Transform these two points from window coordinates to object coordinates . The code is as follows :
void DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth cache
glColor3f(1.0f, 0.0f, 0.0f);
GLint viewport[4];
// Get the range of the current viewport
glGetIntegerv(GL_VIEWPORT, viewport);
GLdouble modelview[16];
GLdouble projection[16];
GLdouble winStartX = 0.0;
GLdouble winStartY = 0.0;
GLdouble winEndX = 100.0;
GLdouble winEndY = 100.0;
GLdouble objStartX, objStartY, objStartZ;
GLdouble objEndX, objEndY, objEndZ;
// Get the view and model transformation matrix
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
// Get the projection matrix
glGetDoublev(GL_PROJECTION_MATRIX, projection);
// Convert window coordinates to object coordinates
gluUnProject(winStartX, winStartY, 0.0, modelview, projection, viewport, &objStartX, &objStartY, &objStartZ);
gluUnProject(winEndX, winEndY, 0.0, modelview, projection, viewport, &objEndX,&objEndY,&objEndZ);
// Perform drawing
glBegin(GL_LINES);
glVertex2f(objStartX, objStartY);
glVertex2f(objEndX, objEndY);
glEnd();
}
The renderings are as follows :
OpenGL Window coordinates in drawing (0,0) It's in the lower left corner of the window , We can see that our code drawing is correct .
边栏推荐
- is not allowed to connect to this mysql server
- The third part of the procedure
- Messageformat usage
- C# Cannot access child value on Newtonsoft.Json.Linq.JProperty
- 05. Redis core chapter: the secret that can only be broken quickly
- 详解连接池参数设置(边调边看)
- C语言初阶(六)函数
- MySQL 5.7 is about to be stopped and only maintained. It's time to learn a wave of MySQL 8
- 04. New features of redis: Interpretation of multithreading model
- 02. Redis Blockbuster: viewing core principles from high-frequency problems
猜你喜欢
随机推荐
Quickly analyze oom using mat tools
功能测试怎么学?阿里工程师教4个步骤
The third part of the procedure
Fastapi web framework [pydantic]
The most powerful eight part essay in 2022, "code out eight part essay - cut out the offer line"
Research and implementation of embedded software framework based on multi process architecture
轻量级开源SAST工具semgrep分析1/2
The advanced process resistance of Intel and TSMC is increasing, and Chinese chips are expected to shorten the gap
MySQL - data type
Scholar magic changes QT creator plug-in framework (with examples)
WPF DataContext 使用
第九章Cisco ASA应用NAT
Middle order traversal of leetcode-94-binary tree
领导:谁再用redis过期监听实现关闭订单,立马滚蛋!
Deep water area involvement
【100个 Unity踩坑小知识点】| Unity 使用Quaternion.AngleAxis随机一个方向
Network multimedia -- linphone correlation analysis -- directory
3000帧动画图解MySQL为什么需要binlog、redo log和undo log
中芯国际据理力争赢了美国诉讼,证明独立自主才能有更长远的发展
我国服装纺织制造业面临着异常严峻的挑战










