Getting started with OpenGL using 'C' (not C++)

Hi,

First question, is it possible/useful to program OpenGL just with C and not C++? If it’s possible, does it have severe limitations on what I can do? I have just finished a cource on C and would like to get things happening with OGL straight away instead of learning C++.

In as little code as possible, what’s the minimum I need to get a triangle rendered? I want to avoid all the setup of windows etc. Does GLUT work with plain C? Ideally, I’d like a ‘hello world’ program that draws a triangle instead of a text string. I’ve read the tutorials at NeHe and all the things about setting up windows is really too tedious.

Lastly, am I taking the wrong mentality already? Should I be learning all the tedious stuff first or C++ before I take on learning OpenGL?

Many Thanks.

No, It is easy if you use GLUT to setup a window. To learn the easy way, you can start with the red book:
http://ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_PG/

It teaches you how to create a gradient triangle and setup a window with only 55 lines of code.

If you need the GLUT Package, goto:
http://www.xmission.com/~nate/glut.html

Hope that helps.

[This message has been edited by VC6-OGL (edited 12-06-2002).]

It is easier to program OpenGL in VC6 or Visual C++ 6 for me, but that is my opinion. And it would be a good idea to learn all you can in C++ before trying to start OpenGL Programming, but that is also my opinion.

  • VC6-OGL

here is the least anount of code you need

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

void myInit();
void myDraw();

main( int argc, char *argv[] )
{

/* Initialize window system */
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 250, 250 );
glutCreateWindow( “Hello World” );

/* Initialize graphics */
myInit();

/* Display callback and enter event loop */
glutDisplayFunc( myDraw );
glutMainLoop();
}

/set up viewing/
void myInit()
{
/* Background color */
glClearColor(0.0, 0.0, 0.0, 1.0);

/* Projection */
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );
}

void myDraw()
{
/* Clear the screen */
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT );

/* Example code to draw 3 white points /
/
* Draw your points here **/
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 );

glEnd();

/* Execute draw commands */
glFlush();
}

So what are you exactly trying to do?

also in my opinion nothing in opengl is much easier or harder with c++. Just knowing C will be fine when you are just starting out with opengl

or

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

void init( void ) {
glClearColor ( 0.0, 0.0, 0.0, 0.0 );
glShadeModel ( GL_SMOOTH );
}

void triangle( void ) {
glBegin ( GL_TRIANGLES );
glColor3f ( 1.0, 0.0, 0.0 );
glVertex2f ( 5.0, 5.0 );
glColor3f ( 0.0, 1.0, 0.0 );
glVertex2f ( 25.0, 5.0 );
glColor3f ( 0.0, 0.0, 1.0 );
glVertex2f ( 5.0, 25.0 );
glEnd();
}

void display( void ) {
glClear ( GL_COLOR_BUFFER_BIT );
triangle();
glFlush();
}

void reshape( int h, int w ) {
glViewport ( 0, 0, ( GLsizei ) w, ( GLsizei ) h);
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
if ( w <= h )
gluOrtho2D ( 0.0, 30.0, 0.0, 30.0 * ( GLfloat ) h / ( GLfloat ) w );
else
gluOrtho2D ( 0.0, 30.0 * ( GLfloat ) h / ( GLfloat ) w, 0.0, 30.0 );
glMatrixMode ( GL_MODELVIEW );
}

int main( int argc, char** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize ( 500, 500 );
glutInitWindowPosition ( 100, 100 );
glutCreateWindow ( “Traingle” );
init();
glutDisplayFunc ( display );
glutReshapeFunc ( reshape );
glutMainLoop();
return 0;
}

[This message has been edited by VC6-OGL (edited 12-06-2002).]

[This message has been edited by VC6-OGL (edited 12-06-2002).]

Thanks guys
The red book 55 line code example is very enlightening.

Just curious, how powerful is GLUT? If one ever wanted to build concise but powerful programs like shadows, smoke etc, or maybe expose the latest feature of a new GPU, would one still use GLUT?

