【要求】

实现自己的三角形线框显示算法,能够将三角形的各个顶点经过坐标变换后变换到屏幕坐标,并以连线的方式将三角形的各个边显示出来。要求连线不使用Windows API,而是利用自己的2D Line算法,例如:2DDA算法。

方法1  DDA算法

理论学习 参考博客  博客1   博客2

代码(创建glut工程)

#include<windows.h>
#include<bits/stdc++.h>
#include <GL/glut.h>
using namespace std;
struct node
{
    float x,y,z;
};
vector<node> G;

vector<node> Get_Point()
{
    vector<node> G;
    G.push_back({0, 0, 0});
    G.push_back({0.1, 0.1, 0.1});
    G.push_back({0.2, 0.2, 0.2});
    return G;
}

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(2.0f);
    glBegin(GL_POINTS);

//    printf("%d\n", G.size());
//    G = Get_Point();
    for(int i = 0; i < G.size(); ++i){
        cout<<G[i].x<<" "<<G[i].y<<" "<<G[i].z<<endl;
        glVertex3f(G[i].x/100, G[i].y/100, G[i].z/100);
    }
    glEnd();
    glFlush();
}

void init(int argc,char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(1000,1000);
    glutCreateWindow("Three Window");
}


inline int round(const float a) {return int (a + 0.5);}
void lineDDA(int x0,int y0,int xEnd,int yEnd){
     int dx = xEnd - x0, dy = yEnd - y0,steps, k ;
	float xIncrement, yIncrement,x = x0,y = y0;
	steps = max(fabs(dx), fabs(dy));

	xIncrement = float (dx) / float (steps);
	yIncrement = float (dy) / float (steps);
	cout<<"xIncrement: "<<xIncrement<<" yIncrement: "<<yIncrement<<endl;
	int id = 1;
	G.push_back({round(x), round(y), ++id});
	for(k = 0;k<steps;k++){
	    x += xIncrement;
	    y += yIncrement;
	    G.push_back({round(x), round(y), ++id});
	}
}




int main(int argc,char* argv[])
{
    init(argc, argv);
//    lineDDA(10, 10, 75, 75);
    lineDDA(0, 0, 10, 10);
    lineDDA(0, 0, 0, 10);
    lineDDA(0, 10, 10, 10);
    glutDisplayFunc(&Display);
    glutMainLoop();
    return 0;
}

方法2 调用windows的API (创建glut工程)

#include<windows.h>
#include <GL\glut.h>

void init(){//貌似是坐标变换
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,200.0,0.0,150.0);
}


void drawLine(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.4,0.2);
    int point1[] = { 50, 100 };
    int point2[] = { 50, 200 };
    int point3[] = { 200, 100 };

    glBegin(GL_LINES);
        glVertex2i(10, 10);
        glVertex2i(75, 75);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(75, 75);
        glVertex2i(75, 10);
    glEnd();

    glBegin(GL_LINES);
    glVertex2i(75, 10);
    glVertex2i(10, 10);
    glEnd();

    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("三角形");
    init();
    glutDisplayFunc(&drawLine);

    glutMainLoop();
    return 0;
}

其他

用glfw画各种图形

参考官网代码:官网  

主体代码就是在这个位置写

计算机图形学  画三角形、圆等(入门代码)_#include

 

#include<GLFW/glfw3.h>
#include<math.h>

const float Pi = acos(-1);
const float R = 0.5f;
const int n = 100;

int main(void)
{
	GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		/* Render here */
		glClear(GL_COLOR_BUFFER_BIT);

		//画三角形
		/*glBegin(GL_TRIANGLES);
			glVertex2d(0, 0.5f);
			glVertex2d(0.5f, -0.5f);
			glVertex2d(-0.5f, -0.5f);
		glEnd();*/
		

		/*
			画圆
		*/
		glBegin(GL_LINE_LOOP);

		for (int i = 0; i < n; ++i) {
			glVertex2d(R*sin(2.0*Pi / n * i), R*cos(2.0*Pi / n * i));
		}
		glEnd();



		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}