GLUT通过单击,拖动以平移来缩放到光标

时间:2016-06-24 08:42:10

标签: c++ opengl 2d

我尝试将此功能设为Google地图或http://phrogz.net/tmp/canvas_zoom_to_cursor.html

如何使用缩放中心等鼠标位置?

如何通过移动光标拖动图像?想要按钮== 0&&状态== GLUT_DOWN。

任何帮助非常感谢

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "glut.h"

#define windowSize 900.0
double zoomFactor = 1;
int mouseX=0, mouseY=0;
void reshape (int w, int h)
{
}
void mouse(int button, int state, int x, int y) {
if (button == 0)
{
   if (state == GLUT_UP) return;
   zoomFactor -= 0.05; 
}
else if (button == 2) {
    if (state == GLUT_UP) return;
    zoomFactor += 0.05;
}
else return;
glutPostRedisplay();
}
void motion(int ax, int ay) {
mouseX = ax;
mouseY = ay;
}

Void display(){
glClear(GL_COLOR_BUFFER_BIT);
glViewport (0.0, 0.0, glutGet(GLUT_INIT_WINDOW_WIDTH), glutGet(GLUT_INIT_WINDOW_HEIGHT));
 glMatrixMode (GL_PROJECTION);
 glLoadIdentity ();
 gluOrtho2D (-glutGet(GLUT_INIT_WINDOW_WIDTH)*zoomFactor, glutGet(GLUT_INIT_WINDOW_WIDTH)*zoomFactor,\
 -glutGet(GLUT_INIT_WINDOW_HEIGHT)*zoomFactor, glutGet(GLUT_INIT_WINDOW_HEIGHT)*zoomFactor);

glBegin(GL_LINES); 
glVertex2f ((0),(0)); 
glVertex2f ((100),(0));
 glEnd();
glBegin(GL_LINES); 
glVertex2f ((100),(0)); 
glVertex2f ((100),(100));
 glEnd();
glFlush();
}

int main(int argc,char** argv) {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowSize, windowSize);
glutInitWindowPosition(500,0);
glutCreateWindow("test");
glClearColor(0, 0.1,0.8,0.90);    
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 200, 200,0);   
glutDisplayFunc( display );
glutPassiveMotionFunc(motion);    
glutMouseFunc(mouse);
glutMainLoop(;   
return(0);
}

0 个答案:

没有答案