gpt4 book ai didi

c++ - 多个对象无法渲染?

转载 作者:行者123 更新时间:2023-12-02 10:31:15 27 4
gpt4 key购买 nike

我应该在屏幕上显示两个方 block ,但是当我运行它时,我看到的只是一个虚无的屏幕。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>




struct Shaderprogramsource
{
std::string VertexSouce;
std::string FragmentSource;
};

static Shaderprogramsource Parseshader(const std::string& filepath)
{
std::ifstream stream(filepath);

enum class Shadertype
{
VERTEX = 0,
FRAGMENT = 1,
NONE = 5
};

std::string line;
std::stringstream ss[3];

Shadertype type = Shadertype::NONE;

while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
type = Shadertype::VERTEX;

else if (line.find("fragment") != std::string::npos)
type = Shadertype::FRAGMENT;
}
else
{
ss[(int)type] << line << "\n";
}
}

return Shaderprogramsource{ ss[0].str(), ss[1].str() };
}

static 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 << message;

return 0;
}

return id;
}

static unsigned int CreateShader(
const std::string& Vertexshader,
const std::string& Fragmentshader)
{
unsigned int program = glCreateProgram();
unsigned int vertex = CompileShader(GL_VERTEX_SHADER, Vertexshader);
unsigned int fragment = CompileShader(GL_FRAGMENT_SHADER, Fragmentshader);

glAttachShader(program, vertex);
glAttachShader(program, fragment);

glLinkProgram(program);
glValidateProgram(program);

return program;
}

int main(void)
{
GLFWwindow* window;

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

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






/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(800, 800, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

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

if (GLEW_OK == glewInit())
{
}

float vertices[] = { -0.1, -0.1,
0.1, -0.1,
0.1, 0.1,
-0.1, 0.1 };



float vertices2[] = { -0.1,0.1,
0.1, 0.1,
0.1, 0.3,
-0.1, 0.3 };


unsigned int indices[] = {

0, 1, 2,
2, 3, 0


};

unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

unsigned int vao2;
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);


unsigned int buffer1;

unsigned int vbo;

glGenBuffers(1, &buffer1);
glBindBuffer(GL_ARRAY_BUFFER, buffer1);
glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

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

glEnableVertexAttribArray(1);

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



glGenBuffers(1, &vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);





Shaderprogramsource source = Parseshader("res/shaders/Basic.Shader");

unsigned int shader =
CreateShader(source.VertexSouce, source.FragmentSource);




glUseProgram(shader);


int location = glGetUniformLocation(shader, "Color_u");
if (location > -1) {
glUniform4f(location, 0.8f, 0.0f, 0.0f, 1.0f);

}



std::cout << source.VertexSouce;

glUseProgram(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

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

glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(shader);
glBindBuffer(GL_ARRAY_BUFFER, vao);
glBindBuffer(GL_ARRAY_BUFFER, vao2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo);


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

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

glEnableVertexAttribArray(0);

glEnableVertexAttribArray(1);

glUseProgram(shader);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

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

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

glDeleteProgram(shader);

glfwTerminate();
return 0;
}

着色器:
#shader vertex
#version 330 core

layout(location = 0) in vec3 position;



void main()
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
};

#shader fragment
#version 330 core

layout(location = 0) out vec4 color;

uniform vec4 Color_u;

void main()
{
color = Color_u;
};

最佳答案

