1. 学习网站:
Nehe的OpenGL中文教程:http://www.yakergong.net/nehe/,(貌似这里的sdk和代码不能下载)
Nehe的原版英文教程:http://nehe.gamedev.net/ (可以下载源码)
2. OpenGL的SDK下载:http://download.youkuaiyun.com/detail/damotiansheng/8031709
3. 下载了sdk后,在vs中设置了库文件和包含文件目录后(可能还需把dll放到system32目录下),添加代码如下:
注:下面的代码是从http://nehe.gamedev.net/下载来的,为lesson1的代码,同时增加些代码,不过需要改头文件,把头文件改为如下所示,即可编译运行了
/*
* This Code Was Created By Jeff Molofee 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
* And Optimizing This Code, Making It More Flexible!
* If You've Found This Code Useful, Please Let Me Know.
* Visit My Site At nehe.gamedev.net
*/
#include <windows.h> // Header File For Windows
#include <gl/glew.h> <span style="color:#ff0000;">// 包含最新的gl.h,glu.h库 //头文件不用原版的glaux.h,否则我的编译不过</span>
#include <gl/glut.h> // 包含OpenGL实用库
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; <span style="color:#ff0000;">// Permanent Rendering Context,它把OpenGL调用函数连接到设备上下文hDC,然后hDC再进行绘制</span>
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); //视点为(0,0) // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//深度0.1到100的场景都可见,即需要被绘制
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
int InitGL(GLvoid) //初始化,以给定颜色清除屏幕 // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
int DrawGLScene(GLvoid) //绘制图形 // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER