作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 macOS 10.15.7 上使用 Xcode(版本 12.0.1)。
我用 Homebrew 安装了最新版本的 glew、glfw、glm,并尝试运行这个简单的代码片段,我的 OpenGL 版本似乎与使用的着色器不兼容。
我不想更改代码,有什么方法可以降级着色器? (也可以用 Homebrew 来完成吗?)
代码如下:
#include <iostream>
//#define GLEW_STATIC 1 // This allows linking with Static Library on Windows, without DLL
#include <GL/glew.h> // Include GLEW - OpenGL Extension Wrangler
#include <GLFW/glfw3.h> // GLFW provides a cross-platform interface for creating a graphical context,
// initializing OpenGL and binding inputs
#include <glm/glm.hpp> // GLM is an optimized math library with syntax to similar to OpenGL Shading Language
const char* getVertexShaderSource()
{
// Insert Shaders here ...
// For now, you use a string for your shader code, in the assignment, shaders will be stored in .glsl files
return
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec3 aColor;"
"out vec3 vertexColor;"
"void main()"
"{"
" vertexColor = aColor;"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);"
"}";
}
const char* getFragmentShaderSource()
{
return
"#version 330 core\n"
"in vec3 vertexColor;"
"out vec4 FragColor;"
"void main()"
"{"
" FragColor = vec4(vertexColor.r, vertexColor.g, vertexColor.b, 1.0f);"
"}";
}
int compileAndLinkShaders()
{
// compile and link shader program
// return shader program id
// ------------------------------------
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
const char* vertexShaderSource = getVertexShaderSource();
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const char* fragmentShaderSource = getFragmentShaderSource();
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
//return -1;
}
int createVertexArrayObject()
{
// A vertex is a point on a polygon, it contains positions and other data (eg: colors)
glm::vec3 vertexArray[] = {
glm::vec3( 0.0f, 0.5f, 0.0f), // top center position
glm::vec3( 1.0f, 0.0f, 0.0f), // top center color (red)
glm::vec3( 0.5f, -0.5f, 0.0f), // bottom right
glm::vec3( 0.0f, 1.0f, 0.0f), // bottom right color (green)
glm::vec3(-0.5f, -0.5f, 0.0f), // bottom left
glm::vec3( 0.0f, 0.0f, 1.0f), // bottom left color (blue)
};
// Create a vertex array
GLuint vertexArrayObject;
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
// Upload Vertex Buffer to the GPU, keep a reference to it (vertexBufferObject)
GLuint vertexBufferObject;
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexArray), vertexArray, GL_STATIC_DRAW);
glVertexAttribPointer(0, // attribute 0 matches aPos in Vertex Shader
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
2*sizeof(glm::vec3), // stride - each vertex contain 2 vec3 (position, color)
(void*)0 // array buffer offset
);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, // attribute 1 matches aColor in Vertex Shader
3,
GL_FLOAT,
GL_FALSE,
2*sizeof(glm::vec3),
(void*)sizeof(glm::vec3) // color is offseted a vec3 (comes after position)
);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0); // VAO already stored the state we just defined, safe to unbind buffer
glBindVertexArray(0); // Unbind to not modify the VAO
return vertexArrayObject;
}
int main(int argc, char*argv[])
{
// Initialize GLFW and OpenGL version
glfwInit();
#if defined(PLATFORM_OSX)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
// On windows, we set OpenGL version to 2.1, to support more hardware
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
#endif
// Create Window and rendering context using GLFW, resolution is 800x600
GLFWwindow* window = glfwCreateWindow(800, 600, "Comp371 - Lab 01", NULL, NULL);
if (window == NULL)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to create GLEW" << std::endl;
glfwTerminate();
return -1;
}
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Compile and link shaders here ...
int shaderProgram = compileAndLinkShaders();
// Define and upload geometry to the GPU here ...
int vao = createVertexArrayObject();
// Entering Main Loop
while(!glfwWindowShouldClose(window))
{
// Each frame, reset color of each pixel to glClearColor
glClear(GL_COLOR_BUFFER_BIT);
// TODO - draw rainbow triangle
glUseProgram(shaderProgram);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3); // 3 vertices, starting at index 0
glBindVertexArray(0);
// End frame
glfwSwapBuffers(window);
// Detect inputs
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// Shutdown GLFW
glfwTerminate();
return 0;
}
这是我遇到的错误
2021-01-22 21:23:23.336552-0500 OpenGLtest [27358:306573] Metal API Validation Enabled
2021-01-22 21:23:23.361027-0500 OpenGLtest [27358:306750] flock failed to lock maps file: errno = 35
2021-01-22 21:23:23.361368-0500 OpenGLtest [27358:306750] flock failed to lock maps file: errno = 35
ERROR::SHADER::VERTEX::COMPILATION_FAILED
ERROR: 0:1: '' : version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:2: 'layout' : syntax error: syntax error
ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
ERROR: 0:1: '' : version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:2: 'f' : syntax error: syntax error
ERROR::SHADER::PROGRAM::LINKING_FAILED
ERROR: One or more attached shaders not successfully compiled
Program ended with exit code: 9
如何解决这个问题?
最佳答案
您正在尝试在 GL 3.2 上下文中编译 #version 330
GLSL 代码。自 GL 3.2 only supports #version
s up to 150
那是行不通的。
要么重新编写您的 GLSL 以针对 #version 150
,要么请求 GL 3.3 上下文。
关于c++ - OpenGL - 为什么我会收到此错误?不支持 COMPILATION_FAILED 版本 '330',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65855509/
我在 macOS 10.15.7 上使用 Xcode(版本 12.0.1)。 我用 Homebrew 安装了最新版本的 glew、glfw、glm,并尝试运行这个简单的代码片段,我的 OpenGL 版
我是一名优秀的程序员,十分优秀!