Hello guys,
While experimenting with OpenGL It seems I do not understand something with GlDrawArraysInstanced()
I want to achieve: 1INT of an Array = 1 COLOR = 1 INSTANCE = 2TRIANLES = 1 SQUARE
I have a map that fits my screen with integers, each representing a color (4 differents available):
int map_size = tile_x*tile_y;
int *mapmat = new int [map_size];
for (int i = 0; i < map_size; i++) {
mapmat[i] = (i % 4);
}
mapmat[0] = 3; //3 is grey
mapmat[1] = 2; //2 is blue
mapmat[2] = 1; //1 is green
mapmat[3] = 0; //0 is red
mapmat[4] = 3;
mapmat[5] = 1;
I feed it into a buffer and bind it to a VAO:
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(mapmat), mapmat, GL_STATIC_DRAW);
GLuint shaderProgram = graphic_engine::compile_program("shader/simple.vs","shader/simple.fs");
glUseProgram(shaderProgram);
GLuint matAttrib = glGetAttribLocation(shaderProgram, "mat");
glEnableVertexAttribArray(matAttrib);
glVertexAttribIPointer(matAttrib, 1, GL_INT, 0, 0);
glVertexAttribDivisor(matAttrib,1);
here are about my vertex and fragment shaders:
#version 150 core //VERTEX SHADER
#extension GL_ARB_explicit_attrib_location : require
layout (location = 0) in int mat;
layout (location = 1) uniform vec4 screen; //rx,ry,tile_x,tile_y //some screen settings used only set once ...
out VS_OUT
{
vec4 color;
}vs_out;
// triangles layout
// 2------1
// | /|
// | / |
// | / |
// | / |
// |/ |
// 0------3
const int indexes[6]={0,1,2,0,3,1};
void main() {
float rx = screen[0];
float ry = screen[1];
int tx = int(screen[2]);
int ty = int(screen[3]);
float ox = -1.0 + rx*(gl_InstanceID % tx);
float oy = 1.0 - ry*(gl_InstanceID / tx) ;
vec4 vertices[4]= vec4[4](
vec4(ox,oy-ry,0.5,1.0),
vec4(ox+rx,oy,0.5,1.0),
vec4(ox,oy,0.5,1.0),
vec4(ox+rx,oy-ry,0.5,1.0)
);
const vec4 colors[4]= vec4[4](
vec4(1.0,0.0,0.0,1.0),
vec4(0.0,1.0,0.0,1.0),
vec4(0.0,0.0,1.0,1.0),
vec4(0.5,0.5,0.5,1.0)
);
gl_Position = vertices[indexes[gl_VertexID]]; //array of the 6 needed vertices
vs_out.color = colors[mat]; //the color corresponding to the map
}
#version 150 core //FRAGMENT SHADER
in VS_OUT
{
vec4 color;
}vs_out;
uniform vec4 screen; //rx,ry,tile_x,tile_y
out vec4 colorout;
void main() {
colorout =vs_out.color;
}
I call this each frame:
glDrawArraysInstanced(GL_TRIANGLES, 0,6, tile_x*tile_y);
and here is my output:
[ATTACH=CONFIG]152[/ATTACH]
What do I miss Here ?
I don’t really understand when the Fragment shader gets data, it’s supposed to be per vertex, and I already achieved to put one color per vertex, but what if I want the same color for the 6 vertices of a square ?