tags:

views:

157

answers:

4

I program in 3D using C++, OpenGL and wxWidgets. Not being much aware of what is happening in the other programming language/frameworks, I got this question:

Which programming language (or programming platform) is the easiest today for a beginning 3D programmer today? (Not necessarily a game programmer!) Demonstrate your case by showing the simplest (not smallest) piece of code in that language that will draw a square/cube in a window. The code should be complete and compilable/interpretable.

For the case of C++, OpenGL and GLUT, here is the first example (1-2) from the Red Book (The OpenGL Programming Guide):

#include <GL/gl.h>
#include <GL/glut.h>

void display(void)
{
/*  clear all pixels  */
    glClear (GL_COLOR_BUFFER_BIT);

/*  draw white polygon (rectangle) with corners at
 *  (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)  
 */
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();

/*  don't wait!  
 *  start processing buffered OpenGL routines 
 */
    glFlush ();
}

void init (void) 
{
/*  select clearing (background) color       */
    glClearColor (0.0, 0.0, 0.0, 0.0);

/*  initialize viewing values  */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

/* 
 *  Declare initial window size, position, and display mode
 *  (single buffer and RGBA).  Open window with "hello"
 *  in its title bar.  Call initialization routines.
 *  Register callback function to display graphics.
 *  Enter main loop and process events.
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;   /* ISO C requires main to return int. */
}

As you can very well see, it uses GLUT, which is an archaic windowing framework for OpenGL.

+2  A: 

I'm not sure how or why you would want the code to be any smaller than that...

There's a great deal that you really need to explicitly specify.

Your code consists of:

  • Specifying a square
  • Configuring the display mode and projection
  • Specifying the windowsize and position

You have no more code than information that you specify, which is a great definition for efficient expression. If the code was any smaller, you wouldn't be specifying enough information to describe what you wanted to do.

Dave Gamble
+1  A: 

If you want something besides GLUT, then you need to use the OpenGL context controls provided by your operating system. This varies significantly from platform to platform. NeHe Productions provides dozens of OpenGL tutorials on their site, and in the very first they show you how to create an OpenGL context on several operating systems.

If you still long for cross platform compatibility, then your best bet is likely SDL, which is very much like GLUT in that it is a fairly thin layer over the underlying abstractions, but it provides a slightly richer event system, and more opportunities for interacting with hardware, such as sound and joysticks.

That being said, there's nothing particularly archaic about GLUT. There are many implementations of GLUT. The nice thing about it is that it's almost certain to be present on even strangely configured hosts, whereas any other solution will require either platform specific code or the presence of some cross platform compatibility layer. If I wanted to keep things simple for a program that only needed to display 3-D graphics, and interact only by mouse and keyboard, then I'd probably be totally happy with GLUT.


But if you want even easier than that? well, I'd probably point you to a Scene graph library with bindings to some interpretable language.

TokenMacGuy
+1  A: 

If you want to make this and the other Red Book examples smaller, do not hard code the color, vertex, light, etc. information. Decide on a format and read the model information from a file. It will make the examples easier to understand and in the end you will not want this kind of stuff hard coded.

The rest of the code, as the other posters have pointed out, is necessary. You do not have to use GLUT, though there's nothing wrong with it - I kind of like GLFW. You could also try the Tao Framework, a set of Mono/.Net bindings to GLUT, OpenGL, SDL, GLFW, and much more. Both are cross-platform.

R Ubben
A: 

GLUT may be archaic, but I don't think it's given enough respect. The framework is extremely versatile, and I'm not sure any other language or interface has bested it, while maintaining its versatility. I think it's also the best 3D graphics language to start with - you learn a lot of things that other languages might hide behind-the-scenes, much like learning C++ before Java.

andystacy