我需要一个用opengl写的能进行图片平移旋转变换缩放的小程序代码
时间: 2024-03-17 18:41:46 浏览: 114
好的,以下是一个简单的用OpenGL实现图片平移、旋转、缩放的示例程序,你可以在此基础上进行修改:
```c++
#include <GL/glut.h>
#include <opencv2/opencv.hpp>
using namespace cv;
// 图片文件名
const char* FILENAME = "test.jpg";
// 图片数据
Mat img;
// 图片显示区域
GLfloat rect[4] = {0.0, 0.0, 0.0, 0.0};
// 鼠标按下时的坐标
int lastX, lastY;
// 缩放比例
GLfloat scale = 1.0;
// 旋转角度
GLfloat angle = 0.0;
// 是否按下鼠标左键
bool isLeftButtonDown = false;
// 是否按下鼠标右键
bool isRightButtonDown = false;
// 加载图片
void loadTexture()
{
img = imread(FILENAME, CV_LOAD_IMAGE_COLOR);
if (img.empty())
{
exit(-1);
}
rect[2] = img.cols;
rect[3] = img.rows;
}
// 初始化OpenGL
void init()
{
glEnable(GL_TEXTURE_2D);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, 600, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 600.0, 600.0, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// 绘制图片
void drawImage()
{
glBindTexture(GL_TEXTURE_2D, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.cols, img.rows, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, img.data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glPushMatrix();
glTranslatef(rect[0], rect[1], 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glScalef(scale, scale, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex2f(rect[2], 0.0);
glTexCoord2f(1.0, 1.0); glVertex2f(rect[2], rect[3]);
glTexCoord2f(0.0, 1.0); glVertex2f(0.0, rect[3]);
glEnd();
glPopMatrix();
}
// 显示回调函数
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawImage();
glutSwapBuffers();
}
// 鼠标按下事件回调函数
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
lastX = x;
lastY = y;
isLeftButtonDown = true;
}
else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
isLeftButtonDown = false;
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
lastX = x;
lastY = y;
isRightButtonDown = true;
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
isRightButtonDown = false;
}
}
// 鼠标移动事件回调函数
void motion(int x, int y)
{
if (isLeftButtonDown)
{
rect[0] += x - lastX;
rect[1] += y - lastY;
lastX = x;
lastY = y;
glutPostRedisplay();
}
else if (isRightButtonDown)
{
angle += (x - lastX) / 10.0;
lastX = x;
lastY = y;
glutPostRedisplay();
}
}
// 滚轮事件回调函数
void mouseWheel(int button, int dir, int x, int y)
{
if (dir > 0)
{
scale *= 1.1;
}
else
{
scale /= 1.1;
}
glutPostRedisplay();
}
// 主函数
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL Test");
init();
loadTexture();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMouseWheelFunc(mouseWheel);
glutMainLoop();
return 0;
}
```
需要注意的是,此示例程序使用了OpenCV库来加载图片,如果你没有安装此库,请自行安装。
阅读全文