非常详细地解释了一下代码原理
全网独家一份的保姆级了
#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;
}