gpt4 book ai didi

macos - 加载顶点着色器时出错

转载 作者:行者123 更新时间:2023-12-01 11:47:15 26 4
gpt4 key购买 nike

我正在尝试 OpenGl 和计算机图形学的第一步。我找到了 this book我正在尝试遵循示例和理论,但是当我尝试编译项目时出现以下错误

vertex.glsl failed to compile:
ERROR: 0:1: '' : version '150' is not supported

顶点文件有这个:

#version 150

varying vec4 vPosition;

void main(){
gl_Position = vPosition;
}

我要执行的代码是:

#include "Angel.h"
#include <iostream>

const int numPoints = 5000;
typedef vec2 point2;

void init(){

point2 points[numPoints];
point2 vertices[3] = {
point2(-1.0, -1.0), point2(0.0, 1.0), point2(1.0, -1.0)
};

points[0] = point2(0.25, 0.5);

for (int k = 1; k < numPoints; k++) {
int j = rand()%3;
points[k] = (points[k-1]+vertices[j])/2.0;
}

GLuint program = InitShader("vertex.glsl", "fragment.glsl");
glUseProgram(program);

GLuint abuffer;
glGenVertexArraysAPPLE(1, &abuffer);
glBindVertexArrayAPPLE(abuffer);

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

GLuint location = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(location);
glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

glClearColor(1.0, 1.0, 1.0, 1.0);

}


void display(){
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, numPoints);
glFlush();
}

int main(int argc, char** argv){

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA);

glutInitWindowSize(640, 480);

glutCreateWindow("Sierpinski Gasket");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;
}

我发现问题出在 InitShader("vertex.glsl", "fragment.glsl"); 函数中,它是这样的:

#include "Angel.h"

namespace Angel {

// Create a NULL-terminated string by reading the provided file
static char*
readShaderSource(const char* shaderFile)
{
FILE* fp = fopen(shaderFile, "r");

if ( fp == NULL ) { return NULL; }

fseek(fp, 0L, SEEK_END);
long size = ftell(fp);

fseek(fp, 0L, SEEK_SET);
char* buf = new char[size + 1];
fread(buf, 1, size, fp);

buf[size] = '\0';
fclose(fp);

return buf;
}


// Create a GLSL program object from vertex and fragment shader files
GLuint
InitShader(const char* vShaderFile, const char* fShaderFile)
{
struct Shader {
const char* filename;
GLenum type;
GLchar* source;
} shaders[2] = {
{ vShaderFile, GL_VERTEX_SHADER, NULL },
{ fShaderFile, GL_FRAGMENT_SHADER, NULL }
};

GLuint program = glCreateProgram();

for ( int i = 0; i < 2; ++i ) {
Shader& s = shaders[i];
s.source = readShaderSource( s.filename );
if ( shaders[i].source == NULL ) {
std::cerr << "Failed to read " << s.filename << std::endl;
exit( EXIT_FAILURE );
}

GLuint shader = glCreateShader( s.type );
glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
glCompileShader( shader );

GLint compiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled ) {
std::cerr << s.filename << " failed to compile:" << std::endl;
GLint logSize;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
char* logMsg = new char[logSize];
glGetShaderInfoLog( shader, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;

exit( EXIT_FAILURE );
}

delete [] s.source;

glAttachShader( program, shader );
}

/* link and error check */
glLinkProgram(program);

GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if ( !linked ) {
std::cerr << "Shader program failed to link" << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;

exit( EXIT_FAILURE );
}

/* use program object */
glUseProgram(program);

return program;
}

} // Close namespace Angel block

完整的源代码可以在本书的链接中找到。这个问题是怎么回事?我对着色器一无所知,我只是想让这个应用程序正常工作。我在装有 OS X 10.8、Xcode 4.5 的 Mac 上执行此操作

最佳答案

仔细检查您的实现的 GL_VERSIONGL_SHADING_LANGUAGE_VERSION 是否符合您的预期。

OSX 可能不会/可能不会为您提供带有不合格 glutInitDisplayMode(GLUT_RGBA) 的 GL 3.2 核心上下文。尝试在其中打开一个 GLUT_3_2_CORE_PROFILE:

glutInitDisplayMode(GLUT_RGBA | GLUT_3_2_CORE_PROFILE);

关于macos - 加载顶点着色器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14527025/

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