Getting started with OpenGL

Apprentice
Posts: 14
Joined: 2008.10
Post: #1
I just signed up for an Open GL course on Ed2Go.com.

The instructors posts a lot of examples, buut I can't get anything to compile. For example:

Code:
// First Try: a first OpenGL graphics program

#include <windows.h>  // include windows header file
#include <GL/glut.h>  // include OpenGL header file

void myInit();  // function prototype for my initialization function
void myDisplay();  // function prototype for my display function

void main(int argc, char* argv[])  // main program function
{
    glutInit(&argc, argv);  // initialize the GLUT toolkit
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
    glutInitWindowSize(640, 480);  // set window size
    glutInitWindowPosition(50, 100);  // set window position on the screen
    glutCreateWindow("First Try");  // title and open the window

    glutDisplayFunc(myDisplay);  // register my display function

    myInit();  // call my initialization function
    glutMainLoop();  // begin endless main loop
}

void myInit()  // initialize Open GL state
{
    glClearColor(1.0, 1.0, 1.0, 0.0);  // set background color to white
    glColor3f(0.0, 0.0, 0.0);  // set drawing color to black
    glPointSize(4.0);  // set dot size to 4 pixels
    glMatrixMode(GL_PROJECTION);        
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void myDisplay()  // draw my image
{
    glClear(GL_COLOR_BUFFER_BIT);  // clear the screen
    glBegin(GL_POINTS);  // draw four points
        glVertex2i(150, 150);
        glVertex2i(200, 200);
        glVertex2i(150, 200);
        glVertex2i(200, 150);
    glEnd();
    glColor3f(0.0, 1.0, 0.0);  // set drawing color to green
    glBegin(GL_POLYGON);  // draw triangle
        glVertex2f(10.0, 10.0);
        glVertex2f(100.0, 10.0);
        glVertex2f(10.0, 100.0);
    glEnd();
    glFlush();  // send output to display
}

I know I don't need the #include windows, but what would I need to do to just paste the rest of the code and have it run on my Mac?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
#include <GLUT/glut.h>

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
http://onesadcookie.com/Tutorials is a quick intro to setting up GLUT on Mac OS X. Aside from <GLUT/glut.h> vs. <GL/glut.h> it should mostly work just fine.

You should use GLUT_DOUBLE where they have GLUT_SINGLE and glutSwapBuffers() where they have glFlush() -- and they should, too.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #4
OneSadCookie Wrote:http://onesadcookie.com/Tutorials is a quick intro to setting up GLUT on Mac OS X. Aside from <GLUT/glut.h> vs. <GL/glut.h> it should mostly work just fine.

You should use GLUT_DOUBLE where they have GLUT_SINGLE and glutSwapBuffers() where they have glFlush() -- and they should, too.

Your tutorial is very nice. I followed the instructions, and aside from the difference in XCode versions, it works very well.

I was pretty much able to follow the same instructions for the homework file above, but here's a couple differences:
1) I have to name the file main.cpp - We're using C++ for this class
2) main has to be of return type int. Does anybody know why that is? I get an error if I try return type void for main.
Quote this message in a reply
Member
Posts: 168
Joined: 2004.10
Post: #5
You're still compiling with a C compiler, and C requires that main returns an int. You need to change the compiler to C++ iirc.

[EDIT] Oops, I didn't. Other way round. Misinformation. See Keith's post.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
You have it backward -- C doesn't care what main returns, C++ does.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #7
OneSadCookie Wrote:You have it backward -- C doesn't care what main returns, C++ does.

Why does
Code:
void main()
work on Visual Studio (c++ console project), but not on Xcode?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
'cos GCC (which Xcode uses) is much stricter about C++ than Visual Studio is.

Basically, if your code compiles with GCC, it will almost certainly compile with Visual Studio. If your code compiles with Visual Studio, that's no guarantee that it'll compile with GCC.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #9
Quote:work on Visual Studio (c++ console project), but not on Xcode?

Because as Keith says, Visual Studio is absolutely ridiculous about what it will accept. Other compilers are much better about adhering to the standards and will do something useful (like generate an error) when you do something incorrect.

I can't count the number of bugs that I've found in our cross-platform code by XCode (or Codewarrior back in the day) generating an error where MSVC++ happily compiled.

Wade
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Getting Started with OpenGL - Mac ThrottleMonkey 35 11,373 Mar 18, 2003 03:07 PM
Last Post: xDexx