各种各样的笨蛋:

  • Parseshader()如果第一行以魔法 #shader 以外的任何内容开头,则会中断指令,因为 NONE设置为 5,ss[(int)type] index + assignment 将直接从 3 元素 ss 的末尾走开大批。放下NONE下至 2 .
  • 您的着色器中只有一个顶点属性( 0 )但您正在尝试设置 1 .
  • 这根本不是 VAO 的工作方式。 VAO 从来都不是 glBindBuffer() 的有效参数。 . Intead,绑定(bind)一个 VAO 然后设置你的顶点属性绑定(bind)/布局:
    unsigned int vao1 = 0;
    glGenVertexArrays(1, &vao1);
    glBindVertexArray(vao1);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBindBuffer(GL_ARRAY_BUFFER, buffer1);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);

    然后稍后当您要绘制时,您可以重新绑定(bind) VAO 并立即绘制,跳过所有 glBindBuffer()/glVertexAttribPointer()/glEnableVertexAttribArray()设置。

  • 全部一起:

    screenshot

    // g++ -g main.cpp `pkg-config --cflags --libs glfw3 glew`
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
    #include <iostream>
    #include <sstream>
    #include <string>

    const char* const shaderSource = R"GLSL(
    #shader vertex
    #version 330 core
    layout(location = 0) in vec3 position;
    void main()
    {
    gl_Position = vec4(position.x, position.y, position.z, 1.0);
    };

    #shader fragment
    #version 330 core
    layout(location = 0) out vec4 color;
    uniform vec4 Color_u;
    void main()
    {
    color = Color_u;
    };
    )GLSL";

    struct Shaderprogramsource
    {
    std::string VertexSouce;
    std::string FragmentSource;
    };

    static Shaderprogramsource Parseshader(const std::string& shaderString)
    {
    std::stringstream stream( shaderString );

    enum class Shadertype
    {
    VERTEX = 0,
    FRAGMENT = 1,
    NONE = 2
    };

    std::string line;
    std::stringstream ss[3];

    Shadertype type = Shadertype::NONE;

    while( std::getline(stream, line) )
    {
    if (line.find("#shader") != std::string::npos)
    {
    if (line.find("vertex") != std::string::npos)
    type = Shadertype::VERTEX;

    else if (line.find("fragment") != std::string::npos)
    type = Shadertype::FRAGMENT;
    }
    else
    {
    ss[(int)type] << line << '\n';
    }
    }

    return Shaderprogramsource{ ss[0].str(), ss[1].str() };
    }

    static 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 << message;

    return 0;
    }

    return id;
    }

    static unsigned int CreateShader( const std::string& Vertexshader, const std::string& Fragmentshader )
    {
    unsigned int program = glCreateProgram();
    unsigned int vertex = CompileShader(GL_VERTEX_SHADER, Vertexshader);
    unsigned int fragment = CompileShader(GL_FRAGMENT_SHADER, Fragmentshader);

    glAttachShader(program, vertex);
    glAttachShader(program, fragment);

    glLinkProgram(program);
    glValidateProgram(program);

    return program;
    }

    int main( int argc, char** argv )
    {
    if (!glfwInit())
    return -1;

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
    glfwTerminate();
    return -1;
    }

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

    if (GLEW_OK == glewInit())
    {
    }

    unsigned int buffer1;
    glGenBuffers(1, &buffer1);
    glBindBuffer(GL_ARRAY_BUFFER, buffer1);
    float vertices1[] =
    {
    -0.1, -0.1,
    0.1, -0.1,
    0.1, 0.1,
    -0.1, 0.1,
    };
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);

    unsigned int buffer2;
    glGenBuffers(1, &buffer2);
    glBindBuffer(GL_ARRAY_BUFFER, buffer2);
    float vertices2[] =
    {
    -0.1, 0.1,
    0.1, 0.1,
    0.1, 0.3,
    -0.1, 0.3,
    };
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);

    unsigned int ebo;
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    unsigned int indices[] =
    {
    0, 1, 2,
    2, 3, 0,
    };
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    unsigned int vao1 = 0;
    glGenVertexArrays(1, &vao1);
    glBindVertexArray(vao1);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBindBuffer(GL_ARRAY_BUFFER, buffer1);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);

    unsigned int vao2 = 0;
    glGenVertexArrays(1, &vao2);
    glBindVertexArray(vao2);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBindBuffer(GL_ARRAY_BUFFER, buffer2);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);

    Shaderprogramsource source = Parseshader( shaderSource );
    unsigned int shader = CreateShader(source.VertexSouce, source.FragmentSource);
    glUseProgram(shader);
    int location = glGetUniformLocation(shader, "Color_u");

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
    glfwPollEvents();

    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shader);

    if (location > -1)
    {
    glUniform4f(location, 0.8f, 0.0f, 0.0f, 1.0f);
    }
    glBindVertexArray(vao1);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

    if (location > -1)
    {
    glUniform4f(location, 0.0f, 0.8f, 0.0f, 1.0f);
    }
    glBindVertexArray(vao2);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

    glfwSwapBuffers(window);
    }

    glDeleteProgram(shader);

    glfwTerminate();
    return 0;
    }

    关于c++ - 多个对象无法渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62141165/

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