我读了这个tutorial我正确地执行了它。
但是,我想应用一些更改。第一个变化是看到该圆的不同 View ,例如仅显示圆的 1/4。我知道这是由 glViewPort(parameters) 完成的。但是,更改参数后,没有任何反应。我已经检查了很多论坛来弄清楚为什么会出现这个问题,但我什么也想不通。
有人可以向我解释更多这方面的信息以及如何使用它吗? (了解问题出在哪里)
这是代码(最初写在 tutorial 上)
代码:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
gluOrtho2D(0.0,200.0,0.0,200.0);
//glViewport(0, 0, 250, 250);
}
void setPixel(GLint x,GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void Circle(){
int xCenter=100,yCenter=100,r=50;
int x=0,y=r;
int p = 3/2 - r;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f( 1 ,0, 0);
while(x<=y){
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter+y,yCenter+x);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter+y,yCenter-x);
setPixel(xCenter-x,yCenter-y);
setPixel(xCenter-y,yCenter-x);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-y,yCenter+x);
if (p<0)
p += (2*x)+3;
else {
p += (2*(x-y))+5;
y -= 1;
}
x++;
}
glFlush();
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
glutCreateWindow("My Circl2e");
init();
glViewport(0,0,250,250);
//glLoadIdentity();
glutDisplayFunc(Circle);
glutMainLoop();
return 0;
}
p.s:在某些示例中,我看到他们在使用 glViewPort(parameters) 之前使用 setWindow(parameters)。但是 setWindow() 需要 ubuntu 不可用的库。
最佳答案
默认的 GLUT reshape 函数调用 glViewport()
与窗口大小。来自documentation :
If a reshape callback is not registered for a window or NULL is passed to glutReshapeFunc (to deregister a previously registered callback), the default reshape callback is used. This default callback will simply call glViewport(0,0,width,height) on the normal plane (and on the overlay if one exists).
既然你调用glViewport()
这么早,窗口将在您发出调用后成形,覆盖您指定的视口(viewport)。
您要么需要注册自己的 reshape 函数,然后调用 glViewport()
在那里输入您想要的视口(viewport)参数,或调用 glViewport()
在你的 Circle()
的开头功能。
关于c++ - 在 OpenGL 中使用 glviewport(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42584656/