我是OpenGL的新手,并一直在努力追随红皮书(第8版)。但是,在尝试运行第一个教程时,我遇到了一个问题。起初,我的IDE(VS 2012)发现了该行的问题:
GLuint program = LoadShaders(shaders)
与此相关的问题是无法将ShaderInfo [3]转换为ShaderInfo。我尝试通过将行更改为:
来解决此问题GLuint program = LoadShaders(*shaders)
,这似乎有点奏效。但是,在编译代码时,会出现错误(未解析的外部)。我怀疑我之前尝试修复上述问题的原因可能是原因,但是经过多次使用代码无效后,我决定问一个知道自己在做什么的人。
1>------ Build started: Project: sarpg, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(7): warning C4005: 'BUFFER_OFFSET' : macro redefinition
1> c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\vgl.h(55) : see previous definition of 'BUFFER_OFFSET'
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(23): warning C4305: 'initializing' : truncation from 'double' to 'GLfloat'
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(24): warning C4305: 'initializing' : truncation from 'double' to 'GLfloat'
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(25): warning C4305: 'initializing' : truncation from 'double' to 'GLfloat'
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(26): warning C4305: 'initializing' : truncation from 'double' to 'GLfloat'
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(27): warning C4305: 'initializing' : truncation from 'double' to 'GLfloat'
1>c:\users\callum\documents\visual studio 2012\projects\sarpg\sarpg\main.cpp(28): warning C4305: 'initializing' : truncation from 'double' to 'GLfloat'
1>main.obj : warning LNK4217: locally defined symbol _glewInit@0 imported in function _main
1>main.obj : warning LNK4049: locally defined symbol ___glewBindBuffer imported
1>main.obj : warning LNK4049: locally defined symbol ___glewBufferData imported
1>main.obj : warning LNK4049: locally defined symbol ___glewGenBuffers imported
1>main.obj : warning LNK4049: locally defined symbol ___glewUseProgram imported
1>main.obj : warning LNK4049: locally defined symbol ___glewBindVertexArray imported
1>main.obj : warning LNK4049: locally defined symbol ___glewGenVertexArrays imported
1>main.obj : error LNK2019: unresolved external symbol "unsigned int __cdecl LoadShaders(struct ShaderInfo)" (?LoadShaders@@YAIUShaderInfo@@@Z) referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>C:\Users\Callum\documents\visual studio 2012\Projects\sarpg\Debug\sarpg.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <iostream>
//#include <GL/glew.h>
//#include <GL/glut.h>
#include "LoadShader.h"
#include "vgl.h"
//#pragma comment( lib, "C://Libs/GLEW/glew-1.9.0/lib/glew32.lib" )
#define BUFFER_OFFSET(offset) ((void *)(offset))
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},
{0.85, -0.90},
{-0.90, 0.85},
{0.90, -0.85},
{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);
}
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 init GLEW ... exiting" <<endl;
exit(EXIT_FAILURE);
}
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
//////////////////////////////////////////////////////////////////////////////
//
// --- 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__
#ifndef __VGL_H__
#define __VGL_H__
// #define USE_GL3W
#ifdef USE_GL3W
#include <GL3/gl3.h>
#include <GL3/gl3w.h>
#else
#define GLEW_STATIC
#include <GL/glew.h>
#ifdef _MSC_VER
# ifdef _DEBUG
# if (_MSC_VER >= 1600)
# pragma comment (lib, "glew_static_vs2010_d.lib")
# else
# pragma comment (lib, "glew_static_d.lib")
# endif
# else
# if (_MSC_VER >= 1600)
# pragma comment (lib, "glew_static_vs2010.lib")
# else
# pragma comment (lib, "glew_static.lib")
# endif
# endif
#endif
#endif
#define FREEGLUT_STATIC
#include <GL/freeglut.h>
#ifdef _MSC_VER
# ifdef _DEBUG
# if (_MSC_VER >= 1600)
# pragma comment (lib, "freeglut_static_vs2010_d.lib")
# else
# pragma comment (lib, "freeglut_static_d.lib")
# endif
# else
# if (_MSC_VER >= 1600)
# pragma comment (lib, "freeglut_static_vs2010.lib")
# else
# pragma comment (lib, "freeglut_static.lib")
# endif
# endif
#endif
#define BUFFER_OFFSET(x) ((const void*) (x))
#endif /* __VGL_H__ */
(P.S。是的,我确实记得链接库(我认为))
答案 0 :(得分:1)
缺少LoadShaders(struct ShaderInfo)
符号的错误表示您的main.cpp
包含的内容定义的LoadShaders
原型不同于我在LoadShaders.h
中看到的内容。奇怪的是,也许这是一个错字,你的main.cpp
包括LoadShader.h
(缺少's'),但实际的标题称为LoadShaders.h
。巧合的是,有一个LoadShader.h
在一个不同的函数原型周围放置。