gpt4 book ai didi

c++ - 红皮书示例 1.1 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:27:09 24 4
gpt4 key购买 nike

我是 OpenGL 的新手,一直在努力遵循红皮书(第 8 版)。然而,在尝试运行第一个教程时,我遇到了一个问题。起初,我的 IDE (VS 2012) 发现了以下行的问题:

GLuint program = LoadShaders(shaders)

发现的问题是无法将 ShaderInfo[3] 转换为 ShaderInfo。我试图通过将行更改为来解决此问题:

GLuint program = LoadShaders(*shaders)

,这似乎奏效了一点。然而,在编译代码时,出现了一个错误( Unresolved external 问题)。我怀疑我早先尝试解决上述问题可能是原因,但在多次尝试代码无济于事之后,我决定问问知道他们在做什么的人。

错误信息

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 ==========

main.cpp

#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();
}

负载着色器.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

负载着色器.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__

vgl.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. 是的,我确实记得链接库(我想))

最佳答案

缺少 LoadShaders(struct ShaderInfo) 符号的错误表明您的 main.cpp 包含定义不同 LoadShaders 的内容原型(prototype)比我在您的 LoadShaders.h 中看到的要多。奇怪的是,也许是打字错误,您的 main.cpp 包含 LoadShader.h(缺少“s”),但实际 header 称为 LoadShaders.h 。也许巧合的是,LoadShader.h 位于某个地方,具有不同的函数原型(prototype)。

关于c++ - 红皮书示例 1.1 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17506627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com