तो मैं एक बुनियादी कार्यक्रम लिखने की कोशिश कर रहा हूं जो स्क्रीन पर क्वाड प्रदर्शित करता है। और कोई फर्क नहीं पड़ता कि मैं क्या करता हूं यह सिर्फ एक काली स्क्रीन दिखाता है। मैंने पुस्तकालयों की जाँच की, सब कुछ जुड़ा हुआ था। मैंने कोड की जाँच की, सब कुछ ठीक लग रहा था (लेकिन मुझे पता है कि यह नहीं है) लेकिन सबसे बेतुका है: जब मैं एक ही प्रोग्राम में एक ही प्रोग्राम की तरह एक ही कंप्यूटर पर काम करता हूं तो यह काम करता है, और जब मैं एक को फिर से पेस्ट करता हूं, तो यह काम नहीं करता, फिर से।

वैसे - मैंने इसे ऑनलाइन टेक्स्ट डिफरेंस फाइंडर के माध्यम से चलाने की भी कोशिश की ... इससे पता चला कि यह वही बात थी।

मूल कोड (काम नहीं कर रहा):

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

static unsigned int CompileShader(const std::string& source, unsigned int type) {
    unsigned int shader = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(shader, 1, &src, nullptr);
    glCompileShader(shader);
    int result;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
    // i - int
    // v - vector
    if (result == GL_FALSE) {
        std::cout << "Shader cannot be compiled.\n" << (type == GL_VERTEX_SHADER ? "vertex s." : "fragments.") << "\n";
        int length;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)_malloca(length * sizeof(char));
        glGetShaderInfoLog(shader, length, &length, message);
        std::cout << message << "\n";
        glDeleteShader(shader);
    }
    return shader;
}
static int CreateShader(const std::string& vShader, const std::string& fShader) {
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(vShader, GL_VERTEX_SHADER);
    unsigned int fs = CompileShader(fShader, GL_FRAGMENT_SHADER);
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);
    glDeleteShader(vs);
    glDeleteShader(fs);
    return program;
}
int main() {
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(1280, 720, "CITY ENGINE", 0, 0);
    glfwMakeContextCurrent(window);
    glewInit();
    float vertices[8] = { 0.1f, 0.1f,
    -0.1f, 0.1f,
    -0.1f, -0.1f,
    0.1f, -0.1f };
    unsigned int varray;
    glGenVertexArrays(1, &varray);
    glBindVertexArray(varray);
    unsigned int vbuffer;
    glGenBuffers(1, &vbuffer);
    glBindBuffer(vbuffer, GL_ARRAY_BUFFER);
    glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);
    unsigned int indices[6] = { 0, 1, 2,
    2, 3, 0 };
    unsigned int ibuffer;
    glGenBuffers(1, &ibuffer);
    glBindBuffer(ibuffer, GL_ELEMENT_ARRAY_BUFFER);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
    unsigned int shader = CreateShader(
        "#version 330 core\n"
        "\n"
        "layout(location = 0) in vec4 position;\n"
        "\n"
        "void main()\n"
        "{\n"
        "gl_Position = position;\n"
        "};\n"
        ,
        "#version 330 core\n"
        "\n"
        "layout(location = 0) out vec4 color;\n"
        "uniform vec4 u_Color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "   color = u_Color;\n"
        "};\n"
    );
    glUseProgram(shader);
    int location = glGetUniformLocation(shader, "u_Color");
    glUniform4f(location, 0.3f, 0.3f, 1.0f, 1.0f);
    glBindVertexArray(0);
    glUseProgram(0);
    glBindBuffer(0, GL_ARRAY_BUFFER);
    glBindBuffer(0, GL_ELEMENT_ARRAY_BUFFER);
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(varray);
        glBindBuffer(ibuffer, GL_ELEMENT_ARRAY_BUFFER);
        glUseProgram(shader);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

काम करने वाला:

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

static unsigned int CompileShader(const std::string& source, unsigned int type) {
    unsigned int shader = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(shader, 1, &src, nullptr);
    glCompileShader(shader);
    int result;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
    // i - int
    // v - vector
    if (result == GL_FALSE) {
        std::cout << "Shader cannot be compiled.\n" << (type == GL_VERTEX_SHADER ? "vertex s." : "fragments.") << "\n";
        int length;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)_malloca(length * sizeof(char));
        glGetShaderInfoLog(shader, length, &length, message);
        std::cout << message << "\n";
        glDeleteShader(shader);
    }
    return shader;
}
static int CreateShader(const std::string& vShader, const std::string& fShader) {
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(vShader, GL_VERTEX_SHADER);
    unsigned int fs = CompileShader(fShader, GL_FRAGMENT_SHADER);
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);
    glDeleteShader(vs);
    glDeleteShader(fs);
    return program;
}
int main() {
    glfwInit();
    GLFWwindow* window;
    window = glfwCreateWindow(640, 480, "Modern OpenGL Test", NULL, NULL);
    glfwMakeContextCurrent(window);
    glewInit();
    float vertices[] = {
        -0.5f, -0.5f,
         0.5f, -0.5f,
         0.5f,  0.5f,
         -0.5f,  0.5f,
    };
    unsigned int indices[] = {
        0, 1, 2,
        2, 3, 0
    };
    unsigned int varray;
    glGenVertexArrays(1, &varray);
    glBindVertexArray(varray);
    unsigned int vb;
    glGenBuffers(1, &vb);
    glBindBuffer(GL_ARRAY_BUFFER, vb);
    glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);
    unsigned int ib;
    glGenBuffers(1, &ib);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
    unsigned int shaders = CreateShader(
     "#version 330 core\n"
     "\n"
     "layout(location = 0) in vec4 position;\n"
     "\n"
     "void main()\n"
     "{\n"
     "   gl_Position = position;\n"
     "};\n"
        ,
     "#version 330 core\n"
     "\n"
     "layout(location = 0) out vec4 color;\n"
     "\n"
     "uniform vec4 u_Color;\n"
     "\n"
     "void main()\n"
     "{\n"
     "  color = u_Color;\n"
     "};\n");
    glUseProgram(shaders);
    int location = glGetUniformLocation(shaders, "u_Color");
    glUniform4f(location, 0.3f, 0.3f, 1.0f, 1.0f);
    glBindVertexArray(0);
    glUseProgram(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(varray);
        glUseProgram(shaders);
        glBindBuffer(ib, GL_ELEMENT_ARRAY_BUFFER);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glDeleteProgram(shaders);
    glfwTerminate();
}
2
MegapolisPlayer 28 पद 2020, 02:13

1 उत्तर

सबसे बढ़िया उत्तर

नोटपैड++ आपकी फाइलों के बीच रिटर्न 2 महत्वपूर्ण अंतरों की तुलना करता है।

  1. glBindBuffer(GL_ARRAY_BUFFER, vb); -> glBindBuffer(vbuffer, GL_ARRAY_BUFFER);
  2. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib); -> glBindBuffer(ibuffer, GL_ELEMENT_ARRAY_BUFFER);

और docs.gl पर तुरंत नज़र डालें तो मुझे पता चलता है कि पहला तर्क target होना चाहिए न कि buffer

2
Michał Kaczorowski 28 पद 2020, 02:27
धन्यवाद ... दस्तावेज़ों के बारे में पूरी तरह से भूल गया :)
 – 
MegapolisPlayer
28 पद 2020, 18:16