Open GL and C++

Catalyst
Unregistered
 
Post: #1
I wish to begin programming using C++ and Open GL under OS X, however I have had serious problems finding a guide to get started.

I have looked at the Open GL section of Apple's carbon programming guide, however, strangly enough, it switches into Cocoa/Obj-C when it gives its tutorial, rendering the whole thing useless. I have also read NeHe's guide, however its OS X guide is for Cocoa, and I cannot coax its OS 9 form into compiling(It gives me errors for attempting to link to Obj-C libraries).

I understand that Cocoa is an API for Obj-C and Java only, however I have also found mentions of "mixed" source, combining C++ and Obj-C. Is it possible to effectivly write C++ code without any Obj-C code, and thus use the Cocoa API? I did not think this was possibil, hence I attempted to find a guide to Carbon/Open GL programming, however I have been unable to discover anything.

I should note that switching to Obj-C is not really a solution, as I want the majority of my code to be portable, and because I do not have the time to learn a new language at the moment.
Quote this message in a reply
Patrick
Unregistered
 
Post: #2
Try using SDL's OpenGL stuff ( http://www.libsdl.org/ ). The NeHe tutorials have code examples for it and it is widely portable to many OSes ( Win, Lin, Mac etc ). The SDL on X code bootstraps on Obj-C but after that you can use C++ without ever having to write Obj-C code yourself. Cool

edit: oops, libsdl.org not sdl.org
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #3
Or you could use GLUT, if all you want is OpenGL itself (and are willing to live with a simplified window manager and input system).
Quote this message in a reply
Catalyst
Unregistered
 
Post: #4
Quote:Originally posted by Patrick
Try using SDL's OpenGL stuff ( http://www.libsdl.org/ ). The NeHe tutorials have code examples for it and it is widely portable to many OSes ( Win, Lin, Mac etc ). The SDL on X code bootstraps on Obj-C but after that you can use C++ without ever having to write Obj-C code yourself. Cool

edit: oops, libsdl.org not sdl.org

I wish to learn pure Open GL(Not the SDL), so that is not of any use to me, unfortunatly. As for NeHe's tutorials, I have already explained that I cannot get them to work, as they give me errors(now, I get errors from unresolved variables), and I am still confused by his insertion of C code into a Cocoa project.

Quote:you could use GLUT, if all you want is OpenGL itself (and are willing to live with a simplified window manager and input system).
Do you know of any tutorials/guides for GLUT/Carbon, as I want to start from a good base, from where I can learn from other's source code and reference docs.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
There's no such thing as "pure" OpenGL, you have to have an interface to the OS in there somewhere. On the Mac, that's AGL for Carbon, NSGL for Cocoa, or CGL for either (but only fullscreen). On Windows it's WGL, on Linux it's glX.

SDL and GLUT both take the place of these layers, but they do it in a platform-independent manner. Both have C-only APIs.

Objective C is a superset of C, you can put as much C code as you want into a Cocoa project. It's also much easier to do most things in Cocoa than it is in (say) Carbon, so don't discard it just because it doesn't look familiar.

Most OpenGL sample code on the web assumes Windows, SDL or GLUT. Take your pick.
Quote this message in a reply
Catalyst
Unregistered
 
Post: #6
Quote:Originally posted by OneSadCookie
There's no such thing as "pure" OpenGL, you have to have an interface to the OS in there somewhere. On the Mac, that's AGL for Carbon, NSGL for Cocoa, or CGL for either (but only fullscreen). On Windows it's WGL, on Linux it's glX.

SDL and GLUT both take the place of these layers, but they do it in a platform-independent manner. Both have C-only APIs.

Objective C is a superset of C, you can put as much C code as you want into a Cocoa project. It's also much easier to do most things in Cocoa than it is in (say) Carbon, so don't discard it just because it doesn't look familiar.

Most OpenGL sample code on the web assumes Windows, SDL or GLUT. Take your pick.

Thank you for clearing a fair bit up, however, I still have a few more questions.

I read in this sites faq of mix Objective C, and C++(using the .mm file(I know this is just a convention)), so is all C++ legal Objective-C under using this method? Also, I have consistently read that Cocoa is accessibly to Objective C and Java exclusivily, and that C and C++ programmers must use Carbon. Can you clear up these inconsistencies? Hence, the question, If I wrote a complete C++ programming, using Cocoa API calls(and open GL ones), would it compile if I claimed it was a "mixed" source file?

Thanks for the help so far.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
You can compile C++ code and Objective C code in the same file if the file is named .mm. It's not a true blend of the two languages (C++ classes and ObjC classes are completely different still, for example), but yes, it can be done. It takes about 3x as long to compile as C++, though, which already takes about 3x as long to compile as C/ObjC...

Cocoa is only accessible to Objective C and Java (and AppleScript, Perl, Python, ...). It could never be fully accessible to C++, but using .mm files (aka ObjC++) and a bit of ObjC syntax you can call Cocoa successfully from C++.

