感谢none爹的支持和修改建议!!!!
task1主要是跟着pdf做,没什么好讲的,代码也比较直接干脆,就写在下面了。主要是熟悉OpenGL画图的结构和理解流程。
//File ID: Lab03a.cpp
//Title: Working with Graphics Primitives
//Author:
#define FREEGLUT_STATIC
#include
#include
void define_to_OpenGL();
/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//Task 1
glutCreateWindow("Graphics Primitives");
glutDisplayFunc(define_to_OpenGL);
glutMainLoop();
return 0;
}
/
void define_to_OpenGL()
{
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
//The stuff to appear on screen goes here
glutInitWindowSize(600, 400);
glutInitWindowPosition(50, 50);
int L = -100, R = 500, B = -200, T = 200;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(L, R, B, T);
//Task 3
glLineWidth(1.0);
glColor3f(1 , 1 , 0);
glBegin(GL_LINES);
glVertex2f(0.0, 0.0); // start location
glVertex2f(450.0, 0.0); // end location
glEnd();
glColor3f(1, 0, 1);
glBegin(GL_LINES);
glVertex2f(0.0, -150.0); // start location
glVertex2f(0.0, +150.0); // end location
glEnd();
//Task 4
glPointSize(10);
glColor3f(0 , 0 , 0);
glBegin(GL_POINTS);
glVertex2f(0 , 0);
glEnd();
//Task 5
// draw a sine wave
int i;
float x, y;
glColor3f(0.0, 0.0, 1.0);
glPointSize(1);
glBegin(GL_POINTS);
for (i = 0; i < 361; i = i + 5)
{
x = (float)i;
y = 100.0 * sin(i * (6.284 / 360.0));
glVertex2f(x, y);
}
glEnd();
//Tasks 6, 7 and 8
glBegin(GL_TRIANGLES);
glColor3f(1 , 0 , 0 );
glVertex2f(-50 , -50 );
glColor3f(0 , 1 , 0 );
glVertex2f(-50 , 0 );
glColor3f(0 , 0, 1 );
glVertex2f(0 , 0 );
glShadeModel(GL_FLAT);
glEnd();
glFlush();
}
task2的主要难点是要使用cin和cout来得到四组坐标(两条直线),这一点居然可以在define_to_opengl()这个方法里面直接加入,也就是说这里面不止可以存在opengl的方法,其他想要在画图时实现的代码也可以。我感觉有其他的设计方式也可以实现,如果有其他的思路也欢迎分享。DDA的公式、计算长度斜率、判断平行的公式都包装成方法了。
#define FREEGLUT_STATIC
#include
#include
#include
using namespace std;
void define_to_OpenGL(); // Draw geometric elements and output results
void init(); // Initialise coordinates of line end points
void DDA(double x1, double y1, double x2, double y2) {
double m;
m = double((y2 - y1) / (x2 - x1));
cout <<"m = " << m << endl;
if (m > 1) { //判断m大于或小于1两种情况, 若m大于1 则遍历x,否则遍历y
double y = y1;
double yi[100]; //记录y坐标的数组
for (int x = x1; x <= x2; x++) {
yi[x] = round(y);//四舍五入得到y坐标
y += m;
}
for (int x = x1; x <= x2; x++) { //打印所有坐标
cout << "x = " << x << " y = " << yi[x]<> xa >> ya;
cin >> xb >> yb;
cin >> xc >> yc;
cin >> xd >> yd;
l1 = countLength(xa, ya, xb, yb);
l2 = countLength(xc, yc, xd, yd);
g1 = countGradient(xa, ya, xb, yb);
g2 = countGradient(xc, yc, xd, yd);
//cout << xa << endl << ya << endl << xb << endl << yb<