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 8343997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:14:04+00:00 2026-06-09T06:14:04+00:00

This code is from the red book, example 2-15 (well, the code is not

  • 0

This code is from the red book, example 2-15 (well, the code is not exactly the one in the book). Take care of the note I noted.

#include <fstream>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment(lib,"glew32.lib")
using namespace std;
#define BUFFER_OFFSET(offset) ((GLubyte *)NULL+offset)
#define XStart              -0.8
#define XEnd                0.8
#define YStart              -0.8
#define YEnd                0.8
#define NumXPoints          11
#define NumYPoints          11
#define NumPoints           (NumXPoints * NumYPoints)
#define NumPointsPerStrip   (2*NumXPoints)
#define NumStrips           (NumYPoints-1)
#define RestartIndex        0xffff

void display(void)
{
    int i,start;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glDrawElements(GL_TRIANGLE_STRIP,NumStrips*(NumPointsPerStrip+1),GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
    //glFlush();//it works,show a white square with black backgroud
    glutSwapBuffers();///it doesn't work,show what tha area looked like before

}

void init (void) 
{
    GLuint vbo,ebo;
    GLfloat *vertices;
    GLushort *indices;
    glewInit();
    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,2*NumPoints*sizeof(GLfloat),NULL,GL_STATIC_DRAW);
    vertices=(GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
    if(vertices==NULL)
    {
        fprintf(stderr,"Unable to map vertex buffer\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        int i,j;
        GLfloat dx=(XEnd-XStart)/(NumXPoints-1);
        GLfloat dy=(YEnd-YStart)/(NumYPoints-1);
        GLfloat *tmp=vertices;
        int n=0;
        for(j=0;j<NumYPoints;++j)
        {
            GLfloat y=YStart+j*dy;
            for(i=0;i<NumXPoints;++i)
            {
                GLfloat x=XStart + i*dx;
                *tmp++=x;
                *tmp++=y;
            }
        }
        glUnmapBuffer(GL_ARRAY_BUFFER);
        glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));
        glEnableClientState(GL_VERTEX_ARRAY);
    }
    glGenBuffers(1,&ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER,NumStrips*(NumPointsPerStrip+1)*sizeof(GLushort),NULL,GL_STATIC_DRAW);
    indices=(GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,GL_WRITE_ONLY);
    if(indices==NULL)
    {
        fprintf(stderr,"Unable to map index buffer\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        int i,j;
        GLushort *index=indices;
        for(j=0;j<NumStrips;++j)
        {
            GLushort bottomRow=j*NumYPoints;
            GLushort topRow=bottomRow+NumYPoints;
            for(i=0;i<NumXPoints;++i)
            {
                *index++=topRow+i;
                *index++=bottomRow+i;
            }
            *index++=RestartIndex;
        }
        glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);

    }
    glPrimitiveRestartIndex(RestartIndex);
    glEnable(GL_PRIMITIVE_RESTART);
}


void reshape (int w, int h)
{

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (-1,1,-1,1);
    glViewport (0,0,w,h);
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
      case 27:
          exit(0);
          break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (200, 200);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc (keyboard);
    glutMainLoop();
    return 0;  
}

as the http://www.opengl.org/resources/libraries/glut/spec3/node21.html says:
An implicit glFlush is done by glutSwapBuffers before it returns. Subsequent OpenGL commands can be issued immediately after calling glutSwapBuffers, but are not executed until the buffer exchange is completed.
If the layer in use is not double buffered, glutSwapBuffers has no effect.

How do I know whether the layer in use is double buffered?give an double buffered example.is this code I wrote is double buffered?

  • 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-09T06:14:05+00:00Added an answer on June 9, 2026 at 6:14 am

    In your main function, you call

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    

    That needs to be GLUT_DOUBLE. See the documentation for glutInitDisplayMode().

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

Sidebar

Related Questions

I am trying to compile this code example from the book Head First C
I picked up this code from a msdn blog : #include <windows.h> #include <stdio.h>
I copied this code from an example . I've read it 100 times. Array.prototype.map
I am trying to run demo code from Matplotlib: the legend_picking example . This
From this code: HTML <div class=test></div> CSS .test { background-color:red; font-size:20px; -custom-data1: value 1;
This is a code lifted straight from Perl Cookbook : @colors = qw(red blue
Borrowing this code from several different tutorials on how to use Google maps on
In this code (from the WCF REST starterkit - preview2): protected override SampleItem OnAddItem(SampleItem
I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
I am using this code from css tips and tricks to cover a background

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.