计算机图形学图形变换

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

#include "pch.h"
#include<gl/GLUT.H>
#include<windows.h>
#include<math.h>
#include <stdlib.h>
#include<windows.h>
#include <iostream>
#define T 0
#define R 1
using namespace std;
float triangle[3][3]; //初始三角形
float tra_tri[3][3];//平移后的三角形
float rotate_tri[3][3];//旋转后三角形
int s; //选择平移或旋转
void init(void)
{
    
    
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_SMOOTH);
}
void input() {
    
    
	cout << "input coordinate:" << endl;     //输入三角形坐标
	for (int i = 0; i < 3; i++) {
    
    
		for (int j = 0; j < 2; j++) {
    
    
			cin >> triangle[i][j];           
		}
		triangle[i][2] = 1;           //令矩阵第三列都是1
	}
	cout << "translation:0" << endl;
	cout << "rotate:1" << endl;
	cin >> s;
}
void draw_triangle(float a[3][3])    //画三角形
{
    
    
	glShadeModel(GL_SMOOTH);
	glColor3f(0.2, 0.7, 0.30);
	glBegin(GL_TRIANGLES);//画出三角形,为混合色填充方式
	glVertex2f(a[0][0],a[0][1]);

	glColor3f(0.4, 0.5, 0.60);
	glVertex2f(a[1][0],a[1][1]);

	glColor3f(0.9, 0.7, 0.8);
	glVertex2f(a[2][0],a[2][1]);
	glEnd();
}
void translate() {
    
                        //平移
	double tra_matrix[3][3] = {
    
     {
    
    1,0,0},{
    
    0,1,0},{
    
    0,0,1} };   //初始化要相乘的矩阵
	double tra_x, tra_y;

	cout << "Enter translation distance:" << endl;
	cin >> tra_x >> tra_y;
	tra_matrix[2][0] = tra_x;
	tra_matrix[2][1] = tra_y;

	for (int i = 0; i < 3; i++) {
    
    
		for (int j = 0; j < 3; j++) {
    
    
			for (int k = 0; k < 3; k++)
				tra_tri[i][j] += triangle[i][k] * tra_matrix[k][j];    //矩阵相乘
		}
	}
}
void rotate() {
    
               //旋转
	float x, y;
	float rotate_matrix[3][3];
	cout << "Enter rotation distance:" << endl;
	cin >> x >> y;
	cout << "Enter rotation angle:" << endl;
	int theta;//定义旋转角度
	cin >> theta;
	rotate_matrix[0][0] = cos(theta);
	rotate_matrix[0][1] = -sin(theta);
	rotate_matrix[0][2] = x * (1 - cos(theta)) + y * sin(theta);
	rotate_matrix[1][0] = sin(theta);
	rotate_matrix[1][1] = cos(theta);
	rotate_matrix[1][2] = y * (1 - cos(theta)) - x * sin(theta);
	rotate_matrix[2][0] = 0;
	rotate_matrix[2][1] = 0;
	rotate_matrix[2][2] = 1;

	for (int i = 0; i < 3; i++) {
    
    
		for (int j = 0; j < 3; j++) {
    
    
			for (int k = 0; k < 3; k++)
				rotate_tri[i][j] += triangle[i][k] * rotate_matrix[k][j];
		}
	}
}
void display(void)
{
    
    
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glColor3f(1.0, 1.0, 1.0);
	draw_triangle(triangle);//画出原始三角形
	if (s == T) {
    
    
		translate();
		draw_triangle(tra_tri);//经过变换后画出图形
	}

	if (s == R) {
    
    
		rotate();
		draw_triangle(rotate_tri);//经过变换后画出图形
	}

	glFlush();

	//position vertex[3] = { {100.0,200.0},{150.0,100.0},{200.0,250.0} };
	//glColor3f(1.0, 1.0, 1.0);
	//glTranslatef(-100.0, -50.0, 1.0);
	//glLoadIdentity();
	//glTranslatef(0.0, 100.0, 1.0);
	//glRotatef(90.0, 0.0, 0.0, 1.0);

	//glScalef(0.5, 0.5, 1.0);

	//draw_triangle();//经过三种变换后画出图形
	//glFlush();
}

void reshape(int w, int h)
{
    
    
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//if (w <= h)
		gluOrtho2D(-200.0, 250.0, -100.0*(GLfloat)h / (GLfloat)w,
			200.0*(GLfloat)h / (GLfloat)w);//调整裁剪窗口
	//else
	//	gluOrtho2D(-200.0*(GLfloat)w / (GLfloat)h,
			//250.0*(GLfloat)w / (GLfloat)h, -50.0, 200.0);
	glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
    
    
	glutInit(&argc, argv); //初始化glut
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	//设置窗口的模式-深度缓存,单缓存,颜色模型
	glutInitWindowPosition(100,100); //设置窗口的位置
	glutInitWindowSize(600, 600); //设置窗口的大小

	glutCreateWindow("3D Tech- GLUT Tutorial"); //创建窗口并赋予title
	init();
	input();
	glutDisplayFunc(display);	//调用renderScene把绘制传送到窗口
	glutReshapeFunc(reshape);
	glutMainLoop(); //进入循环等待
	return 0;
}


在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42964413/article/details/105727846