开发者

Opengl: how to make this triangle centred on the window

#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, GLin开发者_如何学编程t 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:

Opengl: how to make this triangle centred on the window


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.


glTranslatef(-0.5f, -0.5f, 0.0f);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