Opengl:如何使这个三角形以窗口为中心

发布于 2024-11-08 12:02:04 字数 1663 浏览 0 评论 0原文

#include <GL/glut.h>

GLint winWidth = 600, winHeight = 600;

GLfloat x0 = 100.0, y0 = 100.0, z0 = 50.0;
GLfloat xref = 50, yref = 50.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0,  Vz = 0.0;
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
GLfloat dnear = 25.0, dfar = 125.0;

void init (void)
{   
    glClearColor (1.0, 1.0, 1.0, 0.0);

    //glMatrixMode(GL_MODELVIEW);
    //gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho(0,1,0,1, 0,0.1);
    //gluOrtho2D(0, 1,0,1);
    //gluPerspective(45, 1.2, 1, 10);
    glFrustum(0, 1, 0, 1, 0, 1);
    //gluPerspective(45.0, 1, 1, 15);
}

void displayFcn (void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(0.0, 1.0, 0.0);
    //glPolygonMode(GL_FRONT, GL_FILL);
    //glPolygonMode(GL_BACK, GL_FILL);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(0.5, 1.0, 0.0);
    glEnd();

    glFlush();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0,0,newWidth, newHeight);

    winWidth = newWidth;
    winHeight = newHeight;

}
void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

这是完整的源代码,您可以复制并粘贴到您的 VS 解决方案中并编译。 你需要安装 glut。

结果是这样的: 在此处输入图像描述

#include <GL/glut.h>

GLint winWidth = 600, winHeight = 600;

GLfloat x0 = 100.0, y0 = 100.0, z0 = 50.0;
GLfloat xref = 50, yref = 50.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0,  Vz = 0.0;
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
GLfloat dnear = 25.0, dfar = 125.0;

void init (void)
{   
    glClearColor (1.0, 1.0, 1.0, 0.0);

    //glMatrixMode(GL_MODELVIEW);
    //gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho(0,1,0,1, 0,0.1);
    //gluOrtho2D(0, 1,0,1);
    //gluPerspective(45, 1.2, 1, 10);
    glFrustum(0, 1, 0, 1, 0, 1);
    //gluPerspective(45.0, 1, 1, 15);
}

void displayFcn (void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(0.0, 1.0, 0.0);
    //glPolygonMode(GL_FRONT, GL_FILL);
    //glPolygonMode(GL_BACK, GL_FILL);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(0.5, 1.0, 0.0);
    glEnd();

    glFlush();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0,0,newWidth, newHeight);

    winWidth = newWidth;
    winHeight = newHeight;

}
void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

This is the full source code, you can copy and paste to your VS solution and compile.
You'll need to have glut installed.

The result comes up like this:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这样的小城市 2024-11-15 12:02:04

opengl中窗口的中心是0,0。所以,当你计算顶点时,你必须计算它们使得三角形的中心是0,0。

glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(0.5, 1.0, 0.0);
glEnd();

这些坐标需要更新,您可以在这个问题上找到有关查找三角形中心的讨论:寻找二维三角形的中心这听起来像是来自类似的家庭作业。

The center of the window is 0,0 in opengl. So, when you calculate the vertices, you have to calculate them such that the center of the triangle is 0,0.

glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(0.5, 1.0, 0.0);
glEnd();

those coords will need to be updated, you can find a discussion on finding the centers of triangles at this question: finding center of 2D triangle which sounds like it's from a similar homework assignment.

屋檐 2024-11-15 12:02:04
glTranslatef(-0.5f, -0.5f, 0.0f);
glTranslatef(-0.5f, -0.5f, 0.0f);
陈年往事 2024-11-15 12:02:04

OpenGL 的默认视角是以原点为中心,窗口 x 和 y 的范围为 -1 到 1。您可以通过更改默认查看体积或更改三角形的坐标来更改此设置。

要么

glTranslatef(-.5f, -.5f, .0f);

或者

glBegin(GL_TRIANGLES);
glVertex3f(-.5, -.5, 0.0);
glVertex3f(.50, -.5, 0.0);
glVertex3f(0, .5, 0.0);
glEnd();

都会起作用。

The default perspective for OpenGL is the origin centered and the window x and y ranging from -1 to 1. You can either change this by changing the default viewing volume or changing the coordinates of your triangle.

Either

glTranslatef(-.5f, -.5f, .0f);

or

glBegin(GL_TRIANGLES);
glVertex3f(-.5, -.5, 0.0);
glVertex3f(.50, -.5, 0.0);
glVertex3f(0, .5, 0.0);
glEnd();

will work.

三生路 2024-11-15 12:02:04

看起来您正在尝试使用 glFrustum ,而您想要使用 glOrtho

glFrustum 应该用于生成透视矩阵。 zNear 永远不会 0。您现在调用它的方式会生成错误GL_INVALID_VALUE,因此您将获得默认的投影矩阵,即恒等式。

It looks like you're trying to use glFrustum where you want to use glOrtho

glFrustum is supposed to be used for generating a perspective matrix. zNear is never 0. The way you call it right now generates an error GL_INVALID_VALUE, so you get the default projection matrix, the identity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文