计算机图形学初步

学网上的很多教程配置了环境,要使用的软件在这里有。

第一份图形学程序:

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include <GLFW/glfw3.h>  

// Window dimensions    
const GLuint WIDTH = 1600, HEIGHT = 900;

void display()
{
	// do some change begauce glfw's axis is different
	GLfloat vertices[3][3] = { { 0.0, 10.0, 0.0 }, { -5.0, 0.0, 0.0 }, { 5.0, 0.0, 0.0 } };

	// copy from the assignment which Professor Li gave me

	// an arbitrary triangle in the plane z = 0;
	GLfloat p[3] = { 7.5, 5.0, 0.0 };
	// or set any desired initial point which is inside the triangle;
	int  j, k;
	int  rand();
	int total_points = 500;
	glBegin(GL_POINTS);
	for (k = 0; k < total_points; k++)
	{
		/* pick a random vertex from 0, 1, 2*/
		j = rand() % 3;
		// compute new location;
		p[0] = (p[0] + vertices[j][0]) / 2;
		p[1] = (p[1] + vertices[j][1]) / 2;
		// display new point
		glVertex3fv(p);
	}
	glEnd();
	glFlush();
}


int main()
{
	// learn from https://www.cnblogs.com/AnKen/p/8057000.html
	glfwInit();

	// Create a GLFWwindow object that we can use for GLFW's functions    
	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
	if (window == nullptr)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);


	// learn from https://stackoverflow.com/questions/57336940/how-to-glutdisplayfunc-glutmainloop-in-glfw 
	while (!glfwWindowShouldClose(window))
	{
		// do the drawing
		display();

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
原文地址:https://www.cnblogs.com/Inko/p/11614079.html