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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:50:14+00:00 2026-05-28T18:50:14+00:00

I am in a class in which we will be learning OpenGL. The professor

  • 0

I am in a class in which we will be learning OpenGL. The professor is using Visual Studio, but that isn’t working too well with my install of Parallels, so I just decided to use XCode (which I prefer anyway). I have got basic example code working, but I am having issues running the example that the professor gave us. Here it is:

#include <glut.h>           //must be included for OpenGL
#include <gl\gl.h>              //must be included for OpenGL

#include <time.h>           //must be included for time functions
#include <iostream>         //must be included for console input/output
using namespace std;

#define WINDOW_WID 800
#define WINDOW_HEI 600

int randomPx, randomPy;
/////////////////////////////////////////

void myInit(void) 
{
    randomPx = 400;
    randomPy = 300;
    glClearColor (1.0, 1.0, 1.0, 0.0);   // set background color to black
    glShadeModel (GL_FLAT);
}



void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);        // clear the screen    

    glColor3f(1,0,0);                   //set the drawing color
    glPointSize(10);                    //set the point size
    glBegin(GL_POINTS);
        glVertex2d(randomPx,randomPy);          //Set the position of the vertex
    glEnd();

    glutSwapBuffers ();                 //put everything on your screen
}


void myReshape ( int w, int h)
{
    glViewport (0, 0, w, h);
    glMatrixMode (GL_PROJECTION);           // set "camera type"
    glLoadIdentity ();                      // clear the matrix

    glOrtho(0.0, w, 0.0, h, -1.0, 1.0);     // viewing transformation 
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
}

void Animation()
{
    srand(time(NULL)+rand());               //set the seed for your rand function
    randomPx = rand()%WINDOW_WID;
    randomPy = rand()%WINDOW_HEI;
    Sleep(200);                             //put the program to sleep for 200 ms

    myDisplay();
}

void myMouse(int button, int state, int x, int y)
{    
    y = WINDOW_HEI - y;
    switch (button) 
    {
        case GLUT_LEFT_BUTTON:              //when left mouse button is clicked
            if (state == GLUT_DOWN)         
            {
                cout << "When mouse is up, animation starts\n";
            }
            else if(state == GLUT_UP)
            {
                glutIdleFunc(Animation);    //do Animation when idle
            }
            break;
        case GLUT_RIGHT_BUTTON:             //when right mouse button is clicked
            if (state == GLUT_DOWN)
            {
                glutIdleFunc(NULL);         //do nothing when idle
            }
            break;
        case GLUT_MIDDLE_BUTTON:
            if (state == GLUT_DOWN)
            {
                exit (-1);                  //exit your program
            }
            break;
    }
    myDisplay();
} 

void myMotion(int x, int y)
{   
    y = WINDOW_HEI - y;


    myDisplay();
}

void myKeyboard(unsigned char key, int x, int y)
{
    //TODO 

    myDisplay();
}

/*
 * Request double buffer display mode.
 * Register mouse input callback functions
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);                            // initialize the toolkit
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);     // set display mode
    // glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);     // set display mode
    glutInitWindowSize (WINDOW_WID, WINDOW_HEI);                    // set screen window size
    glutInitWindowPosition (100, 100);       // set window position on screen
    glutCreateWindow (argv[0]);              // open the screen window
    myInit ();
    glutDisplayFunc(myDisplay);              // register redraw function
    glutReshapeFunc(myReshape);              // register reshape function

    glutMouseFunc(myMouse); //GLUT provides a way for you to register the function that will be responsable for processing events generated by mouse clicks.
    glutMotionFunc(myMotion); //There are two types of motion that GLUT handles: active and passive motion. Active motion occurs when the mouse is moved and a button is pressed. 
    glutKeyboardFunc(myKeyboard);
    //glutPassiveMotionFunc(myPassiveMotion); //Passive motion is when the mouse is moving but no buttons are pressed. If an application is tracking motion, an event will be generated per frame during the period that the mouse is moving.
    //glutEntryFunc(processMouseEntry); //GLUT is also able to detect when the mouse leaves or enters the window region. A callback function can be registered to handle these two events.

    glutMainLoop();                          // go into a perpetual loop
    return 0;
}

I took out the imports statements, and replaced them with the imports for example XCode OpenGL code:

#include <iostream>
#include <GLUT/glut.h>
#include <time.h>

And I don’t get any error messages, but when I run the application, and click on the window, it does not start the animation it is supposed to. (I know it works, because in Visual Studio on every other computer in the class it worked fine.)

It does register the click on the screen by printing out: “When mouse is up, animation starts”

But after that, it just gives me the Spinning Beachball of Death until I stop it from running via XCode. So is there something else I have to adjust to get this working?

  • 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-05-28T18:50:15+00:00Added an answer on May 28, 2026 at 6:50 pm

    First of all, why are you using a backslash for your import gl.h? Secondly, you should link your code to the GLUT and OpenGL library, so you should include/import them as OpenGL/gl.h and GLUT/glut.h. But your main problem here is the sleep function. It doesn’t exist on macs and thus, your code crashes when it tries to animate. Use [NSThread sleepForTimeInterval:0.2] instead if you want to wait for 200 ms. But you will have to change your file extension to .mm in order to use objective-c code and also import the Foundation library to use the NSThread class.

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

Sidebar

Related Questions

I'm currently working on an oophp application. I have a site class which will
I want to create a class that initializes a timer which will be used
I have a static timer class which will be called by ANY webpage to
I have created a class for a dashboard item which will hold information such
I need a base class for my DTO classes which will be used in
Hi I need to extend the CListControl class in C++/MFC, which will add several
Which virtual table will be pure virtual function located? In the base class or
Guys I'm working on class called LINT (large int) for learning purposes, and everything
What i'm trying to do is create a template array class that will store
Since I have started learning OpenGL, I thought I would as well write a

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.