Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:57:03+00:00 2026-06-14T11:57:03+00:00

I am trying to draw a simple quad with OpenGL.The same code wouldn’t fail

  • 0

I am trying to draw a simple quad with OpenGL.The same code wouldn’t fail if I weren’t using the depth and draw the quad with 0.0 as z coordinate.
It seems like the depth isn’t enabled or whatsoever:

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>

int width=500, height=500, depth=500;


void init()
{
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, width, height, 0);
    gluLookAt(250, 250, 250, 250, 250, -250, 0, 1, 0);
    gluPerspective(45, 1, 1.0, 200.0);
}


void display()
{
    glClearColor(0.0, 0.0, 0.0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor4f(1, 0, 0, 0);
    glBegin(GL_QUADS);
    glVertex3i(100, 100, 100);
    glVertex3i(100, 300, 100);
    glVertex3i(300, 300, 100);
    glVertex3i(300, 100, 100);
    glEnd();

    glutSwapBuffers();
}


int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    init();
    glutMainLoop();
    return 0;
}

I just see a black window, but I should see a red quad inside it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:57:05+00:00Added an answer on June 14, 2026 at 11:57 am

    Change your init() function to:

    void init()
    {
        glEnable(GL_DEPTH_TEST);
        glViewport(0, width, height, 0);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45, 1, 1.0, 200.0);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(250, 250, 250, 250, 250, -250, 0, 1, 0);
    }
    

    Your problem is that gluLookAt() should be used to define the model-view matrix, but you are multiplying the projection one. Also the glViewport() call is unrelated to the matrix calculations, so I moved it to the beggining of the function. Not really needed, but I find that clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw a textured quad in XNA using the sample code from
I'm trying to draw a simple circle using OpenGL ES. The problem is that
I am trying to draw a simple 256x256 pixel RGBA square using python's png
I am trying to draw circular maze in openGl.For that I created a simple
I'm trying to show textures by openGL. In following code I want to draw
I have a simple OpenGL program and trying to draw an instanced array that
I am having a hard time trying to draw simple lines through an OpenGL
I am just trying to draw a simple pie chart using flotr(an open source
I'm using Sparklines and am trying to draw a simple blox plot with predetermined
I'm trying to develop a simple 2D fighting game using OpenGL, and I'm stuck

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.