Home > Net >  shader in C opengl2.1 doesn't compile in arch linux
shader in C opengl2.1 doesn't compile in arch linux

Time:02-16

I am trying to create a red triangle in C using graphics api opengl 2.1 like below: enter image description here

it will still compile but my code says that there is a error and is in white shown below:

opengl 2.1 is supported!
error!

enter image description here

my code is:

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

#include <iostream>

void error_check() {
    GLenum error = glGetError();
    if (error != NULL) {
        std::cout << "error!" << std::endl;
    }
}
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);

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

    return program;
}

int main() {
    glfwInit();
    GLFWwindow* engine_window =
        glfwCreateWindow(600, 600, "Senku-Engine 2", NULL, NULL);
    if (engine_window == NULL) {
        printf("window not opening!..");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(engine_window);
    glewExperimental = GL_TRUE;
    glewInit();
    if (GLEW_VERSION_2_1 == GL_TRUE) {
        std::cout << "opengl 2.1 is supported!" << std::endl;
    }

    float vertices[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), vertices, GL_STATIC_DRAW);

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

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

    std::string fragmentShader =
        "#version 120 core\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);
    error_check();

    glViewport(0, 0, 600, 600);
    while (!glfwWindowShouldClose(engine_window)) {
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(engine_window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

it seems my code shows the red color when using just one line of code in shader source code but doesn't work if i use more than one string.

CodePudding user response:

The profile string core might not be supported by a pure GLSL 120 implementation, use

#version 120

in and out are GLSL 130, layout qualifiers are GLSL 330. You need

attribute vec4 position;

In the fragment shader, remove the out variable and do

gl_FragColor = vec4(1.0,0.0,0.0,1.0);

These are the most obvious problems. If you have further errors, use glGetShaderInfoLog to query the compilation output which tells you about the problems. See Shader error handling.

  • Related