0

I´m trying to render a rectangle, using shaders. But when I start the programm, OpenGL renders the Rectangle but not in the right color. I´m watching a youtube tutorial about OpenGL which is in C++ but I want to code it in C, so the OpenGL commands and the GLSL source should be right. Can anyone help me? Thanks :

Edit: When I print the String "char *shaderSource" in the function compileShader, it shows me that the String looks a bit different at the end:

"#version 330
 layout(location = 0) out vec4 color;
 void main(){
 color = vec4(1.0f, 1.0f, 0.0f, 1.0f);
 }
R$▒)H"

this chars at the end are every time different

main.c

#define APIENTRY __stdcall
#include<glad/glad.h>
#include <GL/glut.h>
#include <stdio.h>
#include "VertexBuffer.h"
//#include"shader.h"
#include"shader2.h"


struct VertexBuffer buffer1;

void display(void)
{
    glClearColor(0,1.0,1.0,0);
    glClear(GL_COLOR_BUFFER_BIT); //Löscht Bild aus Speicher

    bindVertexBuffer(&buffer1);
    glDrawArrays(GL_TRIANGLES,0, 3); //Modus, start, anzahl
    unbindVertexBuffer();

    glFlush(); //Gibt dem Bildschirm das Bild
}

void keyboard(unsigned char key, int x, int y)
{
    if(key == 27) //Wenn Escape schließen
    {
        exit(0);
    }
}

void reshape(int width, int height)
{

}

int main (int argc, char **argv)
{

    glutInit(&argc, argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(183,100);
    glutCreateWindow("TestApp");
    glutDisplayFunc(display); //display func initialisieren
    glutKeyboardFunc(keyboard); //keyboard func initialisieren
    glutReshapeFunc(reshape); //reshape func initialisieren


    gladLoadGL();



    //Initialize the triangle-------------------------
    struct Vertex verticies[3];
    verticies[0].x = -0.5;
    verticies[0].y = -0.5;
    verticies[0].z = 0;

    verticies[1].x = 0;
    verticies[1].y = 0.5;
    verticies[1].z = 0;

    verticies[2].x = 0.5;
    verticies[2].y = -0.5;
    verticies[2].z = 0;

     uint32_t numVerticies = 3;
    //-------------------------------------------------


    buffer1.data = verticies; //Daten an Buffer übergeben
    buffer1.numVerticies = numVerticies; //Anzahl verticies an Buffer übergeben

    initVertexBuffer(&buffer1); //Buffer initialisieren, mit Adresse des Buffers

    struct Shader shader1;
    shader1.vertexShader = "#version 330 core layout(location = 0) in vec3 position; void main(){   gl_Position = vec4(position, 1.0f);}";
    shader1.fragmentShader = "#version 330 core layout(location = 0) in vec3 position; void main(){gl_Position = vec4(position, 1.0f);}";
    initShader(&shader1);
    bindShader(&shader1);

    //LOOP starts
    glutMainLoop();
    return 0;
}

shader2.h

#include<glad/glad.h>
#include<stdio.h>

struct Shader
{
    //Parameter
    const char *vertexShader;
    const char *fragmentShader;

    GLuint shaderId;

};

GLuint createShader();
GLuint compileShader();

void initShader(struct Shader *shader)
{
    (*shader).shaderId = createShader((*shader), (*shader).vertexShader, (*shader).fragmentShader);

}

void bindShader(struct Shader *shader)
{
    glUseProgram((*shader).shaderId);
}

void unBindShader(struct Shader *shader)
{
    glUseProgram(0);
}






GLuint createShader(struct Shader *shader, const char *vertexShader, const char *fragmentShader)
{


    GLuint program = glCreateProgram();
    GLuint vs = compileShader(vertexShader, GL_VERTEX_SHADER);
    GLuint fs = compileShader(fragmentShader, GL_FRAGMENT_SHADER);

    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);

   // glDetachShader(program, vs);
   // glDetachShader(program, fs);

    return program;
}

GLuint compileShader(char *shaderSource, GLenum type)
{
    GLuint id = glCreateShader(type);
    glShaderSource(id, 1, &shaderSource, 0);
    glCompileShader(id);


    return id;
}

0 Answers0