使用一个着色器程序,我的代码如下所示:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
InitFunctions();
glUseProgram(ShaderProgram);
//render objects
glutSwapBuffers();
但我想添加另一个着色器程序,但它似乎清除了屏幕。即使只是在渲染对象后放置它也会清除屏幕:
glUseProgram(ShaderProgramNew);
屏幕上没有清除屏幕的唯一时间是我在glUseProgram()的两次使用中使用相同的着色器
要加载着色器,我使用此功能:
GLuint ShaderProgramManager::loadProgram(char* VertexShaderFileLocation, char* FragmentShaderFileLocation){
cout << "Creating Shader...\n";
cout << "Initalizing Variables\n";
GLuint ShaderProgram = glCreateProgram();
GLuint VertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
string VString = loadShader(VertexShaderFileLocation);
string FString = loadShader(FragmentShaderFileLocation);
const GLchar* VText = (const GLchar*)VString.c_str();
const GLchar* FText = (const GLchar*)FString.c_str();
cout << "Load VertexShader and FragmentShader\n";
const GLchar* pp[1];
pp[0] = FText;
GLint Lengthss[1];
Lengthss[0] = strlen(FText);
glShaderSource(FragmentShader, 1, (const GLchar**)&pp, Lengthss);
glCompileShader(FragmentShader);
const GLchar* p[1];
p[0] = VText;
GLint Lengths[1];
Lengths[0] = strlen(VText);
glShaderSource(VertexShader, 1, (const GLchar**)&p, Lengths);
glCompileShader(VertexShader);
GLint success;
glGetShaderiv(FragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(FragmentShader, sizeof(InfoLog), NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", GL_FRAGMENT_SHADER, InfoLog);
}
glGetShaderiv(VertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(VertexShader, sizeof(InfoLog), NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", GL_VERTEX_SHADER, InfoLog);
}
cout << "Shaders Compiled.\n";
cout << "Attach Shaders\n";
glAttachShader(ShaderProgram, VertexShader);
glAttachShader(ShaderProgram, FragmentShader);
cout << "Link Program\n";
glLinkProgram(ShaderProgram);
cout << "Validate Program\n";
glValidateProgram(ShaderProgram);
cout << "Complete.\n";
return ShaderProgram;
}
而且:
string ShaderProgramManager::loadShader(char* location){
string textS = "";
string line;
ifstream myfile(location);
cout << "Start of shader tex:\n" << "\n";
while (getline(myfile, line))
{
//cout << line << "\n\n";
textS = textS + line + "\n";
}
cout << "End of shader text" << "\n";
//text = new char[sizeOfCharArray];
//strcat(text, textS.c_str());
myfile.close();
cout << "Shader Finished Loading.\n";
return textS;
}
在程序开始时我运行
ShaderProgramManager spm;
ShaderProgram = spm.loadProgram("data/Shaders/VertexShader.txt", "data/Shaders/FragmentShader.txt");
gWorldLocation = glGetUniformLocation(ShaderProgram, "gWorld");
rotmLocation = glGetUniformLocation(ShaderProgram, "rotm");
PermLocation = glGetUniformLocation(ShaderProgram, "persm");
答案 0 :(得分:4)
当我想在一次渲染过程中使用2个不同的着色器时,此解决方案对我来说非常适合。
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram01);
// Do Modelview and projection matrix stuff and uniforms
// Render objects with fstshaderProgram
glUseProgram(shaderProgram02);
// Do Modelview and projection matrix stuff and uniforms
// Render objects with 2nd shaderProgram
glutSwapBuffers();
非常重要的是你必须重新装上所有需要的制服。对于这种方法,我假设您正在正确加载着色器并且它们正在正常工作。 使用过剩,你每次迭代只调用一次glutSwapBuffers()。