我应该在输出上看到一个白色三角形,但我只是看到一个黑屏。我认为问题在于glsl着色器。我的代码有什么问题?
#define GLEW_STATIC
#include <iostream>
#include <GL/glew.h>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
// Shader sources
const char* vertexSource = R"glsl(
#version 330 core
in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
)glsl";
const char* fragmentSource = R"glsl(
#version 330 core
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
)glsl";
int main()
{
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 2;
settings.majorVersion = 3;
settings.minorVersion = 2;
settings.attributeFlags = sf::ContextSettings::Core;
sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Titlebar | sf::Style::Close, settings);
// Initialize GLEW
glewExperimental = GL_TRUE;
glewInit();
//generate a vao
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//generate 1 buffer
GLuint vbo;
glGenBuffers(1, &vbo);
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)
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//compiling the vertex shader
GLuint vertexshader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexshader, 1, &vertexSource, NULL);
glCompileShader(vertexshader);
GLint shaderstatus;
glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &shaderstatus);
char buffer[512];
glGetShaderInfoLog(vertexshader, 512, NULL, buffer);
while (shaderstatus = true){
cout << "vertex" << endl;
cout << buffer << endl;
break;
}
//compiling the fragment shader
GLuint fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentshader, 1, &fragmentSource, NULL);
glCompileShader(fragmentshader);
GLint fragmentstatus;
glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &fragmentstatus);
char bufferfrag[512];
glGetShaderInfoLog(fragmentshader, 512, NULL, bufferfrag);
while (fragmentstatus = true){
cout << "fragment" << endl;
cout << bufferfrag << endl;
break;
}
//connecting vertex and fragment shaders
GLuint shaderprogram = glCreateProgram();
glAttachShader(shaderprogram, vertexshader);
glAttachShader(shaderprogram, fragmentshader);
glBindFragDataLocation(shaderprogram, 0, "outColor");
glLinkProgram(shaderprogram);
glUseProgram(shaderprogram);
GLint posAttrib = glGetAttribLocation(shaderprogram, "Position");
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posAttrib);
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
}
// Clear screen
window.clear();
//window.clear();
glDrawArrays(GL_TRIANGLES, 0, 3);
// Update the window
window.display();
}
return 0;
}
最佳答案
在行:
GLint posAttrib = glGetAttribLocation(shaderprogram, "Position");
由于GLSL区分大小写,因此“位置”中的p应该小写。
另外,在:
while (shaderstatus = true){
cout << "vertex" << endl;
cout << buffer << endl;
break;
}
应该有两个相等的符号,因为这是为了测试相等性,而不是将shaderstatus设置为true。