gpt4 book ai didi

c++ - OpenGL 不会绘制三角形,遵循精确的格式

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:25 25 4
gpt4 key购买 nike

我能够创建一个窗口并且一切正常,但是当我尝试制作一个三角形时,它不会显示。

我已经创建了一个缓冲区并绑定(bind)它,还制作了我的着色器,所以我不明白为什么它不起作用。

我正在使用 MacOSX 和 Xcode。

#include <iostream>

using namespace std;
//GLEW
#define GLEW_STATIC
#include <GL/glew.h>

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

const GLint WIDTH = 800, HEIGHT = 600;


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 result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);

if (result == 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)
{
//glfwInit();


glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

GLFWwindow * window;

if (!glfwInit())
return -1;

window = glfwCreateWindow( WIDTH, HEIGHT, "Learn OpenGL", NULL, NULL);

int screenWidth, screenHeight;
glfwGetFramebufferSize( window, &screenWidth, & screenHeight);

if (!window)
{
cout << "Failed to create" << endl;
glfwTerminate();

return -1;
}

glfwMakeContextCurrent( window );

glewExperimental = GL_TRUE;

if ( glewInit() != GLEW_OK )
{
cout << "Failed to Initialize Glew" << endl;

return -1;
}

glViewport ( 0, 0, screenWidth, screenHeight);

//vertex buffer

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, (sizeof(float)*2), 0);


std::string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";

std::string fragmentShader =
"#version 330 core\n"
"\n"
" layout(location = 0) out vec4 color;"
"\n"
"void main()\n"
"{\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

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

while ( !glfwWindowShouldClose ( window ))
{

glClearColor(0.2f, 0.3F, 0.3, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );


glDrawArrays(GL_TRIANGLES, 0, 3);


glfwSwapBuffers( window );
glfwPollEvents();

}

glfwTerminate( );
return 0;

}

最佳答案

几件事:

  1. 在调用 glfwWindowHint() 之前调用 glfwInit()。否则它不会有任何效果,你会(可能)以 a Compatibility context instead of the Core context 结束。你在问。

    如果你设置an error callback :

    int main(void)
    {
    glfwSetErrorCallback( []( int, const char* str )
    {
    std::cerr << "GLFW error: " << str << std::endl;
    });

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

    if (!glfwInit())
    return -1;
    ...

    ...您可以看到调用 (most) 导致的 GLFW 错误流glfwInit() 之前的 GLFW 函数:

    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
  2. Vertex Array Objects (VAOs)在核心上下文中不是可选的。

    在尝试启用/设置顶点属性和绘图之前创建并绑定(bind)一个 VAO:

    ...
    GLuint vao = 0;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    glEnableVertexAttribArray(0);
    ...

一起:

#include <iostream>

using namespace std;
//GLEW
#define GLEW_STATIC
#include <GL/glew.h>

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

const GLint WIDTH = 800, HEIGHT = 600;


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 result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);

if (result == 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)
{
if (!glfwInit())
return -1;

glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

GLFWwindow * window;
window = glfwCreateWindow( WIDTH, HEIGHT, "Learn OpenGL", NULL, NULL);

int screenWidth, screenHeight;
glfwGetFramebufferSize( window, &screenWidth, & screenHeight);

if (!window)
{
cout << "Failed to create" << endl;
glfwTerminate();

return -1;
}

glfwMakeContextCurrent( window );

glewExperimental = GL_TRUE;

if ( glewInit() != GLEW_OK )
{
cout << "Failed to Initialize Glew" << endl;

return -1;
}

glViewport ( 0, 0, screenWidth, screenHeight);

//vertex buffer

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);

GLuint vao = 0;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

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


std::string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";

std::string fragmentShader =
"#version 330 core\n"
"\n"
" layout(location = 0) out vec4 color;"
"\n"
"void main()\n"
"{\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

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

while ( !glfwWindowShouldClose ( window ))
{

glClearColor(0.2f, 0.3F, 0.3, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );


glDrawArrays(GL_TRIANGLES, 0, 3);


glfwSwapBuffers( window );
glfwPollEvents();

}

glfwTerminate( );
return 0;
}

关于c++ - OpenGL 不会绘制三角形,遵循精确的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764162/

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