1
#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

user469652
  • 48,855
  • 59
  • 128
  • 165

4 Answers4

1

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.

Community
  • 1
  • 1
Joshua Smith
  • 6,561
  • 1
  • 30
  • 28
  • Thanks can you use replace glFrustum(0, 1, 0, 1, 0, 1) this line, instead, use gluPerspective to produce the same result? I'm confused ~ – user469652 May 20 '11 at 12:58
  • The center of the window is anything you want in OpenGL, not a particular coordinate. – datenwolf May 20 '11 at 14:46
0
glTranslatef(-0.5f, -0.5f, 0.0f);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Yes, this works. Thanks for your answer. By the way, I tried to use glFrustum(0, 2, 0, 2, 0, 1); Why do I see the same result? shouldn't the triangle appears smaller, as the windows included more 'space'? – user469652 May 20 '11 at 12:59
0

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.

Mark
  • 6,108
  • 3
  • 34
  • 49
0

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.

Bahbar
  • 17,760
  • 43
  • 62