我正在使用openGL的几何着色器,并在进行更复杂的任务之前做一些测试以掌握它。我写了一些代码将输入的GL_POINTS转换为GL_TRIANGLE_STRIPs,然后在Geometry Shader中应用MVP矩阵变换。虽然没有MVP矩阵的渲染会产生正确的输出,但渲染它不会产生任何效果。这是代码(希望足够小):
int main(){
// Create context and window
sf::Context context;
sf::Window window(sf::VideoMode(800, 600), "Voxel Engine");
window.setVerticalSyncEnabled(true);
// Initialize glew
glewExperimental = true;
GLenum err = glewInit();
if (err != GLEW_OK){
std::cout << "glewIniti failed, aborting...\n";
}
// Create VAO
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Vector for verts
std::vector<glm::vec3> vertices;
// Insert a few vertices
for (int i = -5; i < 5; i++){
vertices.push_back(glm::vec3(i*.3, 0.0, 0.0));
}
// View matrix vectors
glm::vec3 eye = glm::vec3(0.0f, 5.0f, 5.0f);
glm::vec3 at = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
// Create VBO for vertices
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
// Create MVP matrices
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 View = glm::lookAt(eye, at, up);
glm::mat4 Model = glm::mat4(1.0f);
// Load the shaders
GLuint programID = LoadShaders("VertexShader.glsl", "GeometryShader.glsl", "FragmentShader.glsl");
// Get uniform locations
GLuint M = glGetUniformLocation(programID, "M");
GLuint V = glGetUniformLocation(programID, "V");
GLuint P = glGetUniformLocation(programID, "P");
// Main loop
bool running = true;
sf::Event event;
while (running){
while (window.pollEvent(event)){
switch (event.type){
case sf::Event::Closed:
window.close();
return 0;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glUniformMatrix4fv(M, 1, GL_FALSE, &Model[0][0]);
glUniformMatrix4fv(V, 1, GL_FALSE, &View[0][0]);
glUniformMatrix4fv(P, 1, GL_FALSE, &Projection[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_POINTS, 0, 10);
glDisableVertexAttribArray(0);
window.display();
}
return 0;
}
顶点着色器:
#version 430 core
layout(location = 0) in vec3 position;
void main(){
gl_Position = vec4(position, 1);
}
几何着色器:
#version 430 core
layout (points) in;
uniform mat4 M, V, P;
layout (triangle_strip) out;
layout (max_vertices = 14) out;
void main(void){
// create a vertex for each point on the cube
vec4 v1 = M*V*P*(gl_in[0].gl_Position + vec4(.1, -.1, -.1, 0.0));
vec4 v2 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, -.1, -.1, 0.0));
vec4 v3 = M*V*P*(gl_in[0].gl_Position + vec4(.1, .1, -.1, 0.0));
vec4 v4 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, .1, -.1, 0.0));
vec4 v5 = M*V*P*(gl_in[0].gl_Position + vec4(.1, -.1, .1, 0.0));
vec4 v6 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, -.1, .1, 0.0));
vec4 v7 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, .1, .1, 0.0));
vec4 v8 = M*V*P*(gl_in[0].gl_Position + vec4(.1, .1, .1, 0.0));
gl_Position = v4;
EmitVertex();
gl_Position = v3;
EmitVertex();
gl_Position = v7;
EmitVertex();
gl_Position = v8;
EmitVertex();
gl_Position = v5;
EmitVertex();
gl_Position = v3;
EmitVertex();
gl_Position = v1;
EmitVertex();
gl_Position = v4;
EmitVertex();
gl_Position = v2;
EmitVertex();
gl_Position = v7;
EmitVertex();
gl_Position = v6;
EmitVertex();
gl_Position = v5;
EmitVertex();
gl_Position = v2;
EmitVertex();
gl_Position = v1;
EmitVertex();
EndPrimitive();
}
Fragment Shader:
#version 430 core
out vec4 color;
void main(){
color = vec4(0, 1, 0, 1);
}
如有必要,我可以提供我的LoadShaders功能,但我不认为问题存在于那里。
所以,除了性能之外,还有我做过的任何明显错误的事情吗?