图形学实验 图像平移缩放旋转 OpenGL实现

时间:2022-12-19 19:35:09

图形学实验一

运行环境:

实验工程和配置: 点击下载实验工程

Windows 10 

Visual Studio 2017 已配置lib和include文件夹

系统文件配置32和64位的OpenGL环境。

实验一 图形的 平移 缩放

鼠标左右击缩放,方向键控制移动。(一开始在左下角,所以容易看不见)
// test1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK 
#pragma comment(lib, "Ws2_32.lib")
#include <cmath>
#include <cstdlib>
#include <gl/glut.h>

float width, highth, angle;
void init(void)//画正方形

{

	glClearColor(1.0, 1.0, 6.0, 1.0);//背景颜色

	glMatrixMode(GL_PROJECTION);// 投影

	gluOrtho2D(0.0, 600.0, 0.0, 600.0);//参数分别代表(左下角x坐标,右上角x坐标,左下角y坐标,右上角y坐标)

}

void display(void)

{

	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(0.0, 1.0, 1.0);//矩形颜色
	GLfloat
		PointA[2] = { 0+width,50+highth },
		PointB[2] = { 10 + width,10 + highth },
		PointC[2] = { 50 + width,0 + highth },
		PointD[2] = { 10 + width,-10 + highth },
		PointE[2] = { 0 + width,-50 + highth },
		PointF[2] = { -10 + width,-10 + highth },
		PointG[2] = { -50 + width,0 + highth },
		PointH[2] = { -10 + width,10 + highth };

	glBegin(GL_LINE_LOOP);//闭合折线
	glLineWidth(6.0);
	glVertex2fv(PointA);
	glVertex2fv(PointB);
	glVertex2fv(PointC);
	glVertex2fv(PointD);
	glVertex2fv(PointE);
	glVertex2fv(PointF);
	glVertex2fv(PointG);
	glVertex2fv(PointH);
	glEnd();

	glFlush();

}

void mySpecialKeyboard(int key, int x, int y)

{

	if (key == GLUT_KEY_RIGHT)

		width += 5;

	if (key == GLUT_KEY_LEFT)

		width -= 5;

	if (key == GLUT_KEY_UP)

		highth += 5;

	if (key == GLUT_KEY_DOWN)

		highth -= 5;

	glutPostRedisplay();


}

void myKeyboard(unsigned char key, int x, int y)

{


	glutPostRedisplay();

}
void mymouse(int button, int state, int x, int y)//鼠标控制缩放
{
	if (state == GLUT_DOWN)
	{
		if (button == GLUT_LEFT_BUTTON)
		{

			glScalef(0.5, 0.5, 0.0);
			display();

		}
		else if (button == GLUT_RIGHT_BUTTON)
		{
			glScalef(1.5, 1.5, 0.0);
			display();
		}
		glutPostRedisplay();//重新调用绘制函数
	}
	return;
}

int main(int argc, char** argv)

{

	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

	glutInitWindowPosition(50, 50);

	glutInitWindowSize(600, 600);

	glutCreateWindow("王子源,152210708117,鼠标右键放大,方向键移动");

	init();

	glutDisplayFunc(display);

	glutSpecialFunc(mySpecialKeyboard);

	glutMouseFunc(&mymouse);
	glutKeyboardFunc(myKeyboard);
	glutMainLoop();
	return 0;
}

实验一 图像的旋转

AS控制旋转方向

// test3.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK 
#pragma comment(lib, "Ws2_32.lib")
#include <cmath>
#include <cstdlib>
#include <gl/glut.h>
float DEG_TO_RAD = 0.0;  //角度转为弧度的参数,即 2*PI/360   
float theta = 30.0; //直线与X轴正方向的夹角 
float length = 200.0;  //直线的长度 
float xf=300.0, yf = 200.0; //直线的第一个端点   
void init(void)
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 1.0);
	glBegin(GL_POLYGON);

	
	glVertex2f(xf +  (0)*cos(DEG_TO_RAD)-  (200)*sin(DEG_TO_RAD), yf +(200)*cos(DEG_TO_RAD) +0*sin(DEG_TO_RAD));
	glVertex2f(xf + (200)*cos(DEG_TO_RAD) -  (0)*sin(DEG_TO_RAD), yf +  (0)*cos(DEG_TO_RAD) + 200 * sin(DEG_TO_RAD));
	glVertex2f(xf + (30)*cos(DEG_TO_RAD) +    30*sin(DEG_TO_RAD), yf -   30*cos(DEG_TO_RAD) + 30 * sin(DEG_TO_RAD));
	glVertex2f(xf +  (0)*cos(DEG_TO_RAD) + (200)*sin(DEG_TO_RAD), yf -(200)*cos(DEG_TO_RAD) + 0 * sin(DEG_TO_RAD));
	glVertex2f(xf -   30*cos(DEG_TO_RAD) +  (30)*sin(DEG_TO_RAD), yf - (30)*cos(DEG_TO_RAD) -30 * sin(DEG_TO_RAD));
	glVertex2f(xf -  200*cos(DEG_TO_RAD) -   (0)*sin(DEG_TO_RAD), yf +  (0)*cos(DEG_TO_RAD) -200 * sin(DEG_TO_RAD));

	glEnd();
	glutSwapBuffers();     //交换前后台缓存
}

void myKeyboard(unsigned char key, int x, int y)
{
	
	if (key == 'a' || key == 'A')
		DEG_TO_RAD += 0.1;
	if (key == 's' || key == 'S')
		DEG_TO_RAD -= 0.1;
	if (key == 'c' || key == 'C')
		exit(0);
	if (DEG_TO_RAD>360) DEG_TO_RAD -= 360;
	if (DEG_TO_RAD<0) DEG_TO_RAD += 360;
	glutPostRedisplay(); //重新调用绘制函数  
}
void main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(680, 680);
	glutCreateWindow("152210708117 A键左转,S键右转");
	init();
	glutDisplayFunc(display);
	glutKeyboardFunc(myKeyboard);
	// glutIdleFunc(idleFunc);             //指定空闲回调函数      
	glutMainLoop();
}

实验结果

图形学实验 图像平移缩放旋转 OpenGL实现

图形学实验 图像平移缩放旋转 OpenGL实现

图形学实验 图像平移缩放旋转 OpenGL实现