Compare commits

...

1 Commits

3 changed files with 9 additions and 3 deletions

View File

@@ -1,15 +1,19 @@
#include<GL/glut.h> #include<GL/glut.h>
using namespace std; using namespace std;
// 回调函数
void myDisplay() void myDisplay()
{ {
// 清除缓冲区
glClearColor(0.0, 0.0, 0.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// 正交模式
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500.0, 0.0, 500.0); gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glColor4f(0.0, 1.0, 0.0, 0.0); glColor4f(0.0, 1.0, 0.0, 0.0);
glRectf(50.0, 50.0, 400.0, 400.0); glRectf(50.0, 50.0, 400.0, 400.0);
// 划线
glColor3f(1.0, 1.0, 0.0); glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2f(50.0, 50.0); glVertex2f(50.0, 50.0);
@@ -18,12 +22,14 @@ void myDisplay()
glVertex2f(50.0, 400.0); glVertex2f(50.0, 400.0);
glEnd(); glEnd();
// 画点
glColor3f(1.0, 0.0, 0.0); glColor3f(1.0, 0.0, 0.0);
glPointSize(20.0); glPointSize(20.0);
glBegin(GL_POINTS); glBegin(GL_POINTS);
glVertex2f(15.0, 15.0); glVertex2f(15.0, 15.0);
glEnd(); glEnd();
// 画三角形
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0); glColor3f(0.0, 0.0, 1.0);
glVertex2i(200, 300); glVertex2i(200, 300);
@@ -41,7 +47,7 @@ int main(int argc, char* argv[])
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100); glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500); glutInitWindowSize(500, 500);
glutCreateWindow("ÀÖÒâ"); glutCreateWindow("The First OpenGL Program");
glutDisplayFunc(&myDisplay); glutDisplayFunc(&myDisplay);
glutMainLoop(); glutMainLoop();
return 0; return 0;

View File

@@ -15,7 +15,7 @@ void drawDDALine(int x1, int y1, int x2, int y2) {
int dy = y2 - y1; int dy = y2 - y1;
// 确定步数,取 dx 和 dy 中绝对值较大的那个 // 确定步数,取 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; 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++) { for (int k = 0; k < steps; k++) {
x += xIncrement; x += xIncrement;
y += yIncrement; y += yIncrement;
// 将浮点坐标四舍五入转换为整数像素坐标 // 将浮点坐标四舍五入取整转换为整数像素坐标
std::cout << (int)round(x) << ", " << (int)round(y)<<"\n"; std::cout << (int)round(x) << ", " << (int)round(y)<<"\n";
glVertex2i((int)round(x), (int)round(y)); glVertex2i((int)round(x), (int)round(y));
} }

Binary file not shown.