xiug
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
#include<GL/glut.h>
|
||||
using namespace std;
|
||||
|
||||
// 回调函数
|
||||
void myDisplay()
|
||||
{
|
||||
// 清除缓冲区
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// 正交模式
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
|
||||
glColor4f(0.0, 1.0, 0.0, 0.0);
|
||||
glRectf(50.0, 50.0, 400.0, 400.0);
|
||||
|
||||
// 划线
|
||||
glColor3f(1.0, 1.0, 0.0);
|
||||
glBegin(GL_LINES);
|
||||
glVertex2f(50.0, 50.0);
|
||||
@@ -18,12 +22,14 @@ void myDisplay()
|
||||
glVertex2f(50.0, 400.0);
|
||||
glEnd();
|
||||
|
||||
// 画点
|
||||
glColor3f(1.0, 0.0, 0.0);
|
||||
glPointSize(20.0);
|
||||
glBegin(GL_POINTS);
|
||||
glVertex2f(15.0, 15.0);
|
||||
glEnd();
|
||||
|
||||
// 画三角形
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor3f(0.0, 0.0, 1.0);
|
||||
glVertex2i(200, 300);
|
||||
@@ -41,7 +47,7 @@ int main(int argc, char* argv[])
|
||||
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
|
||||
glutInitWindowPosition(100, 100);
|
||||
glutInitWindowSize(500, 500);
|
||||
glutCreateWindow("ÀÖÒâ");
|
||||
glutCreateWindow("The First OpenGL Program");
|
||||
glutDisplayFunc(&myDisplay);
|
||||
glutMainLoop();
|
||||
return 0;
|
||||
|
||||
@@ -15,7 +15,7 @@ void drawDDALine(int x1, int y1, int x2, int y2) {
|
||||
int dy = y2 - y1;
|
||||
|
||||
// 确定步数,取 dx 和 dy 中绝对值较大的那个
|
||||
int steps = std::abs(dx) > std::abs(dy) ? std::abs(dx) : std::abs(dy);
|
||||
int steps = std::abs(dx) > std::abs(dy) ? std::abs(dx) : std::abs(dy); //三元表达式
|
||||
|
||||
// 计算每一步的增量
|
||||
float xIncrement = (float)dx / steps;
|
||||
@@ -28,7 +28,7 @@ void drawDDALine(int x1, int y1, int x2, int y2) {
|
||||
for (int k = 0; k < steps; k++) {
|
||||
x += xIncrement;
|
||||
y += yIncrement;
|
||||
// 将浮点坐标四舍五入转换为整数像素坐标
|
||||
// 将浮点坐标四舍五入取整转换为整数像素坐标
|
||||
std::cout << (int)round(x) << ", " << (int)round(y)<<"\n";
|
||||
glVertex2i((int)round(x), (int)round(y));
|
||||
}
|
||||
|
||||
BIN
lab1/2.exe
BIN
lab1/2.exe
Binary file not shown.
Reference in New Issue
Block a user