我正在制作一个简单的程序来渲染三角形。程序从文件中读取顶点着色器和片段着色器并编译它们。然而,其中一个着色器 - 但从来都不是 - 都不会编译。
这是我的主文件的源代码:
//glew
#define GLEW_STATIC
#include <GL/glew.h>
//GLFW
#include <GLFW/glfw3.h>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
//Read shaders from file
const GLchar* readfile(std::string filename){
std::ifstream file (filename, std::ifstream::in);
std::stringstream buffer;
buffer << file.rdbuf();
std::string dataString = buffer.str();
const char *dataCString = dataString.c_str();
const GLchar *data = const_cast<GLchar*>(dataCString);
file.close();
return data;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode){
//when user presses the escape key WindowShouldClose is set to true closing the application
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
void render(){
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0,3);
}
int main(){
//initialize glfw with necessary information
glfwInit();
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);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//create a window with glfw
GLFWwindow* window = glfwCreateWindow(800, 600, "opengl window", nullptr, nullptr);
glfwMakeContextCurrent(window);
//set required key callback functions
glfwSetKeyCallback(window, key_callback);
//initialize glew
glewExperimental = GL_TRUE;
glewInit();
//specify size of rendering window
glViewport(0, 0, 800, 600);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
float vertices[]={
0.0f, 0.5f, //Vertex 1 (X, Y)
0.5f, -0.5f, //Vertex 2 (X, Y)
-0.5f, -0.5f //Vertex 3 (X, Y)
};
GLuint vbo;
glGenBuffers(1, &vbo); //Generate 1 buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
const GLchar* vertexSource = readfile("vertexShader.glsl");
const GLchar* fragmentSource = readfile("fragmentShader.glsl");
//Compile vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
//Check vertex shader compiled sucessfully
GLint vertexStatus;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexStatus);
std::cout << "Vertex shader compiled: " << vertexStatus << std::endl;
char buffer[1024];
glGetShaderInfoLog(vertexShader, 1024, NULL, buffer);
std::cout << buffer << std::endl;
//Compile fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
//Check fragment shader compiled sucessfully
GLint fragmentStatus;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentStatus);
std::cout << "Fragment shader compiled: " << fragmentStatus << std::endl;
glGetShaderInfoLog(fragmentShader, 1024, NULL, buffer);
std::cout << buffer << std::endl;
//Compile shaders into program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
//game loop
while(!glfwWindowShouldClose(window)){
//check and call events
glfwPollEvents();
//rendering goes here
render();
//swap the buffers
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
这是fragmentShader.glsl:
#version 150 core
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
这是vertexShader.glsl
#version 150 core
in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
当其中一个着色器无法编译时,我收到此错误,并且我99.99%表示我的第一行没有可能导致此问题的垃圾字符。
0:1(1): error: syntax error, unexpected $end
答案 0 :(得分:3)
dataString
readfile()
只有{{1}}中的本地生命周期,因此只要函数剩下,您返回的指针就会无效。