我试图在cpp中学习gl,并尝试创建一些着色器,以阅读其文档并遵循yt教程。 它只是编译失败。 昨天刚在控制台着色器“ + filePath +”中打印无法编译 今天打印:
错误:0:7:“ gl_position”:未声明的标识符
错误:0:7:'xy':字段选择需要左侧的结构,向量或矩阵 手侧 错误:0:7:'分配':无法从'highp float的2分量向量'转换 到“高浮点”
错误:0:8:'z':字段选择需要左侧的结构,向量或矩阵 手侧 错误:0:9:'w':字段选择需要在左侧的结构,向量或矩阵 手侧
Shader shaders / colorShading.vert无法编译 输入任何密钥以退出...
这是我的着色器源代码:
#version 130
in vec2 vertexPosition;
void main()
{
gl_position.xy = vertexPosition * 2;
gl_position.z = 0.0;
gl_position.w = 1.0;
}
///////////////////////////////////////////////// ///////
#include "GLSLProgram.h"
#include "Error.h"
#include <fstream>
#include <vector>
GLSLProgram::GLSLProgram() :_numAttributes(0), _programID(0), _vertexShaderID(0), _fragmentShaderID(0)
{
}
GLSLProgram::~GLSLProgram()
{
}
void GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& framgmentShaderFilePath)
{
_vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
if (_vertexShaderID == 0)
{
fatalError("Vertex shader failed to be created!");
}
_fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
if (_fragmentShaderID == 0)
{
fatalError("Fragment shader failed to be created!");
}
compileShader(vertexShaderFilePath, _vertexShaderID);
compileShader(framgmentShaderFilePath, _fragmentShaderID);
}
void GLSLProgram::linkShaders()
{
// Attach Shaders to Program
glAttachShader(_programID, _vertexShaderID);
glAttachShader(_programID, _fragmentShaderID);
// Link the Program.
glLinkProgram(_programID);
GLuint isLinked = 0;
glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
if (GL_FALSE == isLinked) {
GLint maxLength = 0;
glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<char> errorLog(maxLength);
glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
glDeleteProgram(_programID);
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
std::printf("%s\n", &(errorLog[0]));
fatalError("Shaders failed to link!");
return;
}
glDetachShader(_programID, _vertexShaderID);
glDetachShader(_programID, _fragmentShaderID);
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
}
void GLSLProgram::addAttribute(const std::string& attributeName)
{
glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
}
void GLSLProgram::use()
{
glUseProgram(_programID);
for (int i = 0; i < _numAttributes; i++)
{
glEnableVertexAttribArray(i);
}
}
void GLSLProgram::unuse()
{
glUseProgram(0);
for (int i = 0; i < _numAttributes; i++)
{
glDisableVertexAttribArray(i);
}
}
void GLSLProgram::compileShader(const std::string & filePath, GLuint& id)
{
std::ifstream fileptr(filePath);
if (fileptr.fail()) {
// Not sold on this
perror(filePath.c_str());
fatalError("Failed to Open " + filePath);
}
std::string fileContents = "";
std::string line;
while (std::getline(fileptr, line))
{
fileContents += line + "\n";
}
fileptr.close();
// This is weird
const GLchar * contentsPtr = fileContents.c_str();
glShaderSource(id, 1, &contentsPtr, 0);
glCompileShader(id);
GLint success = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
glDeleteShader(id);
std::printf("%s\n", &(errorLog[0]));
fatalError("Shader " + filePath + " failed to Compile");
}
}
答案 0 :(得分:1)
顶点着色器无法编译,因为它必须是gl_Position
而不是gl_position
。 GLSL区分大小写。参见Vertex Shader - Outputs:
void main()
{
gl_Position.xy = vertexPosition * 2;
gl_Position.z = 0.0;
gl_Position.w = 1.0;
}