网上有好多在vs2013中如何实现opengl红宝书第八版的首个例子的文章,但是自己尝试过后发现都多多少少有些问题。
首先去 http://www.opengl-redbook.com/ 下载红宝书的源代码,解压得到这个
将lib文件夹下的文件全部copy到vs2013安装目录的lib文件夹里
将include文件夹下的文件全部copy到vs2013安装目录的include文件夹里(注意里面有个GL文件夹,你自己的安装目录里如果配置过opengl也应该有GL文件夹)
接下来就可以在vs中新建项目了
新建一个cpp文件输入如下代码(课本上的)
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;
void init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices][2] = {
{ -0.90, -0.90 }, // Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, // Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_STATIC_DRAW);
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangles.vert" },
{ GL_FRAGMENT_SHADER, "triangles.frag" },
{ GL_NONE, NULL }
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
glVertexAttribPointer(vPosition, 2, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(0));
glewExperimental = GL_TRUE;
glEnableVertexAttribArray(vPosition);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
//---------------------------------------------------------------------
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}<pre name="code" class="cpp">//
//
// --- LoadShaders.cxx ---
//
//
#include <cstdlib>
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include "LoadShaders.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//----------------------------------------------------------------------------
static const GLchar*
ReadShader(const char* filename)
{
#ifdef WIN32
FILE* infile;
fopen_s(&infile, filename, "rb");
#else
FILE* infile = fopen(filename, "rb");
#endif // WIN32
if (!infile) {
#ifdef _DEBUG
std::cerr << "Unable to open file '" << filename << "'" << std::endl;
#endif /* DEBUG */
return NULL;
}
fseek(infile, 0, SEEK_END);
int len = ftell(infile);
fseek(infile, 0, SEEK_SET);
GLchar* source = new GLchar[len + 1];
fread(source, 1, len, infile);
fclose(infile);
source[len] = 0;
return const_cast<const GLchar*>(source);
}
//----------------------------------------------------------------------------
GLuint
LoadShaders(ShaderInfo* shaders)
{
if (shaders == NULL) { return 0; }
GLuint program = glCreateProgram();
ShaderInfo* entry = shaders;
while (entry->type != GL_NONE) {
GLuint shader = glCreateShader(entry->type);
entry->shader = shader;
const GLchar* source = ReadShader(entry->filename);
if (source == NULL) {
for (entry = shaders; entry->type != GL_NONE; ++entry) {
glDeleteShader(entry->shader);
entry->shader = 0;
}
return 0;
}
glShaderSource(shader, 1, &source, NULL);
delete[] source;
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
#ifdef _DEBUG
GLsizei len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len + 1];
glGetShaderInfoLog(shader, len, &len, log);
std::cerr << "Shader compilation failed: " << log << std::endl;
delete[] log;
#endif /* DEBUG */
return 0;
}
glAttachShader(program, shader);
++entry;
}
#ifdef GL_VERSION_4_1
if (GLEW_VERSION_4_1) {
// glProgramParameteri( program, GL_PROGRAM_SEPARABLE, GL_TRUE );
}
#endif /* GL_VERSION_4_1 */
glLinkProgram(program);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
#ifdef _DEBUG
GLsizei len;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len + 1];
glGetProgramInfoLog(program, len, &len, log);
std::cerr << "Shader linking failed: " << log << std::endl;
delete[] log;
#endif /* DEBUG */
for (entry = shaders; entry->type != GL_NONE; ++entry) {
glDeleteShader(entry->shader);
entry->shader = 0;
}
return 0;
}
return program;
}
//----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif // __cplusplus
init();glutDisplayFunc(display);glutMainLoop();}
//
//
// --- LoadShaders.cxx ---
//
//
#include <cstdlib>
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include "LoadShaders.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//----------------------------------------------------------------------------
static const GLchar*
ReadShader(const char* filename)
{
#ifdef WIN32
FILE* infile;
fopen_s(&infile, filename, "rb");
#else
FILE* infile = fopen(filename, "rb");
#endif // WIN32
if (!infile) {
#ifdef _DEBUG
std::cerr << "Unable to open file '" << filename << "'" << std::endl;
#endif /* DEBUG */
return NULL;
}
fseek(infile, 0, SEEK_END);
int len = ftell(infile);
fseek(infile, 0, SEEK_SET);
GLchar* source = new GLchar[len + 1];
fread(source, 1, len, infile);
fclose(infile);
source[len] = 0;
return const_cast<const GLchar*>(source);
}
//----------------------------------------------------------------------------
GLuint
LoadShaders(ShaderInfo* shaders)
{
if (shaders == NULL) { return 0; }
GLuint program = glCreateProgram();
ShaderInfo* entry = shaders;
while (entry->type != GL_NONE) {
GLuint shader = glCreateShader(entry->type);
entry->shader = shader;
const GLchar* source = ReadShader(entry->filename);
if (source == NULL) {
for (entry = shaders; entry->type != GL_NONE; ++entry) {
glDeleteShader(entry->shader);
entry->shader = 0;
}
return 0;
}
glShaderSource(shader, 1, &source, NULL);
delete[] source;
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
#ifdef _DEBUG
GLsizei len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len + 1];
glGetShaderInfoLog(shader, len, &len, log);
std::cerr << "Shader compilation failed: " << log << std::endl;
delete[] log;
#endif /* DEBUG */
return 0;
}
glAttachShader(program, shader);
++entry;
}
#ifdef GL_VERSION_4_1
if (GLEW_VERSION_4_1) {
// glProgramParameteri( program, GL_PROGRAM_SEPARABLE, GL_TRUE );
}
#endif /* GL_VERSION_4_1 */
glLinkProgram(program);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
#ifdef _DEBUG
GLsizei len;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len + 1];
glGetProgramInfoLog(program, len, &len, log);
std::cerr << "Shader linking failed: " << log << std::endl;
delete[] log;
#endif /* DEBUG */
for (entry = shaders; entry->type != GL_NONE; ++entry) {
glDeleteShader(entry->shader);
entry->shader = 0;
}
return 0;
}
return program;
}
//----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif // __cplusplus
这个时候编译并运行,如果没有画出三角形,并且弹出如下信息:
这个时候可以改变这条语句:glutInitContextVersion(4, 3);把(4,3)改为(4,2)或者(3,1)等低版本,或者干脆注释掉。
这个时候已经可以运行了,单画出来的三角形是白色的,并不是蓝色的。。
终于到了最后一步了:
在你的工程目录下添加俩个文件,triangles.vert和triangles.frag, 用记事本新建就可,改好名字
triangles.vert内容如下:
#version 430 core
layout(location = 0) in vec4 vPosition;
void
main()
{
gl_Position = vPosition;
}
如果之前你出现过版本不支持4.3的问题,直接把第一句删除。
triangles.frag的内容:
#version 430 core
out vec4 fColor;
void
main()
{
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}
同理
如果之前你出现过版本不支持4.3的问题,直接把第一句删除。
ok 大功告成了!!!