GLSL frag shader error

After finally getting rid of linker errors from the last post (it was a 32/64 bit mismatch), I successfully compiled a program from opengl-tutorial.org . I got to adding color, but when I run the program an error pops up in the shader info log:

Fragment shader failed to compile with the following errors
ERROR: 0:1: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 1 compilation errors.  No code generated


My C++ code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
using namespace glm;

GLfloat vao_data [] =
{
	0.0f,1.0f,0.0f,
	-1.0f,-1.0f,0.0f,
	1.0f,-1.0f,0.0f
};

const char* loadShader(string path)
{
	string out = "";

	ifstream in(path, ios::in);
	string line = "";
	if (in.is_open())
	{
		while (getline(in, line))
		{
			out += line + "\n";
		}

		in.close();
	}
	return out.c_str();
}

int main()
{
	if (!glfwInit())
	{
		cerr << "Cannot initialize GLFW." << endl;
		return -1;
	}

	glfwWindowHint(GLFW_SAMPLES, 8);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

	GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL tutorial", nullptr, nullptr);
	if (window == nullptr)
	{
		cerr << "Failed to open window/create context. Please check if your GPU supports OpenGL 3.3." << endl;
		glfwTerminate();
		return -2;
	}
	glfwMakeContextCurrent(window);
	glewExperimental = GL_TRUE;
	if (glewInit() != GLEW_OK)
	{
		cerr << "Cannot initialize GLEW." << endl;
		return -3;
	}

	GLuint vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	GLuint vertexBuffer;
	glGenBuffers(1, &vertexBuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vao_data), vao_data, GL_STATIC_DRAW);

	const char* vsSource = loadShader("vertex_shader.glsl");
	const char* fsSource = loadShader("frag_shader.glsl");

	GLuint vs, fs, program;
	vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, &vsSource, nullptr);
	glCompileShader(vs);
	char vsLog[512];
	glGetShaderInfoLog(vs, sizeof(vsLog), nullptr, vsLog);
	cout << "Vertex shader compile log:" << endl;
	cout << vsLog << endl;

	fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, &fsSource, nullptr);
	glCompileShader(fs);
	char fsLog[512];
	glGetShaderInfoLog(fs, sizeof(fsLog), nullptr, fsLog);
	cout << "Fragment shader compile log:" << endl;
	cout << fsLog << endl;

	program = glCreateProgram();
	glAttachShader(program, vs);
	glAttachShader(program, fs);
	glLinkProgram(program);

	char programLog[512];
	glGetProgramInfoLog(program, sizeof(programLog), nullptr, programLog);

	cout << "Program link log:" << endl;
	cout << programLog << endl;

	// game loop
	do
	{
		// draw
		glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glUseProgram(program);
		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		glDisableVertexAttribArray(0);

		// swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	} while (!glfwWindowShouldClose(window));

	return 0;
}


GLSL vertex shader code:
1
2
3
4
5
6
7
8
9
#version 330 core

layout(location=0) in vec3 vertexPosition_modelspace;

void main()
{
	gl_Position.xyz=vertexPosition_modelspace;
	gl_Position.w=1.0f;
} 


GLSL fragment shader code:
1
2
3
4
5
6
#version 330 core
out vec3 color;
void main()
{
	color=vec3(1.0f,0.0f,0.0f);
}


Is this an ifstream parsing error (I doubt that, the vs compiles perfectly), or a fragment shader error?

Thanks in advance.
The pointer you're returning has been freed by the time it's used.

Consider this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <iostream>
const char* test()
{
    std::string a = "asd";
    return a.c_str();
}

int main()
{
    std::cout << test() << std::endl;
}

By the standard, the results are undefined, because once test() returns, the data pointed to by std::string a have been freed.
Return a std::string and work it out like this:
1
2
3
4
5
6
7
8
9
10
11
std::string loadShader(string path)
{
	// ...
	return out;
}

//
	std::string vsSourceD = loadShader("vertex_shader.glsl");
	const char* vsSource = vsSourceD.c_str();
	std::string fsSourceD = loadShader("frag_shader.glsl");
	const char* fsSource = fsSourceD.c_str();
Last edited on
Thank you very much!

Everything is working perfectly:

Vertex shader compiled successfully to run on hardware.
Fragment shader compiled successfully to run on hardware.
Vertex shaders linked, fragment shaders linked.


:-)
Registered users can post here. Sign in or register to post.