计算机图形学头歌实训平台作业OpenGL实体消隐

第1关 立方体消隐

#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#include "model.h"
#include "geometry.h"
#include "pngimage.h"

using namespace std;
const double PI = acos(-1.0);

void line(Vec3i p0, Vec3i p1, PNGImage  &image, PNGColor color)
{
	bool steep = false;
	if (std::abs(p0.x - p1.x) < std::abs(p0.y - p1.y))
	{
		std::swap(p0.x, p0.y);
		std::swap(p1.x, p1.y);
		steep = true;
	}
	if (p0.x > p1.x)
	{
		std::swap(p0.x, p1.x);
		std::swap(p0.y, p1.y);
	}

	int dx = p1.x - p0.x;
	int dy = std::abs(p1.y - p0.y);

	int y = p0.y;
	int d = -dx;
	for (int x = p0.x; x <= p1.x; x++)
	{
		if (steep)
			image.set(y, x, color);
		else
			image.set(x, y, color);

		d = d + 2 * dy;
		if (d > 0)
		{
			y += (p1.y > p0.y ? 1 : -1);
			d = d - 2 * dx;
		}
	}
}

Vec3f barycentric(Vec3f *pts, Vec3f P)
{
	Vec3f u = Vec3f(pts[2].x - pts[0].x, pts[1].x - pts[0].x, pts[0].x - P.x) ^
		Vec3f(pts[2].y - pts[0].y, pts[1].y - pts[0].y, pts[0].y - P.y);
	/* `pts` and `P` has integer value as coordinates
	   so `abs(u[2])` < 1 means `u[2]` is 0, that means
	   triangle is degenerate, in this case return something with negative coordinates */
	if (std::abs(u.z) < 1)
		return Vec3f(-1, 1, 1);
	return Vec3f(1.f - (u.x + u.y) / u.z, u.y / u.z, u.x / u.z);
}

void triangle(Vec3f *pts, float *zbuffer, PNGImage &image, PNGColor color)
{
	int minX = min({ pts[0].x, pts[1].x, pts[2].x });
	int maxX = max({ pts[0].x, pts[1].x, pts[2].x });
	int minY = min({ pts[0].y, pts[1].y, pts[2].y });
	int maxY = max({ pts[0].y, pts[1].y, pts[2].y });

	int width = image.get_width();
	int height = image.get_height();
	if (maxX >= width)
		maxX = width - 1;
	if (maxY >= height)
		maxY = height - 1;

	Vec3f P;
	for (P.x = minX; P.x <= maxX; P.x++) {
		for (P.y = minY; P.y <= maxY; P.y++) {
			Vec3f bc_screen = barycentric(pts, P);
			if (bc_screen.x < 0 || bc_screen.y < 0 || bc_screen.z < 0) continue;
			P.z = 0;
			for (int i = 0; i < 3; i++)
				// 请补充代码,计算P点的深度坐标z
				/********** Begin ********/
                P.z += pts[i].z * bc_screen[i]; 
				/********** End **********/
			if (zbuffer[int(P.x + P.y*width)] < P.z) {
    	        /********** Begin ********/
                zbuffer[int(P.x + P.y*width)] = P.z;  
                image.set(P.x, P.y, color);
				/********** End **********/
			}
		}
	}
}

Matrix projection(Vec3f eye, Vec3f center)
{
	Matrix m = Matrix::identity(4);
	m[3][2] = -1.f / (eye - center).norm();
	return m;
}

Matrix viewport(int x, int y, int w, int h, int depth) {
	Matrix m = Matrix::identity(4);
	m[0][3] = x + w / 2.f;
	m[1][3] = y + h / 2.f;
	m[2][3] = depth / 2.f;

	m[0][0] = w / 2.f;
	m[1][1] = h / 2.f;
	m[2][2] = depth / 2.f;
	return m;
}

Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
	Vec3f z = (eye - center).normalize();
	Vec3f x = (up^z).normalize();
	Vec3f y = (z^x).normalize();
	Matrix res = Matrix::identity(4);
	for (int i = 0; i < 3; i++) {
		res[0][i] = x[i];
		res[1][i] = y[i];
		res[2][i] = z[i];
		res[i][3] = -center[i];
	}
	return res;
}