At this stage, you shouldn't be worrying about all this Smile Just grab the GLUT or SDL ports of NeHe's tutorials and jump in with C. Start a new thread if you have problems compiling the tutorials.
Quote this message in a reply
xDexx
Unregistered
 
Post: #8
did you try neheX? they have carbon tutorials for opengl. nehex.idevgames.com
Quote this message in a reply
Catalyst
Unregistered
 
Post: #9
Quote:Originally posted by xDexx
did you try neheX? they have carbon tutorials for opengl. nehex.idevgames.com


Thank you so much. I saw NeheX mentioned on here before, however I assumed it was at gamedev.net (hence I got a dead link). Those tutorials are exactly what I wanted.

Edit: Upon closer inspection, NeheX is rather incomplete, as the source files are not augemented with any documenation about the API-Specific calls, I guess I shall just wade though, and attempt to learn more by breaking the code, unless there is documentation I cannot find(I have checked both NeheX and SealFin's sites).
Quote this message in a reply
Geppy
Unregistered
 
Post: #10
Try this:

http://homepage.mac.com/jonathanholley/....mplate.sit

After it decompresses stick it in /Developer/ProjectBuilder Extras/Project Templates/Application

Many thanks to Carl Norum for giving this to me!
Quote this message in a reply
Catalyst
Unregistered
 
Post: #11
Quote:Originally posted by Geppy
Try this:

http://homepage.mac.com/jonathanholley/....mplate.sit

After it decompresses stick it in /Developer/ProjectBuilder Extras/Project Templates/Application

Many thanks to Carl Norum for giving this to me!

I have used that, with great success Smile

However, I noticed that it includes the Cocoa framework, does this mean that my project is effectivly compiling as a mixed source project? If so, will this lead to any slow-down after compilation/other such oddities?
Quote this message in a reply
Geppy
Unregistered
 
Post: #12
I haven't noticed any slowdown myself.....

Frankly, nowadays I just use it to start a GLUT project and then I completely replace the main.c file with my own .cpp code. However you'll note you don't actually have to include the line "#include <Cocoa/cocoa.h>" to correctly compile. BUT the cocoa framework must be left in to get glut to link in properly.

I think the OS X implementation of GLUT hooks into cocoa for drawing windows and such. (?)
Quote this message in a reply
Geppy
Unregistered
 
Post: #13
Oh also, if you use this:

#if defined(__APPLE_CC__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

and don't include any platform-specific headers than you'll have completly crossplatform code!! Which is a very good thing (for instance in my graphics class I code on my mac and the programs get compiled and graded on linux by my teacher).

I stole that little chunk of code from OneSadCookie so thanks to him! Hmm....given my earlier post and now this one I must sound like some sort of "code pirate"......ARRRR!! Sneaky
Quote this message in a reply
Mars_999
Unregistered
 
Post: #14
Quote:Originally posted by OneSadCookie

At this stage, you shouldn't be worrying about all this Smile Just grab the GLUT or SDL ports of NeHe's tutorials and jump in with C. Start a new thread if you have problems compiling the tutorials.

I am going to back up OSC on this and say get SDL and use C++ with it. You can use C++ in SDL just like C. No problems at all. If you ask me SDL is easier to setup than GLUT for OPENGL.
Here is some code to get you up and running QUICK.


Code:
#include <opengl/glu.h>
#include <opengl/gl.h>
#include <opengl/glext.h>
#include <sdl.h>
#include <fstream>
using namespace std;

const short SCREEN_WIDTH = 1024;
const short SCREEN_HEIGHT = 768;
const short BIT = 32;

void InitSDL(void);
void InitOpenGL(short w, short h);
void Events(void);
void Render(void);

int main(int argc, char *argv[])
{
    InitSDL();
    InitOpenGL(SCREEN_WIDTH, SCREEN_HEIGHT);
    Render();
    SDL_Quit();
    return 0;
}

void Render(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//all rendering will be done here
    SDL_GL_SwapBuffers();
}

void InitSDL(void)
{
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        ofstream fout("error.log");
        fout << "Unable to Init SDL video!!\n";
        fout.close();
        exit(0);
    }

    if(SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BIT, SDL_FULLSCREEN | SDL_OPENGL) == NULL)
    {
        ofstream fout("error.log");
        fout << "Unable to Init SDL videomode!!\n";
        fout.close();
        SDL_Quit();
        exit(0);
    }
}

void InitOpenGL(short w, short h)
{
    glViewport(0, 0, w, h);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, float(w), 0.0f, float(h), -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
}


This will get you up and running in OSX under Project builder once you downloaded SDL and installed it. You will need to add the OpenGL framework to your project also.
Quote this message in a reply
Member
Posts: 157
Joined: 2002.12
Post: #15
All I did was look at Apple's Carbon sample code, using openGL, and everything's working fine for me.
Most of your code will be portable except any calls you make to agl functions.
Quote this message in a reply
Post Reply