GLFW程序不绘制三角形

时间:2018-04-15 21:52:58

标签: c++ opengl glfw

我正在运行一个简单的程序来绘制一个红色三角形。 但是,它只显示一个黑色的窗口。 我怀疑它可能是着色器的问题,因为如果我删除着色器,它会完美地绘制一个白色三角形。

这是我的代码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>

static unsigned int CompileShader(unsigned int type, const std::string& source){

unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);

int res;
glGetShaderiv(id, GL_COMPILE_STATUS, &res);

if(res == GL_FALSE){

    int length;
    glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

    char* message = (char*)alloca(length * sizeof(char));
    glGetShaderInfoLog(id, length, &length, message);
    std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" << std::endl;
    std::cout << message << std::endl;
    glDeleteShader(id);
    return 0;
}

return id;
}


static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader){

unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER , vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER , fragmentShader);

glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);

glDeleteShader(vs);
glDeleteShader(fs);

return program;
}



int main(void)
{

GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
    return -1;

glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 1 );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );


/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "I Rock", NULL, NULL);

if (!window)
{
    glfwTerminate();
    return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);



if(glewInit() != GLEW_OK)
    std::cout<< "Error!" << std::endl;


float positions[6] = {
    -0.5f, -0.5f,
     0.0f,  0.5f,
     0.5f, -0.5f
};


unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER,  6*sizeof(float), positions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0);

#define GLSL(version, shader) "#version " #version "\n" #shader

const char* vertexShader = GLSL
(
 410 core,
 layout( location = 0 ) in vec4 position;
 void main()
 {
     gl_Position = position;
 }
 );

const char* fragmentShader = GLSL
(
 410 core,
 out vec4 color;
 void main()
 {
     color = vec4( 1.0, 0.0, 0.0, 1.0 );
 }
 );

unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);

/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
    /* Render here */
    glClear(GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    /* Swap front and back buffers */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    glfwPollEvents();
}

glDeleteProgram(shader);

glfwTerminate();
return 0;
}

这些是我的规格:

  • GLFW版本:3.2.1 Cocoa NSGL chdir菜单栏视网膜动态
  • GLEW_VERSION:2.1.0
  • GL_VERSION:4.1 ATI-1.60.26
  • GL_VENDOR:ATI Technologies Inc.
  • GL_RENDERER:AMD Radeon R9 M290 OpenGL引擎
  • GL_SHADING_LANGUAGE_VERSION:4.10

1 个答案:

答案 0 :(得分:1)

你需要生成&amp;在启用顶点属性,设置顶点属性指针和绘图之前绑定Vertex Array Object (VAO)

...
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER,  6*sizeof(float), positions, GL_STATIC_DRAW);

// generate & bind VAO
GLuint vao = 0;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0);
...

VAO在Core上下文中不是可选的,就像它们在兼容性中一样。