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

  • SEARCH
  • Home
  • 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 8939333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:48:10+00:00 2026-06-15T10:48:10+00:00

I am trying to draw a 50×50 grid in an OpenGL window. My code

  • 0

I am trying to draw a 50×50 grid in an OpenGL window. My code for drawing the grid is

void GLGrid::draw() {

int y=-width;
int yIncrement = width / 50;
int x=-length;
int xIncrement = length / 50;


glColor3f(0.0f,0.0f,0.0f);
for(y = -width; y < width; y+=yIncrement) {
    glBegin(GL_LINES);
        glVertex3f(-width,y,0);
        glVertex3f(width,y,0);
    glEnd();
}

for(x = -length; x < length; x+=xIncrement) {
    glBegin(GL_LINES);
        glVertex3f(-length,x,0);
        glVertex3f(length,x,0);
    glEnd();
}
}

Note that before I was doing x=0;x < length etc but that was making the line (the only one I see) start in the middle of the screen, rather than the very left. Also, when I draw a rectangle over the whole window, I have to start that at negative 300×300, rather than 0,0.

The only thing I see is one horizontal line across the middle of the screen. I think the problem is I don’t know what my window size actually is. Whenever I print out wherever I click with

static void mouseEvent(int button, int state, int x, int y) {
cout<<"\nMouse Event!";
cout<<"\n\tbutton:"<<button;
cout<<"\n\tstate:"<<state;
cout<<"\n\tx:"<<x;
cout<<"\n\ty:"<<y<<"\n";
}

It prints out that the upper left corner is 0,0 and the bottom right corner is 300,300. So I set my GLGrid length and width to be 300 each. Should I be setting the window length and width to something else? And if so, what? I am very new to OpenGL so please forgive my ignorance. To be thorough and because I don’t know if something else subtle may be the issue, I will include more code

static void initOpenGL() {
//set clear color to white
glClearColor(0.0f,0.0f,0.0f,1.0f);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}


/*OpenGL calls*/
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

if (!init)
{
    initOpenGL();
}

renderScene();

//...more code below in this function but pretty positive its irrelevant



static void renderScene() {

    drawBackground();

    drawGrid();

}


static void drawBackground() {
//draw a white rectangle for background
    glColor3f(1.0f,1.0f,1.0f);
    glBegin(GL_QUADS);
        glVertex3f(-windowMaxX, -windowMaxY, 0);
        glVertex3f(windowMaxX, -windowMaxY, 0);
        glVertex3f(windowMaxX, windowMaxY, 0);
        glVertex3f(-windowMaxX, windowMaxY, 0);
    glEnd();
}


static void drawGrid() {
    GLGrid.draw();
}
  • 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-15T10:48:11+00:00Added an answer on June 15, 2026 at 10:48 am

    When you are drawing your vertical lines you need to change your x value, not your y value:

    glBegin(GL_LINES);
        glVertex3f(x,-length,0);
        glVertex3f(x,length,0);
    glEnd();
    

    There’s probably more wrong, but this is one thing to change.

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

Sidebar

Related Questions

When trying to draw on an OpenGL widget with QT, the window display ends
I'm trying to draw an open rectangle using a Polygon: int[] xPoints = {1,1,3,3};
I am trying to draw a picture to the window but it is only
When trying to draw the following quads in OpenGL using a vertex array (instead
Im trying to draw a globe in Android, I use OpenGL for this. However
I'm trying to draw image using UIImage 's drawInRect: method. Here is the code:
I'm trying to draw some text onto an MapView on Android. The drawing of
I'm trying to draw custom ColumnHeaders in a FastObjectListView control. So far the code
Im trying to draw a line. I wrote a code like below. UIColor *currentColor
I'm trying to draw a simple texture in opengl. I made a simple class

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.