2014-05-15 3 views
0

J'essaie d'apprendre les différentes commandes de dessin utilisées dans OpenGL. Après avoir exécuté ce code, tout ce que je reçois est un écran noir. Je ne reçois pas d'erreurs compilant le code shader et je n'obtiens des erreurs lors de l'appel glGetError()Erreur OpenGL inconnue

#include <iostream> 
#include <GL\glew.h> 
#include <GLFW\glfw3.h> 
#include <glm\glm.hpp> 
#include <glm\gtc\matrix_transform.hpp> 
#include <glm\gtx\transform.hpp> 
#include <common\shader.h> 
using namespace std; 

GLFWwindow* window; 
float aspect = 4.0f/3.0f; 

enum VAO_IDs { Triangles, NumVAOs }; 
enum Buffer_IDs { ArrayBuffer, NumBuffers }; 
enum Attrib_IDs { vPosition, vColor }; 
enum EBO_IDs { ElementBuffer, NumElementBuffers }; 

GLuint program; 
GLuint VAOs[NumVAOs]; 
GLuint Buffers[NumBuffers]; 
GLuint EBOs[NumElementBuffers]; 

GLint render_projection_matrix_loc; 
GLint render_model_matrix_loc; 

void init(void) 
{ 
program = LoadShaders("triangles.vert", "triangles.frag"); 
glUseProgram(program); 

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

GLfloat vertex_colors[] = 
{ 
    1.0f, 1.0f, 1.0f, 1.0f, 
    1.0f, 1.0f, 0.0f, 1.0f, 
    1.0f, 0.0f, 1.0f, 1.0f, 
    0.0f, 1.0f, 1.0f, 1.0f 
}; 

GLushort vertex_indices[] = 
{ 
    0, 1, 2 
}; 

glGenBuffers(NumElementBuffers, EBOs); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[ElementBuffer]); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW); 

glGenVertexArrays(NumVAOs, VAOs); 
glBindVertexArray(VAOs[Triangles]); 

glGenBuffers(NumBuffers, Buffers); 
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); 
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions) + sizeof(vertex_colors), NULL, GL_STATIC_DRAW); 
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex_positions), vertex_positions); 
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertex_positions), sizeof(vertex_colors), vertex_colors); 

glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, NULL); 
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(vertex_positions)); 
glEnableVertexAttribArray(vPosition); 
glEnableVertexAttribArray(vColor); 
} 

void display(void) 
{ 
glm::mat4 model_matrix; 

glEnable(GL_CULL_FACE); 
glDisable(GL_DEPTH_TEST); 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glUseProgram(program); 

glm::mat4 projection_matrix = glm::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 500.0f); 
glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, &projection_matrix[0][0]); 

glBindVertexArray(VAOs[Triangles]); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[ElementBuffer]); 

model_matrix = glm::translate(glm::vec3(-3.0f, 0.0f, -5.0f)); 
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, &model_matrix[0][0]); 
glDrawArrays(GL_TRIANGLES, 0, 3); 

model_matrix = glm::translate(glm::vec3(1.0f, 0.0f, -5.0f)); 
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, &model_matrix[0][0]); 
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL); 
} 

int main(int argc, char** argv) 
{ 
if (!glfwInit()) 
{ 
    cerr << "Failed to initialize GLFW\n"; 
    exit(EXIT_FAILURE); 
} 

glfwWindowHint(GLFW_SAMPLES, 4); 
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 

window = glfwCreateWindow(1024, 768, "C++ Graphics", NULL, NULL); 
if (window == NULL){ 
    cerr << "Failed to open GLFW window\n"; 
    glfwTerminate(); 
    exit(EXIT_FAILURE); 
} 
glfwMakeContextCurrent(window); 

glewExperimental = GL_TRUE; 
if (glewInit() != GLEW_OK) { 
    cerr << "Failed to initialize GLEW\n"; 
    exit(EXIT_FAILURE); 
} 

glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 
init(); 

do{ 
    display(); 
    glGetError(); 
    glGetError(); 

    glfwSwapBuffers(window); 
    glfwPollEvents(); 
} 
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && 
glfwWindowShouldClose(window) == 0); 

glfwTerminate(); 

return 0; 
} 

Voici mon code GLSL:

triangles.vert

#version 430 core 

layout(location = 0) in vec4 vPosition; 
layout(location = 1) in vec4 vColor; 

out vec4 fragmentColor; 

uniform mat4 modelMatrix; 

void main() 
{ 
    gl_Position = modelMatrix * vPosition; 
    fragmentColor = vColor; 
} 

triangles. frag

#version 430 core 

in vec4 fragmentColor; 

out vec4 color; 

void main() 
{ 
    color = fragmentColor; 
} 

Répondre

1

Il existe une discordance entre votre code de dessin et les shaders. Dans le code de dessin, vous définissez une matrice de projection:

Mais votre vertex shader n'applique pas de matrice de projection. Il n'a même pas cet uniforme défini.

Si vous n'appliquez que modelMatrix avec les valeurs de votre code d'installation, sans utiliser de projection, la géométrie entière sera traduite en dehors des limites de l'élément.

model_matrix = glm::translate(glm::vec3(-3.0f, 0.0f, -5.0f)); 
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, &model_matrix[0][0]); 

Cela traduit tout par -5.0f dans la direction z. Puisque les coordonnées de votre triangle initial sont à z = 0.0f, elles finissent à -5.0f. L'écrêtage se produit à une plage de -1.0f à 1.0f des coordonnées de votre vertex shader à gl_Position.