您的位置:首页 > 运维架构

OpenGL显示窗口重定形函数

2016-06-06 13:48 260 查看
我们在建立初始显示窗口后,需要在其后改变位置与形状。窗口尺寸的改变可能改变其纵横比并引起对象形状的改变。所以GLUT库提供glutReshapeFunc(winReshapeFcn)函数。和其他GLUT函数一起放在程序的主过程中,不过该函数是在窗口尺寸输入后激活。其参数是接受新窗口高度的过程名。还可以接着使用新尺寸去重新设置投影参数并完成任何其他操作,包括改变显示窗口颜色。另外,可以保存宽度和高度供其它子程序使用。

void glutReshapeFunc(void (*func)(int width, int height));


官方文档在这里

#include <GL/glut.h>
#include <cmath>
#include <cstdlib>

const double TWO_PI = 6.2831853;

GLsizei winWidth = 400, winHeight = 400;
GLuint regHex;

class screenPt {
private:
GLint x, y;

public:
screenPt() {
x = y = 0;
}
void setCoords(GLint xCoord, GLint yCoord) {
x = xCoord;
y = yCoord;
}
GLint getx() const {
return x;
}
GLint gety() const {
return y;
}
};

static void init(void)
{
screenPt hexVertex, circCtr;
GLdouble theta;
GLint k;

circCtr.setCoords(winWidth/2, winHeight/2);

glClearColor(1.0, 1.0, 1.0, 0.0);

regHex = glGenLists(1);
glNewList(regHex, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
for (k = 0; k < 6; ++k) {
theta = TWO_PI * k / 6.0;
hexVertex.setCoords(circCtr.getx() + 150 * cos(theta),
circCtr.gety() + 150 * sin(theta));
glVertex2i(hexVertex.getx(), hexVertex.gety());
}
glEnd();
glEndList();
}

void regHexagon()
{
glClear(GL_COLOR_BUFFER_BIT);

glCallList(regHex);

glFlush();
}

void winReshapeFcn(int newWidth, int newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)newWidth, 0.0, (GLdouble)newHeight);

glClear(GL_COLOR_BUFFER_BIT);
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);

glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Reshape-Function & Display-List Example");

init();
glutDisplayFunc(regHexagon);
glutReshapeFunc(winReshapeFcn);

glutMainLoop();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: