Deady Online

Delivering information to the world. You are always welcomed to join!

Distribuiţi | 

 How to get attributes to a shader, bind locations, get location, for older version like 100, 110, 120, 130, 150, opengl short guide

In jos 

Mesaje : 343
Data de inscriere : 27/08/2011
Varsta : 23
Localizare : Romania

MesajSubiect: How to get attributes to a shader, bind locations, get location, for older version like 100, 110, 120, 130, 150, opengl short guide   Lun Apr 04, 2016 6:51 pm

How to bind attributes to a shader, how to get attributes location, how to send data like: color, to a shader, on older version like 100, 110, 120, 130, 140, 150 etc, here is an opengl and C++ guide. This should be working for all.

Simple Example of Vertex Shader:
//the vertex shader starts here:

#version 130 //the version used

in vec2 vertexPosition; //get input a vec2 containing the position of the vertices
attribute vec3 color; //get an attribute vec3 containing the color for each vertex
out vec3 thecolor; //get to the fragment shader a vec3 called thecolor.

void main()
    gl_Position.xy=vertexPosition; //the position for X and Y is given in the vertexPosition

    gl_Position.z=-1.0; //set the third dimension for Z to 1.0;
    gl_Position.w=1.0; //set the value for W to 1.0,
    thecolor=color; //thecolor is now getting information from color (which was the color input from the source code).


//the vertex shader ends here:

This particular shader doesn't do much, but it does well. Just gets the position and color from the program's source code. The position which is a 2d position (containing only X and Y), and then get a 1.0 position for Z (automatically), and 1.0 position for W, in the same way. The color gets into the vertex shader with the name "color" and then is copied to a new vector on the same size called "thecolor" and sent it to the fragment shader.

Simple Example of Fragment Shader:
//the fragment shader starts here

#version 130 //the version used
in vec3 thecolor; //gets as input a vec3 named "the color" which comes here from the vertex shader above

out vec3 color; //outputs on the screen the information of a vec3 named "color"

void main()
    color=thecolor; //"color" gets all the information from "thecolor";

//the fragment shader ends here

Now in the source code:
 GLfloat triangle[] //the position of the vertices

//Creating a buffer which contains all the position above (from triangle):
GLuint trianglebufferID;
    glGenBuffers(1, &trianglebufferID);
    glBindBuffer(GL_ARRAY_BUFFER, trianglebufferID);
    glBufferData(GL_ARRAY_BUFFER,sizeof(triangle), triangle, GL_STATIC_DRAW);
    glVertexAttribPointer(0,2,GL_FLOAT, GL_FALSE, 0, 0);
//Creating a buffer for the indices (just to let the vertexshader know, how many of these vertices we want drew);

 GLuint vertices[] = {0,1,2,0,3,4};
    //important to use for vertices only GLuint, and GLushort;
    GLuint indices;
    glGenBuffers(1, &indices);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

//The color value for each vertex:

GLfloat color[]

//Getting a buffer for the color:

 GLint colorlocation; //this variable will store the value of the colorlocation attribute from the vertex shader (in our case was named "color" see this line: attribute vec3 color;)
    GLuint colorbufferID;

    colorlocation = glGetAttribLocation(programID, "color"); //this function return the
//value of the location which is set in the vertexshader for the color attribute:   

glBindBuffer(GL_ARRAY_BUFFER, colorbufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);

//Drawing to the screen:
glDrawElements(GL_TRIANGLES,6, GL_UNSIGNED_INT, nullptr); //because we have a buffer for indices;
SDL_GL_SwapWindow(window); //put the drew window on the screen;

If you are stuck understanding this, here is the code for the entire project!
Sus In jos
Vezi profilul utilizatorului
How to get attributes to a shader, bind locations, get location, for older version like 100, 110, 120, 130, 150, opengl short guide
Pagina 1 din 1

Permisiunile acestui forum:Nu puteti raspunde la subiectele acestui forum
Deady Online :: Others :: Altele!-
Mergi direct la: