Frage

I am trying to learn the different drawing commands used in OpenGL. After running this code all I get is a black screen. I do not get any errors compiling the shader code and I dont get any errors when calling glGetError()

#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;
}

Here is my GLSL code:

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;
}
War es hilfreich?

Lösung

There is a mismatch between your drawing code and the shaders. In the drawing code, you set a projection matrix:

glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, &projection_matrix[0][0]);

But your vertex shader does not apply a projection matrix. It does not even have this uniform defined.

If you apply only modelMatrix with the values from your setup code, without also using a projection, the entire geometry will be translated outside the clip boundaries.

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

This translates everything by -5.0f in z-direction. Since your original triangle coordinates are at z = 0.0f, they end up at -5.0f. Clipping happens at a z-range of -1.0f to 1.0f of the coordinates your vertex shader writes to gl_Position.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top