gpt4 book ai didi

c++ - GLFW程序不画三角形

转载 作者:行者123 更新时间:2023-11-30 05:02:33 25 4
gpt4 key购买 nike

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

这是我的代码:

#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 menubar retina dynamic
  • GLEW_VERSION:2.1.0
  • GL_VERSION:4.1 ATI-1.60.26
  • GL_VENDOR:ATI 技术公司
  • GL_RENDERER:AMD Radeon R9 M290 OpenGL 引擎
  • GL_SHADING_LANGUAGE_VERSION:4.10

最佳答案

您需要生成并绑定(bind)一个 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 在核心上下文中不像在兼容性中那样是可选的。

关于c++ - GLFW程序不画三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49847168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com