OK, so I'm fairly new to OpenGL, but I've been trying to keep to the newer syntax with vertex & uv buffers instead of the glBegin()/glEnd() syntax. I'm working on a GUI system right now, and with my shaders I can make a square that is red, but I can't seem to make it load a texture. Have I used the wrong syntax or is there an easier way to do this?
Here are my 2 functions to draw the GUI:
#SelectExpand
1void GUI::init_GUI
()
2{
3 const GLfloat gui_vertex_data
[] = {
4 0.
0f,
0.
0f,
0.
0f,
5 100.
0f,
0.
0f,
0.
0f,
6 0.
0f,
100.
0f,
0.
0f,
7
8 100.
0f,
0.
0f,
0.
0f,
9 0.
0f,
100.
0f,
0.
0f,
10 100.
0f,
100.
0f,
0.
0f};
11
12 const GLfloat gui_uv_data
[] = {
13 0.
0f,
0.
0f,
14 1.
0f,
0.
0f,
15 0.
0f,
1.
0f,
16
17 1.
0f,
0.
0f,
18 0.
0f,
1.
0f,
19 1.
0f,
1.
0f};
20
21 glGenVertexArrays
(1,
&gui_VertexArrayID
);
22 glBindVertexArray
(gui_VertexArrayID
);
23 //generate buffers and store to GPU
24 //--vertexbuffer
25 glGenBuffers
(1,
&gui_vertexbuffer
);
26 glBindBuffer
(GL_ARRAY_BUFFER, gui_vertexbuffer
);
27 glBufferData
(GL_ARRAY_BUFFER,
sizeof(gui_vertex_data
), gui_vertex_data, GL_STATIC_DRAW
);
28
29 glGenBuffers
(1,
&gui_uvbuffer
);
30 glBindBuffer
(GL_ARRAY_BUFFER, gui_uvbuffer
);
31 glBufferData
(GL_ARRAY_BUFFER,
sizeof(gui_uv_data
), gui_uv_data, GL_STATIC_DRAW
);
32
33 gui_programID
= LoadShaders
("assets/shaders/GUIVertex.vert",
"assets/shaders/GUIFragment.frag");
34 glUseProgram
(gui_programID
);
35
36 bm_gui
= al_load_bitmap("assets/grass_001.png");
37 gui_texture
= al_get_opengl_texture(bm_gui
);
38 gui_textureID
= glGetUniformLocation
(gui_programID,
"gTexture");
39}
#SelectExpand
1void GUI::draw_GUI()
2{
3 glUseProgram(gui_programID);
4
5 glActiveTexture(GL_TEXTURE0);
6 glBindTexture(GL_TEXTURE_2D, gui_texture);
7 glUniform1i(gui_textureID, 0);
8
9 glEnableVertexAttribArray(0);
10 glBindBuffer(GL_ARRAY_BUFFER, gui_vertexbuffer);
11 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
12
13 glEnableVertexAttribArray(1);
14 glBindBuffer(GL_ARRAY_BUFFER, gui_uvbuffer);
15 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
16
17 // Draw the triangle !
18 glDrawArrays(GL_TRIANGLES, 0, 2 * 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
19
20 glDisableVertexAttribArray(0);
21 glDisableVertexAttribArray(1);
22}
And here are my vertex and fragment shaders:
#SelectExpand
1#version 330
2
3layout (location = 0) in vec2 inPosition;
4layout (location = 1) in vec2 inUV;
5
6out vec2 UV;
7
8void main()
9{
10 gl_Position = vec4(inPosition, 0.0, 1.0);
11 UV = inUV;
12}
#SelectExpand
1#version 330
2
3in vec2 UV;
4out vec4 outputColor;
5
6uniform sampler2D gTexture;
7
8void main()
9{
10 outputColor = texture2D(gTexture, UV);
11 //This turns the square red, otherwise it draws nothing
12 //outputColor = vec4(1.0f, 0.0f, 0.0f, 0.0f);
13}
I hope I didn't bog anybody down with the amount code, but I think this is the least I could put to help with my problem. Thanks in advance!
Edit: I think I may have solved it! I feel dumb, but I forgot to change the "0" to a "1" and the "3" to a "2" for the UV vertex attribute pointer code:
#SelectExpand
1 glEnableVertexAttribArray(1);
2 glBindBuffer(GL_ARRAY_BUFFER, gui_uvbuffer);
3 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0 );