现在Windows系统的主流编译环境有Visual Studio,Broland C++ Builder,Dev-C++等,它们都是支持OpenGL的。但老师上课选择VC++ 6.0作为学习OpenGL的环境。
但是我自己选的是VS2010 比较好用。
2.安装glew-1.9.0-win32
在官方网站上面下载:
以前那个官网已经没有了,这个是新的官网
解压后.h文件放在../Microsoft Visual Studio\VC98\include\GL文件夹里,.lib文件放在lib文件夹里,.dll放在system32里面
下面就可以添加代码啦:
/*第一个OPENGL程序*/
#include <GL/glew.h>
#include <GL/wglew.h>
#include <GL/glut.h>
#include <stdio.h>
#include<windows.h> // Header File For Windows
#include<stdlib.h>
#ifdef GL_VERSION_1_3
void setupPointer (void )
{ static GLint vertices [] = { 25, 25, 75, 75, 100, 125, 150, 75, 200, 175, 250, 150, 300, 125, 100, 200, 150, 250, 200, 225, 250, 300, 300, 250}; glEnableClientState (GL_VERTEX_ARRAY ); glVertexPointer (2, GL_INT , 0, vertices ); }
void init (void )
{ GLenum err = glewInit (); if (err != GLEW_OK ) {
exit(-2); } glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH ); setupPointer (); }
void display (void )
{ static GLubyte oneIndices [] = {0, 1, 2, 3, 4, 5, 6}; static const GLubyte twoIndices [] = {1, 7, 8, 9, 10, 11}; static GLsizei count [] = {7, 6}; static const GLvoid * indices [2] = {oneIndices , twoIndices }; glClear (GL_COLOR_BUFFER_BIT ); glColor3f (1.0, 1.0, 1.0); glMultiDrawElementsEXT (GL_LINE_STRIP , count , GL_UNSIGNED_BYTE , indices , 2); // 上面的一句相当于下面的两句 //glDrawElements(GL_LINE_STRIP, count[0], GL_UNSIGNED_BYTE, oneIndices); //glDrawElements(GL_LINE_STRIP, count[1], GL_UNSIGNED_BYTE, twoIndices); glFlush (); }
void reshape (int w , int h )
{ glViewport (0, 0, (GLsizei ) w , (GLsizei ) h ); glMatrixMode (GL_PROJECTION ); glLoadIdentity (); gluOrtho2D (0.0, (GLdouble ) w , 0.0, (GLdouble ) h ); }
void keyboard (unsigned char key , int x , int y )
{ switch (key ) { case 27: exit (0); break ; } } int main (int argc , char ** argv )
{ glutInit (&argc , argv ); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize (350, 350); glutInitWindowPosition (100, 100); glutCreateWindow (argv [0]); init (); glutDisplayFunc (display ); glutReshapeFunc (reshape ); glutKeyboardFunc (keyboard ); glutMainLoop (); return 0; }
#else
int main(int argc, char** argv)
{ fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n"); fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n"); fprintf (stderr, "you may be able to modify this program to make it run.\n"); return 0; }