0

Im trying to learn a gl in cpp and trying to create some shaders reading its documentation and following a yt tutorial. It just had a compilation fail. yestarday it was just printing in console Shader " + filePath + " failed to Compile Today it printing that :

ERROR: 0:7: 'gl_position' : undeclared identifier
ERROR: 0:7: 'xy' : field selection requires structure, vector, or matrix on left hand side ERROR: 0:7: 'assign' : cannot convert from '2-component vector of highp float' to 'highp float'
ERROR: 0:8: 'z' : field selection requires structure, vector, or matrix on left hand side ERROR: 0:9: 'w' : field selection requires structure, vector, or matrix on left hand side

Shader shaders/colorShading.vert failed to Compile Enter any key to quit...

this is my shader source code:

#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"); } } 
3
  • 2
    The shader is failing to compile, but we don't have its source code... Commented Nov 16, 2018 at 9:29
  • 3
    "ERROR: 0:7: 'gl_position' ", it has to be gl_Position, GLSL is case sensitive Commented Nov 16, 2018 at 9:37
  • 1
    Indeed, just a typo: khronos.org/opengl/wiki/Built-in_Variable_(GLSL) Commented Nov 16, 2018 at 9:39

1 Answer 1

3

The vertex shader failed to compile, because it has to be gl_Position instead of gl_position. GLSL is case sensitive. See Vertex Shader - Outputs:

void main() { gl_Position.xy = vertexPosition * 2; gl_Position.z = 0.0; gl_Position.w = 1.0; } 
Sign up to request clarification or add additional context in comments.

5 Comments

You are right! Thank you. now it says: ERROR: 0:8: 'constructor' : too many arguments Shader shaders/colorShading.frag failed to Compile Enter any key to quit...
@IliasKarypidis This is an error in the fragment shader. Pleas show your fragment shader too
now the problem is solved but i get and ms visual c++ runtime library error. debug assertion failed! expression:vector subscrit out of range
can anyone help me ?
@IliasKarypidis No, not without any specific information. "debug assertion failed" can be anywhere in your program. You have to debug your program. Where happens the debug assertion. Apart from that, that's a new question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.