Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

HELP!!! Basic glOrtho problem!

DaMarvDaMarv Member Posts: 3
Hey,

can anyone explain to me why this code doesn't generate a visible red line from the left bottom to the top right corner of my window?



void initGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
glMatrixMode(GL_MODELVIEW);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glEnd();
glFlush();
}

int main(int argc, char** argv) // Create main function for bringing it all together
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB); // Display mode
glutInitWindowSize(500, 500);
glutCreateWindow("Aufgabe ?");
initGL();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key); // callback for key input
// glutIdleFunc(display); // If continuous animation is required
glutMainLoop(); // Initialize the main loop

}



As you can see, I have specified the viewable volume as a cube of length one, one corner at (0, 0, 0), the other one at (1, 1, 1). I'm drawing the diagonal line through these two corners, so what I should expect on the 2D window surface is a diagonal line across the window. Why isn't this happening? My window remains BLACK.

Please help. This is such a simple thing, and I'm stuck at this already.

Cheers
Marvin

Comments

  • InnoventorInnoventor Member Posts: 1
    : Hey,
    :
    : can anyone explain to me why this code doesn't generate a visible
    : red line from the left bottom to the top right corner of my window?
    :
    :
    :
    : void initGL()
    : {
    : glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
    : glMatrixMode(GL_PROJECTION);
    : glLoadIdentity();
    : glOrtho(0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
    : glMatrixMode(GL_MODELVIEW);
    : }
    :
    : void display()
    : {
    : glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen
    : and depth buffer
    : glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    : glBegin(GL_LINES);
    : glVertex3f(0.0f, 0.0f, 0.0f);
    : glVertex3f(1.0f, 1.0f, 1.0f);
    : glEnd();
    : glFlush();
    : }
    :
    : int main(int argc, char** argv) // Create main function for bringing
    : it all together
    : {
    :
    : glutInit(&argc, argv);
    : glutInitDisplayMode(GLUT_RGB); // Display mode
    : glutInitWindowSize(500, 500);
    : glutCreateWindow("Aufgabe ?");
    : initGL();
    : glutDisplayFunc(display);
    : glutReshapeFunc(reshape);
    : glutKeyboardFunc(key); // callback for key input
    : // glutIdleFunc(display); // If continuous animation is required
    : glutMainLoop(); // Initialize the main loop
    :
    : }
    :
    :
    :
    : As you can see, I have specified the viewable volume as a cube of
    : length one, one corner at (0, 0, 0), the other one at (1, 1, 1). I'm
    : drawing the diagonal line through these two corners, so what I
    : should expect on the 2D window surface is a diagonal line across the
    : window. Why isn't this happening? My window remains BLACK.
    :
    : Please help. This is such a simple thing, and I'm stuck at this
    : already.
    :
    : Cheers
    : Marvin
    :


    Hmmm, I generally don't use most of the glut routines, but I am familiar with OpenGL functions, I might be able to help. Here are some suggestions to try:

    If you just want to draw a simple line, make sure these features are turned off:

    glDisable (GL_BLEND) ; (for blending and transparency)
    glDisable (GL_DEPTH_TEST) ; (for preventing farther objects from covering near objects)

    Either could prevent the line from being drawn if not used properly. You can insert them in initGL ()

    You can also insert glLoadIdentity () after glMatrixMode(GL_MODELVIEW);

    If those don't help, try using glOrtho with these settings:

    glOrtho(0.f, 1.f, 0.f, 1.f, 0.1f, 1.f);
    OR
    glOrtho (-0.5f, 0.5f, -0.5f, 0.5f, 0.1f, 5.f); (I generally use glOrtho with settings kinda like this)

    If you still can't see anything, try inserting glViewport () at the begining of initGL () with these settings:

    glViewport (0, 0, 500, 500) ;

    For further troubleshooting, try moving the points toward the center of the view:
    glVertex3f(0.25f, 0.25f, 0.5f);
    glVertex3f(0.75f, 0.75f, 0.5f);

    You can also see if any of the function calls to OpenGL caused errors with glGetError ():

    [code]
    //this implementation is if you're running an Win32 application

    static int prevent_loop = 0 ;

    if (glGetError()) != GL_NO_ERROR) {
    if (!prevent_loop) {
    MessageBox (hwnd, "OpenGL error detected.", "Notification", MB_OK) ;
    prevent_loop = 1 ;
    }
    }[/code]

    I think glut also has an error detection function. There are also ways to get actual error messages from the codes glGetError returns.

    Well, that's about all I can think of! Good Luck!

    -Innoventor
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    Have you tried not using GLUT? I had problems when I started coding GL due to GLUT, so I just use the plain OpenGL API now and it makes debugging easier for me. GLUT does make things easier because you don't have to know as much about the GL API, but generally speaking when things get easier, you lose some control or power. The only other ideas I had were stated in the other reply to your post.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.