Using GLUT, the following code will do this:
(you may need to aquire glut or freeglut to be able to compile this. The alternative, just using a frame/dialog based app and your own code to setup the window, process the keys, handle idle times, handle resizing etc, etc is quite long-winded. glut or freeGlut are a much easier learning curve)
This code could be trimmed further, though will be left as an exercise to the reader..
#include <GL/glut.h>
#include <stdlib.h>
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3d(1,0,0);
glVertex3f(-1,-1,-10);
glColor3d(1,1,0);
glVertex3f(1,-1,-10);
glColor3d(1,1,1);
glVertex3f(1,1,-10);
glColor3d(0,1,1);
glVertex3f(-1,1,-10);
glEnd();
glutSwapBuffers();
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
}
glutPostRedisplay();
}
static void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT quadPoly");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glClearColor(0,0,0,0);
glutMainLoop();
return EXIT_SUCCESS;
}
A few tutes -
http://www.codecolony.de/OpenGL/[
^]
I would have recomended NEHE's openGL tutes too, though I seem unable to access them for some period now. Perhaps you could access a google-cached version of the page
Here's another page with a go-to-whoa! set of tutes. My apologies for the title of the site.
OpenGl tutes[
^]
Look for the Red Book, the Blue Book or the Orange Book (start with the red one)
There's also a number of articles here on CP for that. Curiously enough, the search term "openGL tute" run through google brings up
[EDIT:] It seems that Nehe's site was suffering database problems, though is now back online. I highly reccomend his series of 48 tutes. Start at the beginning and work your way through them. GLAUX is rather old in the tooth now, though is mostly used for image-loading routines. Just get a bmp or tga loader and you'll be able to do without it for the most part.
NEHE openGL tutes[
^]