OpenGl自学记录_看了必会_全网最全保姆级齐次平移

这篇博客详细介绍了使用OpenGL进行二维图形的齐次平移变换,通过C++实现了一个简单的图形窗口,利用鼠标点击事件进行图形平移,并展示了如何进行几何变换和矩阵乘法。内容涵盖GLUT库的使用、坐标轴绘制、平移矩阵的构建及应用。
摘要由CSDN通过智能技术生成

非常详细地解释了一下代码原理
全网独家一份的保姆级了在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#define GLUT_DISABLE_ATEXIT_HACK
#include "GLUT.H"
#include<math.h>
#include <string.h> 

#define ZVALUE 20.0f

int w_width = 600;
int w_height = 600;
int lineWidth;

//非齐次二维几何变换
struct my_v_homogeneous
{
	float x;
	float y;
	int ratio;
};

struct my_v_homogeneous triangle[3];

void init(void)
{
	triangle[0].x = 0;
	triangle[0].y = 200;
	triangle[0].ratio = 1;

	triangle[1].x = -100*sqrt(3);
	triangle[1].y = -100;
	triangle[1].ratio = 1;

	triangle[2].x = 100*sqrt(3);
	triangle[2].y = -100;
	triangle[2].ratio = 1;

}

void draw_coordinate()
{
	glBegin(GL_LINES);
	glColor3f(1.0, 0.0, 0.0);
	glVertex2f(w_width, 0.0);
	glVertex2f(-w_width, 0.0);
	glColor3f(0.0, 1.0, 0.0);
	glVertex2f(0.0, w_height);
	glVertex2f(0.0, -w_height);
	glEnd();
}

void display(void)
{
	glClearColor(1.f, 1.f, 1.f, 0.f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	draw_coordinate();
	glColor3f(0, 0, 0);

	glBegin(GL_LINE_LOOP); //GL_POINTS
	for (int vIndex = 0; vIndex < 2; vIndex++)
	{
		glVertex2f(triangle[vIndex].x, triangle[vIndex].y);
		glVertex2f(triangle[vIndex+1].x, triangle[vIndex+1].y);
	}
	glEnd();

	glutSwapBuffers();

}
struct my_v_homogeneous matrix_multiply_vector(float matrix[][3], struct my_v_homogeneous input_v) 
{
	struct my_v_homogeneous translated_v;  
	translated_v.x = matrix[0][0] * input_v.x + matrix[0][1] * input_v.y + matrix[0][2] * 1; 
	translated_v.y = matrix[1][0] * input_v.x + matrix[1][1] * input_v.y + matrix[1][2] * 1; 
	translated_v.ratio = matrix[2][0] * input_v.x + matrix[2][1] * input_v.y + matrix[2][2] * 1; 
	return translated_v;
}

void my_traslate_homogeneous(struct my_v_homogeneous* polygon, int polygon_vertex_count, int tx, int ty)
//tx表示x方向的平移量,ty表示y方向的平移量
{
	//装配生成平移矩阵
	float translate_matrix[3][3];
	memset(translate_matrix, 0, sizeof(int) * 9);
	//memset() 函数可以说是初始化内存的“万能函数”,通常为新申请的内存进行初始化工作。它是直接操作内存空间,mem即“内存”(memory)的意思。
	translate_matrix[0][0] = translate_matrix[1][1] = translate_matrix[2][2] = 1;
	translate_matrix[0][2] = tx;
	translate_matrix[1][2] = ty;
	for (int vIndex = 0; vIndex < polygon_vertex_count; vIndex++)
	{
		struct my_v_homogeneous input_v;
		input_v.x = polygon[vIndex].x;
		input_v.y = polygon[vIndex].y;
		input_v.ratio = 1;
		input_v = matrix_multiply_vector(translate_matrix, input_v); 
		//平移矩阵作用到每个顶点,即矩阵顶点(向量)相乘
		polygon[vIndex].x = input_v.x;
		polygon[vIndex].y = input_v.y;
	}
}


void mouse(int button, int state, int x, int y) 
{
	switch (button) 
	{
	case GLUT_LEFT_BUTTON:
		if (state == GLUT_DOWN) 
		{
		my_traslate_homogeneous(triangle, 3, 40, 50);//齐次平移
		glutPostRedisplay();
		}
		break;
	case GLUT_RIGHT_BUTTON:
		if (state == GLUT_DOWN)
		{
			my_traslate_homogeneous(triangle, 3, -40, -50);//齐次平移
			glutPostRedisplay();
		}
		break;
	default:
		break;
	}
}







//投影方式、modelview方式等设置
void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h); 
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	if (w <= h)
		glOrtho(-0.5 * w_width, 0.5 * w_width, -0.5 * w_height * (GLfloat)w_height / (GLfloat)w_width, 0.5 * w_height * (GLfloat)w_height / (GLfloat)w_width, -ZVALUE, ZVALUE); 
	else 
		glOrtho(-0.5 * w_width, 0.5 * w_width, -0.5 * w_height * (GLfloat)w_width / (GLfloat)w_height, 0.5 * w_height * (GLfloat)w_width / (GLfloat)w_height, -ZVALUE, ZVALUE); 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity();
}



//主调函数


int main(int argc, char** argv) 
{
	glutInit(&argc, argv); 
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
	glutInitWindowSize(w_width, w_height); 
	glutInitWindowPosition(100, 100); 
	glutCreateWindow("二维图形齐次平移变换"); 
	init(); 
	glutReshapeFunc(reshape); 
	glutDisplayFunc(display); 
	glutMouseFunc(mouse); 
	glutMainLoop(); 
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值