Matrix translation(Vec3f v) {
	Matrix Tr = Matrix::identity(4);
	Tr[0][3] = v.x;
	Tr[1][3] = v.y;
	Tr[2][3] = v.z;
	return Tr;
}

Matrix scale(float factorX, float factorY, float factorZ)
{
	Matrix Z = Matrix::identity(4);
	Z[0][0] = factorX;
	Z[1][1] = factorY;
	Z[2][2] = factorZ;
	return Z;
}

Matrix rotation_x(float angle)
{
	angle = angle * PI / 180;
	float sinangle = sin(angle);
	float cosangle = cos(angle);

	Matrix R = Matrix::identity(4);
	R[1][1] = R[2][2] = cosangle;
	R[1][2] = -sinangle;
	R[2][1] = sinangle;
	return R;
}

Matrix rotation_y(float angle)
{
	angle = angle * PI / 180;
	float sinangle = sin(angle);
	float cosangle = cos(angle);

	Matrix R = Matrix::identity(4);
	R[0][0] = R[2][2] = cosangle;
	R[0][2] = sinangle;
	R[2][0] = -sinangle;
	return R;
}

Matrix rotation_z(float angle) {
	angle = angle * PI / 180;
	float sinangle = sin(angle);
	float cosangle = cos(angle);

	Matrix R = Matrix::identity(4);
	R[0][0] = R[1][1] = cosangle;
	R[0][1] = -sinangle;
	R[1][0] = sinangle;
	return R;
}

