忙了一下午, 終於能運行:ios
Opengl編程指南里, 書裏代碼不全, 好不容量纔在網上找到原代碼編程
https://blog.csdn.net/candycat1992/article/details/39676669函數
不知爲什麼,以前下載glew2.1壓縮包會出錯, 不能解壓, 今天下下來卻是好的.oop
使用 [管理 NuGet 程序包] 下載下來一個glut的包. 這個包裏也帶有glew32.lib x64環境下能用.ui
以前曾出現過LNK2001錯誤, 不知爲什麼.spa
書上有用到 glutInitContextVersion 這個函數, 我這裏是用不了的. 還好不是必要的..net
2018-05-05_研究新版本glew2.1.cpp: code
// 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:blog
#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