GLUT 滚轮缩放
GLUT zooming in and out by wheel
我已经浏览了很多资源并尝试了很多变体,但缩放仍然不起作用。我无法将 glprojection 更改为 gluPerspective,因为在这种情况下,我的程序不会绘制任何东西。这是大概的代码。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "glut.h"
#define windowSize 900.0
double zoomFactor = 1;
void reshape (int w, int h)
{
glViewport (0.0, 0.0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-(GLdouble) w * zoomFactor, (GLdouble) w* zoomFactor, -(GLdouble) h* zoomFactor, (GLdouble) h* zoomFactor);
}
void mouse(int button, int state, int x, int y)
{
if (button == 3 || button == 4 )
{
if (state == GLUT_UP) zoomFactor += 0.05;
else zoomFactor -= 0.05;
}
else return;
glutReshapeFunc(reshape);
}
Void display(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f ((0),(0));
glVertex2f ((100),(0));
glEnd();
glBegin(GL_LINES);
glVertex2f ((100),(0));
glVertex2f ((100),(100));
glEnd();
glBegin(GL_LINES);
glVertex2f ((0),(0));
glVertex2f ((100),(1000));
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 );
glutMouseFunc(mouse);
glutMainLoop(;
return(0);
}
glutReshapeFunc
仅在 window 大小发生变化时告诉 GLUT 要调用哪个函数。调用它来响应输入事件几乎没有意义,因为它不会调用重塑函数。
无论如何,您的问题是,您在重塑函数中设置了视口和投影。致所有教程作者:停止这样做,这只会让新手养成坏习惯。
现在请和我说话:"Everything drawing related goes into the display function. The viewport is a drawing state and in every OpenGL program just a little bit more complex than 'Hello Triangle' the viewport is going to be changed several times drawing a single frame – for example when using framebuffer objects. I will thereby vow to never place calls to glViewport
or projection matrix setup in the window reshape handler. I also vow to slap everyone in the face who writes code like this in my presence."
将重塑函数中的全部内容移动到 display
(设置全局变量,或使用 glutGet(GLUT_WINDOW_{WIDTH,HEIGHT})
在显示函数中获取 window 维度)并调用 glutPostRedisplay
在你的鼠标滚轮函数中触发重绘。
我已经浏览了很多资源并尝试了很多变体,但缩放仍然不起作用。我无法将 glprojection 更改为 gluPerspective,因为在这种情况下,我的程序不会绘制任何东西。这是大概的代码。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "glut.h"
#define windowSize 900.0
double zoomFactor = 1;
void reshape (int w, int h)
{
glViewport (0.0, 0.0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-(GLdouble) w * zoomFactor, (GLdouble) w* zoomFactor, -(GLdouble) h* zoomFactor, (GLdouble) h* zoomFactor);
}
void mouse(int button, int state, int x, int y)
{
if (button == 3 || button == 4 )
{
if (state == GLUT_UP) zoomFactor += 0.05;
else zoomFactor -= 0.05;
}
else return;
glutReshapeFunc(reshape);
}
Void display(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f ((0),(0));
glVertex2f ((100),(0));
glEnd();
glBegin(GL_LINES);
glVertex2f ((100),(0));
glVertex2f ((100),(100));
glEnd();
glBegin(GL_LINES);
glVertex2f ((0),(0));
glVertex2f ((100),(1000));
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 );
glutMouseFunc(mouse);
glutMainLoop(;
return(0);
}
glutReshapeFunc
仅在 window 大小发生变化时告诉 GLUT 要调用哪个函数。调用它来响应输入事件几乎没有意义,因为它不会调用重塑函数。
无论如何,您的问题是,您在重塑函数中设置了视口和投影。致所有教程作者:停止这样做,这只会让新手养成坏习惯。
现在请和我说话:"Everything drawing related goes into the display function. The viewport is a drawing state and in every OpenGL program just a little bit more complex than 'Hello Triangle' the viewport is going to be changed several times drawing a single frame – for example when using framebuffer objects. I will thereby vow to never place calls to glViewport
or projection matrix setup in the window reshape handler. I also vow to slap everyone in the face who writes code like this in my presence."
将重塑函数中的全部内容移动到 display
(设置全局变量,或使用 glutGet(GLUT_WINDOW_{WIDTH,HEIGHT})
在显示函数中获取 window 维度)并调用 glutPostRedisplay
在你的鼠标滚轮函数中触发重绘。