Recently, the company has never been able to do anything, so it began to visit the forum to pay attention to some things. I accidentally saw the information about the android game engine, so I started to learn OpenGL with a whim. I personally think this should be a foundation for learning the game engine. Recently I was reading the C programming language, so I plan to start learning OpenGL from the C language.
Maybe Java has been doing for a long time, especially do not like Microsoft's development interface, so I decided to use my favorite DEV-C ++.
DEV-C ++ originated from Bell Labs, but was updated a long time ago to 4.9.9.2. It will not be updated, and two derivative products will be replaced later.
DEV-C ++ comes with examples of OpenGL. File-> New-> Project-> multmedia-> OpenGL, you can build an OpenGL demo, which is a continuously rotating triangle after compilation and running.
I searched some OpenGL entry-level documents on the Internet, and recommended the glut toolkit, which is said to bring a lot of convenience.
So began to install glut for DEC-C ++.
Download the glut-3.7.6-bin of glut, which contains several files
Copy glut. H to the installation directory of DEV-C ++ under .. \ include \ GL.
Copy glut. Def to the Lib folder.
Copy the file to system c: \ windows \ system32.
Create a project
#define GLUT_DISABLE_ATEXIT_HACK#include <windows.h>#include <GL/glut.h>const int A = 500; /* length of a side of the monitor window */const float B = 500; /* length of a side of the clipping rectangle */const float C = 200; /* length of a side of the square the program draws */void myinit(void){ glClearColor(0.7, 0.7, 0.7, 0.0); /* gray background */ glMatrixMode(GL_PROJECTION); /* In World coordinates: */ glLoadIdentity(); /* position the "clipping rectangle" */ gluOrtho2D( -B/2, B/2, -B/2, B/2);/* at -B/2, its right edge at +B/2, its bottom */ glMatrixMode(GL_MODELVIEW); /* edge at -B/2 and its top edge at +B/2 */}void display( void ){ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ glMatrixMode(GL_MODELVIEW); /* The following coordinates are expressed */ glLoadIdentity(); /* in terms of World coordinates */ glBegin(GL_POLYGON) ; /* draw a filled polygon */ glColor3f ( 1.0, 0.3, 0.2); /* draw in light red */ glVertex2f( -C/2, -C/2 ); /* (x,y) */ glVertex2f( C/2, -C/2 ); /* (x,y) */ glVertex2f( C/2, C/2 ); /* (x,y) */ glVertex2f( -C/2, C/2 ); /* (x,y) */ glEnd(); glFlush(); /* send all commands */}int main(int argc, char** argv){ glutInit(&argc,argv); glutInitWindowSize( A, A ); /* A x A pixel screen window */ glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE); glutCreateWindow("My Rectangle"); /* window title */ glutDisplayFunc(display); /* tell OpenGL main loop what */ myinit(); /* set attributes */ glutMainLoop(); /* pass control to the main loop */ return 0;}
Please note:
#define GLUT_DISABLE_ATEXIT_HACK#include <windows.h>
These two lines must be included, if not added, will package error during compilation: 50 E: \ Program Files \ DEV-CPP \ include \ GL \ glut. h redeclaration of C ++ built-in type 'shport'
After adding it, it still cannot be compiled. You need to make some settings.
Project-project options-parameters add three files: libglu32.a, libglu32.a, and libopengl32.a
These three files are all in the Lib folder in the installation directory of the DEV-C ++.
Then compile and run
The result is as follows:
Here we end the first step of OpenGL learning.
Split line -------------------------------------------------------------------------
How to install glut, Google gave a lot of available information. But each compilation always appears: 50 E: \ Program Files \ DEV-CPP \ include \ GL \ glut. h redeclaration of C ++ built-in type 'short 'makes me struggle for a long time. Click here to open the link. Code added in the two lines
#define GLUT_DISABLE_ATEXIT_HACK#include <windows.h>
What does it mean? No specific explanation is found. We can infer from the name that some error messages should be ignored ......