Compare commits
4 Commits
47a36fca7f
...
master
@@ -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;
|
||||
|
||||
80
lab1/2.cpp
Normal file
80
lab1/2.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <GL/glut.h>
|
||||
|
||||
// 窗口宽度和高度
|
||||
const int WIDTH = 640;
|
||||
const int HEIGHT = 480;
|
||||
|
||||
void drawDDALine(int x1, int y1, int x2, int y2) {
|
||||
float x = x1;
|
||||
float y = y1;
|
||||
|
||||
// 计算差值
|
||||
int dx = x2 - x1;
|
||||
int dy = y2 - y1;
|
||||
|
||||
// 确定步数,取 dx 和 dy 中绝对值较大的那个
|
||||
int steps = std::abs(dx) > std::abs(dy) ? std::abs(dx) : std::abs(dy); //三元表达式
|
||||
|
||||
// 计算每一步的增量
|
||||
float xIncrement = (float)dx / steps;
|
||||
float yIncrement = (float)dy / steps;
|
||||
|
||||
// 开始绘制点
|
||||
glBegin(GL_POINTS);
|
||||
glVertex2i((int)round(x), (int)round(y)); // 绘制起点
|
||||
|
||||
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));
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
||||
// 显示回调函数
|
||||
void display() {
|
||||
drawDDALine(0, 0, 50, 20);
|
||||
glFlush();
|
||||
}
|
||||
|
||||
// 初始化 OpenGL 设置
|
||||
void init() {
|
||||
// 设置背景颜色为白色
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
|
||||
// 设置投影矩阵为 2D 正交投影
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
// 定义可视区域,左下角(0,0),右上角(WIDTH, HEIGHT)
|
||||
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// 初始化 GLUT
|
||||
glutInit(&argc, argv);
|
||||
|
||||
// 设置显示模式:单缓冲、RGB 颜色模式
|
||||
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
|
||||
|
||||
// 设置窗口大小和位置
|
||||
glutInitWindowSize(WIDTH, HEIGHT);
|
||||
glutInitWindowPosition(100, 100);
|
||||
|
||||
// 创建窗口
|
||||
glutCreateWindow("DDA算法");
|
||||
|
||||
// 注册回调函数
|
||||
glutDisplayFunc(display);
|
||||
|
||||
// 初始化设置
|
||||
init();
|
||||
|
||||
// 进入主循环
|
||||
glutMainLoop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
lab1/2.exe
Normal file
BIN
lab1/2.exe
Normal file
Binary file not shown.
86
lab1/3.cpp
Normal file
86
lab1/3.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <GL/glut.h> // 引入 GLUT 库,用于创建窗口和处理事件
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 辅助函数:根据八分对称性,绘制8个对称点
|
||||
void plotCirclePoints(int xc, int yc, int x, int y) {
|
||||
glBegin(GL_POINTS);
|
||||
// 第1象限
|
||||
glVertex2i(xc + x, yc + y);
|
||||
glVertex2i(xc + y, yc + x);
|
||||
// 第2象限
|
||||
glVertex2i(xc - y, yc + x);
|
||||
glVertex2i(xc - x, yc + y);
|
||||
// 第3象限
|
||||
glVertex2i(xc - x, yc - y);
|
||||
glVertex2i(xc - y, yc - x);
|
||||
// 第4象限
|
||||
glVertex2i(xc + y, yc - x);
|
||||
glVertex2i(xc + x, yc - y);
|
||||
glEnd();
|
||||
}
|
||||
|
||||
// 中点画圆算法主函数
|
||||
// 参数: (xc, yc) 为圆心坐标, r 为半径
|
||||
void midpointCircle(int xc, int yc, int r) {
|
||||
int x = 0;
|
||||
int y = r;
|
||||
// 初始决策参数 d = 3 - 2r
|
||||
int d = 3 - 2 * r;
|
||||
|
||||
// 绘制初始点
|
||||
plotCirclePoints(xc, yc, x, y);
|
||||
|
||||
// 循环计算并绘制第一个八分之一圆弧,直到 x <= y
|
||||
while (x <= y) {
|
||||
x++;
|
||||
if (d < 0) {
|
||||
// 中点在圆内,选择正右方的点 (x+1, y)
|
||||
d = d + 4 * x + 6;
|
||||
} else {
|
||||
// 中点在圆外或圆上,选择右下方的点 (x+1, y-1)
|
||||
d = d + 4 * (x - y) + 10;
|
||||
y--;
|
||||
}
|
||||
std::cout<< xc + x << ", " << yc + y << std::endl;
|
||||
// 绘制由当前(x, y)决定的8个对称点
|
||||
plotCirclePoints(xc, yc, x, y);
|
||||
}
|
||||
glFlush(); // 强制执行所有OpenGL命令
|
||||
}
|
||||
|
||||
// --- OpenGL 设置与回调函数 ---
|
||||
|
||||
// 显示回调函数
|
||||
void display() {
|
||||
glClear(GL_COLOR_BUFFER_BIT); // 清除颜色缓冲区
|
||||
|
||||
glColor3f(1.0, 0.0, 0.0); // 设置绘制颜色为红色
|
||||
glPointSize(2.0f); // 设置点的大小
|
||||
|
||||
// 调用中点画圆算法
|
||||
midpointCircle(50, 50, 30);
|
||||
}
|
||||
|
||||
// 初始化函数
|
||||
void init() {
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0); // 设置背景颜色为黑色
|
||||
glMatrixMode(GL_PROJECTION); // 设置投影矩阵
|
||||
gluOrtho2D(0.0, 500.0, 0.0, 500.0); // 定义二维正交投影,窗口坐标范围 (0,0) 到 (500,500)
|
||||
}
|
||||
|
||||
// --- 主函数 ---
|
||||
int main(int argc, char** argv) {
|
||||
glutInit(&argc, argv); // 初始化 GLUT
|
||||
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // 使用单缓冲和RGB颜色模型
|
||||
glutInitWindowSize(500, 500); // 设置窗口大小
|
||||
glutInitWindowPosition(100, 100); // 设置窗口位置
|
||||
glutCreateWindow("Midpoint Circle Algorithm"); // 创建窗口并设置标题
|
||||
|
||||
init(); // 调用初始化函数
|
||||
glutDisplayFunc(display); // 注册显示回调函数
|
||||
glutMainLoop(); // 进入 GLUT 事件处理主循环
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
lab1/3.exe
Normal file
BIN
lab1/3.exe
Normal file
Binary file not shown.
BIN
lab1/freeglut.dll
Normal file
BIN
lab1/freeglut.dll
Normal file
Binary file not shown.
175
lab2/lab2.cpp
Normal file
175
lab2/lab2.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <GL/glut.h>
|
||||
#include <iostream>
|
||||
|
||||
// --- 定义区域编码 ---
|
||||
#define INSIDE 0 // 0000
|
||||
#define LEFT 1 // 0001
|
||||
#define RIGHT 2 // 0010
|
||||
#define BOTTOM 4 // 0100
|
||||
#define TOP 8 // 1000
|
||||
|
||||
// --- 定义裁剪窗口结构 ---
|
||||
struct ClipWindow {
|
||||
float xmin, xmax, ymin, ymax;
|
||||
};
|
||||
|
||||
// --- 全局变量设置 ---
|
||||
ClipWindow rect;
|
||||
int x_0, y_0, x_1, y_1; // 线段端点
|
||||
|
||||
/**
|
||||
* 计算点 (x, y) 的区域编码
|
||||
*/
|
||||
int computeCode(float x, float y, ClipWindow rect) {
|
||||
int code = INSIDE;
|
||||
if (x < rect.xmin) code |= LEFT;
|
||||
else if (x > rect.xmax) code |= RIGHT;
|
||||
if (y < rect.ymin) code |= BOTTOM;
|
||||
else if (y > rect.ymax) code |= TOP;
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cohen-Sutherland 算法核心
|
||||
* 注意:这里传入坐标的引用,直接修改坐标值以得到裁剪后的结果
|
||||
*/
|
||||
void cohenSutherlandClip(float& x_0, float& y_0, float& x_1, float& y_1, ClipWindow rect) {
|
||||
int code1 = computeCode(x_0, y_0, rect);
|
||||
int code2 = computeCode(x_1, y_1, rect);
|
||||
bool accept = false;
|
||||
|
||||
while (true) {
|
||||
// 如果两端点都在窗口内 (0000 | 0000 == 0)
|
||||
if (!(code1 | code2)) {
|
||||
accept = true;
|
||||
break;
|
||||
}
|
||||
// 如果两端点都在窗口的同一侧外部 (例如都在左边:0001 & 0001 != 0) 按位与运算
|
||||
else if (code1 & code2) {
|
||||
break; // 直接舍弃
|
||||
}
|
||||
// 3. 线段部分在窗口内,需要求交点
|
||||
else {
|
||||
int code_out;
|
||||
float x, y;
|
||||
|
||||
// 选择窗口外的一个端点
|
||||
if (code1 != 0)
|
||||
code_out = code1;
|
||||
else
|
||||
code_out = code2;
|
||||
|
||||
// 计算交点
|
||||
if (code_out & TOP) {
|
||||
x = x_0 + (x_1 - x_0) * (rect.ymax - y_0) / (y_1 - y_0);
|
||||
y = rect.ymax;
|
||||
}
|
||||
else if (code_out & BOTTOM) {
|
||||
x = x_0 + (x_1 - x_0) * (rect.ymin - y_0) / (y_1 - y_0);
|
||||
y = rect.ymin;
|
||||
}
|
||||
else if (code_out & RIGHT) {
|
||||
y = y_0 + (y_1 - y_0) * (rect.xmax - x_0) / (x_1 - x_0);
|
||||
x = rect.xmax;
|
||||
}
|
||||
else if (code_out & LEFT) {
|
||||
y = y_0 + (y_1 - y_0) * (rect.xmin - x_0) / (x_1 - x_0);
|
||||
x = rect.xmin;
|
||||
}
|
||||
|
||||
// 用交点替换原来的外部端点
|
||||
if (code_out == code1) {
|
||||
x_0 = x; y_0 = y;
|
||||
code1 = computeCode(x_0, y_0, rect);
|
||||
}
|
||||
else {
|
||||
x_1 = x; y_1 = y;
|
||||
code2 = computeCode(x_1, y_1, rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果接受,则绘制裁剪后的线段(蓝色)
|
||||
if (accept) {
|
||||
glBegin(GL_LINES);
|
||||
glColor3f(0.0f, 0.0f, 1.0f); // 蓝色
|
||||
glVertex2f(x_0, y_0);
|
||||
glVertex2f(x_1, y_1);
|
||||
glEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// --- 显示回调函数 ---
|
||||
void myDisplay() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// 1. 绘制裁剪窗口 (红色线框)
|
||||
glColor3f(1.0f, 0.0f, 0.0f); // 红色
|
||||
glBegin(GL_LINE_LOOP);
|
||||
glVertex2f(rect.xmin, rect.ymin);
|
||||
glVertex2f(rect.xmax, rect.ymin);
|
||||
glVertex2f(rect.xmax, rect.ymax);
|
||||
glVertex2f(rect.xmin, rect.ymax);
|
||||
glEnd();
|
||||
|
||||
// 2. 绘制原始线段 (绿色)
|
||||
// 注意:为了演示效果,我们在调用裁剪函数前,先保存原始坐标,
|
||||
// 或者在这里直接用初始值绘制。这里我们使用初始值绘制。
|
||||
int orig_x_0 = 50, orig_y_0 = 350;
|
||||
int orig_x_1 = 550, orig_y_1 = 50;
|
||||
|
||||
glBegin(GL_LINES);
|
||||
glColor3f(0.0f, 1.0f, 0.0f); // 绿色
|
||||
glVertex2f(orig_x_0, orig_y_0);
|
||||
glVertex2f(orig_x_1, orig_y_1);
|
||||
glEnd();
|
||||
|
||||
// 3. 执行裁剪并绘制结果 (蓝色)
|
||||
// 使用 float 变量传入,因为算法内部会修改它们
|
||||
float cx_0 = orig_x_0, cy_0 = orig_y_0;
|
||||
float cx_1 = orig_x_1, cy_1 = orig_y_1;
|
||||
|
||||
// 加粗蓝色线条以便区分
|
||||
glLineWidth(3.0);
|
||||
cohenSutherlandClip(cx_0, cy_0, cx_1, cy_1, rect);
|
||||
glLineWidth(1.0); // 恢复线宽
|
||||
|
||||
glFlush();
|
||||
}
|
||||
|
||||
// --- 初始化设置 ---
|
||||
void Init() {
|
||||
glShadeModel(GL_FLAT);
|
||||
|
||||
rect.xmin = 100;
|
||||
rect.xmax = 400;
|
||||
rect.ymin = 100;
|
||||
rect.ymax = 300;
|
||||
}
|
||||
|
||||
// --- 窗口大小调整 ---
|
||||
void myReshape(int w, int h) {
|
||||
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
// 设置二维投影范围,与坐标数值对应
|
||||
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
|
||||
// --- 主函数 ---
|
||||
int main(int argc, char* argv[]) {
|
||||
glutInit(&argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
|
||||
glutInitWindowPosition(100, 100);
|
||||
glutInitWindowSize(600, 400);
|
||||
glutCreateWindow("实验2_03毕爽爽 Cohen-Sutherland 算法");
|
||||
|
||||
Init();
|
||||
|
||||
glutDisplayFunc(myDisplay);
|
||||
glutReshapeFunc(myReshape);
|
||||
|
||||
glutMainLoop();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user