int main(int argc, char** argv)
{
	const PNGColor white = PNGColor(255, 255, 255, 255);
	const PNGColor black = PNGColor(0, 0, 0, 255);
	const PNGColor red = PNGColor(255, 0, 0, 255);
	const PNGColor green = PNGColor(0, 255, 0, 255);
	const PNGColor blue = PNGColor(0, 0, 255, 255);
	const PNGColor yellow = PNGColor(255, 255, 0, 255);
	const PNGColor orange = PNGColor(255, 128, 0, 255);
	const PNGColor clr[6] = {green, yellow, blue, red, white, orange};

	Model *model = NULL;
	const int width = 800;
	const int height = 800;
	const int depth = 255;

	//generate some image
	PNGImage image(width, height, PNGImage::RGBA); //Error when RGB because lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size() in encode
	image.init(black);
	model = new Model("cube.obj");

	float *zbuffer = new float[width*height];
	for (int i = 0; i < width * height; i++)
		zbuffer[i] = -numeric_limits<float>::max();

	Vec3f eye(0, 1.2, 4),center(0, 0, 0), up(0, 1, 0);
	Matrix ModelView = lookat(eye, center, up) * rotation_y(45) * scale(0.4, 0.4, 0.4);
	Matrix Projection = projection(eye, center);
	Matrix ViewPort = viewport(width / 4, width / 4, width / 2, height / 2, depth);

	for (int i = 0; i < model->nfaces(); i++)
	{
		vector<int> face = model->face(i);
		if ((int)face.size() == 4)
		{
			Vec3f triangleVertex1[3], triangleVertex2[3];
			for (int j = 0; j < 3; j++)
			{
				Vec3f v1 = model->vert(face[j]);
				Vec3f v2 = model->vert(face[(j + 2) % 4]);

				triangleVertex1[j] = ViewPort * Projection * ModelView * v1;
				// 请补充代码,完成triangleVertex2[j]的计算
				/********** Begin ********/
                triangleVertex2[j] = ViewPort * Projection * ModelView * v2; 

				/********** End **********/
			}
			// 请将triangle函数补充完整
			/********** Begin ********/			
			triangle(triangleVertex1, zbuffer, image, clr[i]);  
            triangle(triangleVertex2, zbuffer, image, clr[i]); 
			/********** End **********/
		}
	}

	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_png_file("../img_step3/test.png");
	delete model;
	delete zbuffer;
	return 0;
}
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实验一 OpenGL+GLUT开发平台搭建 5 小实验1: 开发环境设置 5 小实验2: 控制窗口位置和大小 6 小实验3:默认的可视化范围 6 小实验4:自定义可视化范围 7 小实验5: 几何对象变形的原因 8 小实验6: 视口坐标系及视口定义 8 小实验7:动态调整长宽比例,保证几何对象不变形 9 实验二 动画和交互 10 小实验1: 单缓冲动画技术 10 小实验2: 双缓冲动画技术 11 小实验3:键盘控制 13 小实验4:鼠标控制【试着单击鼠标左键或者右键,试着按下鼠标左键后再移动】 14 实验三 几何变换、观察变换、三维对象 16 小实验1:二维几何变换 16 小实验2:建模观察(MODELVIEW)矩阵堆栈 17 小实验3:正平行投影1 19 小实验4:正平行投影2 19 小实验5:正平行投影3 20 小实验6:透射投影1 21 小实验6:透射投影2 22 小实验7:三维对象 24 实验四 光照模型和纹理映射 26 小实验1:光照模型1----OpenGL简单光照效果的键步骤。 26 小实验2:光照模型2----光源位置的问题 28 小实验3:光照模型3----光源位置的问题 31 小实验4:光照模型4----光源位置的问题 33 小实验5:光照模型5----光源位置的问题 35 小实验6:光照模型6----光源位置的问题 38 小实验7:光照模型7----光源位置的动态变化 40 小实验8:光照模型8----光源位置的动态变化 43 小实验9:光照模型9---光源位置的动态变化 45 小实验10:光照模型10---聚光灯效果模拟 48 小实验11:光照模型11---多光源效果模拟 50 小实验12:光照效果和雾效果的结合 53 小实验13:纹理映射初步—掌握OpenGL纹理映射的一般步骤 56 小实验13:纹理映射—纹理坐标的自动生成(基于参数的曲面映射) 59 小实验14:纹理映射—纹理坐标的自动生成(基于参考面距离) 61
计算机图形学是研究计算机如何生成、处理和显示图像的学科。而educoder实训是向学习者提供一种动手实践的方式,通过编写代码来深入理解计算机图形学的基本原理和操作。 OpenGL是一种用于计算机图形学的开放标准和编程接口。下面是使用OpenGL绘制点的代码示例: ```cpp #include <GL/glut.h> void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); glColor3f(1.0, 0.0, 0.0); // 设置点的颜色为红色 glVertex2f(0.0, 0.0); // 设置点的坐标为(0, 0) glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB); glutInitWindowSize(400, 400); glutInitWindowPosition(100, 100); glutCreateWindow("OpenGL Point"); glutDisplayFunc(display); glutMainLoop(); return 0; } ``` 这段代码使用OpenGL库函数绘制了一个红色的点,坐标为(0, 0)。首先,我们使用`glClear`函数清除颜色缓冲区,然后使用`glBegin`开始定义绘制的图形类型为点。接着,使用`glColor3f`设置点的颜色为红色,然后使用`glVertex2f`定义点的坐标。最后,使用`glEnd`结束绘制,使用`glFlush`刷新绘制结果。 在`main`函数中,我们使用`glutInit`初始化OpenGL运行环境,使用`glutInitDisplayMode`设置显示模式为RGB,使用`glutInitWindowSize`设置窗口大小,使用`glutInitWindowPosition`设置窗口位置,使用`glutCreateWindow`创建窗口,使用`glutDisplayFunc`指定渲染函数为`display`,最后使用`glutMainLoop`进入事件循环。 这段代码可以在educoder实训平台中运行,让学习者通过编写和调试代码来学习OpenGL的使用和基本图形的绘制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值