忙了一下午, 终于能运行:
Opengl编程指南里, 书里代码不全, 好不容量才在网上找到原代码
https://blog.csdn.net/candycat1992/article/details/39676669
不知为何,之前下载glew2.1压缩包会出错, 不能解压, 今天下下来倒是好的.
使用 [管理 NuGet 程序包] 下载下来一个glut的包. 这个包里也带有glew32.lib x64环境下能用.
之前曾出现过LNK2001错误, 不知为何.
书上有用到 glutInitContextVersion 这个函数, 我这里是用不了的. 还好不是必要的.
2018-05-05_研究新版本glew2.1.cpp:
// 2018-05-05_开发环境再试验.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <GL/glew.h>
#include <GL/glut.h>
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
#define BUFFER_OFFSET(offset) ((void*)(offset))
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;
// 书p21
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
// 书P12
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);
// 其实不用Shader也能运行, 不过面片是白色的, 没有这两个Shader文件的话,不会有报错,但 面片是白色的
ShaderInfo shaders[] = {
// { GL_VERTEX_SHADER,"triangles.vert" },
//{ GL_FRAGMENT_SHADER,"triangles.frag" },
{ GL_NONE,NULL }
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
// END Shader
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition);
}
// 书P11
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowPosition(960, 100);
glutInitWindowSize(512, 512);
//glutInitContextVersion(4, 3);
glutCreateWindow("第一个OpenGL程序");
if (glewInit())
{
cerr << "Unable to initalize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(&display);
glutMainLoop();
return 0;
}
stdafx.h:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: 在此处引用程序需要的其他头文件
#include<iostream>
using namespace std;
找到了源码的下载才取得了LoadShader这两个文件:
(OpenGL编程指南(第八版)pdf + 源码) http://down.51cto.com/data/2193129
LoadShaders.h:
//
//
// --- LoadShaders.h ---
//
//
#ifndef __LOAD_SHADERS_H__
#define __LOAD_SHADERS_H__
#include <GL/gl.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//----------------------------------------------------------------------------
//
// LoadShaders() takes an array of ShaderFile structures, each of which
// contains the type of the shader, and a pointer a C-style character
// string (i.e., a NULL-terminated array of characters) containing the
// entire shader source.
//
// The array of structures is terminated by a final Shader with the
// "type" field set to GL_NONE.
//
// LoadShaders() returns the shader program value (as returned by
// glCreateProgram()) on success, or zero on failure.
//
typedef struct {
GLenum type;
const char* filename;
GLuint shader;
} ShaderInfo;
GLuint LoadShaders( ShaderInfo* );
//----------------------------------------------------------------------------
#ifdef __cplusplus
};
#endif // __cplusplus
#endif // __LOAD_SHADERS_H__
LoadShaders.cpp:
//
//
// --- LoadShaders.cxx ---
//
//
#include "stdafx.h"
#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_s(&infile,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