GLUT is for creating windows and handle input, that’s all. It doesn’t care what you do in OpenGL. And it’s really not the best windowing API if you want to make a larger program, but that isn’t the point with GLUT either.

[This message has been edited by Bob (edited 12-06-2002).]

Hi!

Now I will make you even more confused

Check GLFW too (an alternative to GLUT). You will also find basic tutorials on the GLFW page.

GLFW triangle example (with animation, FPS counter & comments):

#include <stdio.h>
#include <GL/glfw.h>

//========================================================================
// main()
//========================================================================

int main( void )
{
int width, height, running, frames, x, y;
double t, t0, fps;
char titlestr[ 200 ];

// Initialise GLFW
glfwInit();
// Open OpenGL window
if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) )
{
    glfwTerminate();
    return 0;
}
// Enable sticky keys
glfwEnable( GLFW_STICKY_KEYS );
// Disable vertical sync (on cards that support it)
glfwSwapInterval( 0 );
// Main loop
running = GL_TRUE;
frames = 0;
t0 = glfwGetTime();
while( running )
{
    // Get time and mouse position
    t = glfwGetTime();
    glfwGetMousePos( &x, &y );
    // Calculate and display FPS (frames per second)
    if( (t-t0) > 1.0 | | frames == 0 )
    {
        fps = (double)frames / (t-t0);
        sprintf( titlestr, "Spinning Triangle (%.1f FPS)", fps );
        glfwSetWindowTitle( titlestr );
        t0 = t;
        frames = 0;
    }
    frames ++;
    // Get window size (may be different than the requested size)
    glfwGetWindowSize( &width, &height );
    height = height > 0 ? height : 1;
    // Set viewport
    glViewport( 0, 0, width, height );
    // Clear color buffer
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT );
    // Select and setup the projection matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f,
        100.0f );
    // Select and setup the modelview matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt( 0.0f, 1.0f, 0.0f,    // Eye-position
               0.0f, 20.0f, 0.0f,   // View-point
               0.0f, 0.0f, 1.0f );  // Up-vector
    // Draw a rotating colorful triangle
    glTranslatef( 0.0f, 14.0f, 0.0f );
    glRotatef( 0.3*(GLfloat)x + (GLfloat)t*100.0f, 0.0f, 0.0f, 1.0f );
    glBegin( GL_TRIANGLES );
      glColor3f( 1.0f, 0.0f, 0.0f );
      glVertex3f( -5.0f, 0.0f, -4.0f );
      glColor3f( 0.0f, 1.0f, 0.0f );
      glVertex3f( 5.0f, 0.0f, -4.0f );
      glColor3f( 0.0f, 0.0f, 1.0f );
      glVertex3f( 0.0f, 0.0f, 6.0f );
    glEnd();
    // Swap buffers
    glfwSwapBuffers();
    // Check if the ESC key was pressed or the window was closed
    running = !glfwGetKey( GLFW_KEY_ESC ) &&
              glfwGetWindowParam( GLFW_OPENED );
}
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;

}

BTW, OpenGL was designed for ANSI C. Using it from C++ does not have any advantages with regard to the OpenGL API.

[This message has been edited by marcus256 (edited 12-08-2002).]

OpenGL was wrote with ‘C’, so programming in plan ‘C’ is no problem.
I think ‘C’ is a little easier to start programming with.

I have opengl examples on my website writtin in plain ‘C’.
http://www.angelfire.com/linux/nexusone/

C is enough for getting started with OpenGL, and even advanced. And actually C++ makes no easier to do windows/input tasks easier… but glfw and glut are easier to deal than winapi(and even MFC), but knowing winapi gives you more control over your programs windows layerying, input… etc.
It would even be better for everyone to use winapi within C++… :stuck_out_tongue:

Originally posted by pushDX:
It would even be better for everyone to use winapi within C++… :stuck_out_tongue:

…unless they want to write portable and/or readable code