From a6d3f3d779b031f8ca70510abe90fade909a8d0d Mon Sep 17 00:00:00 2001 From: ccolin Date: Sat, 26 Dec 2020 01:21:28 +0100 Subject: textures and whatnot i forgot to commit for a while lol --- src/opengl_widget.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/opengl_widget.cc') diff --git a/src/opengl_widget.cc b/src/opengl_widget.cc index f10a8c6..ca216b3 100644 --- a/src/opengl_widget.cc +++ b/src/opengl_widget.cc @@ -6,24 +6,31 @@ static const GLchar *vertex_shader_source = R"glsl( #version 330 core -layout(location = 0) in vec3 pos; +layout(location = 0) in vec3 in_pos; +layout(location = 1) in vec2 in_uv; + +out vec2 uv; uniform mat4 proj; uniform mat4 view; uniform mat4 model; void main() { - gl_Position = proj * view * model * vec4(pos, 1.0); + gl_Position = proj * view * model * vec4(in_pos, 1.0); + uv = in_uv; } )glsl"; static const GLchar *fragment_shader_source = R"glsl( #version 330 core +in vec2 uv; out vec4 final_col; +uniform sampler2D tex; + void main() { - final_col = vec4(0, 0, 0, 1); + final_col = texture(tex, uv); } )glsl"; @@ -91,7 +98,7 @@ void OpenGLWidget::initializeGL() { GLuint shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); - glBindFragDataLocation(shader_program, 0, "out_color"); + glBindFragDataLocation(shader_program, 0, "final_col"); glLinkProgram(shader_program); glGetProgramiv(shader_program, GL_LINK_STATUS, &status); if (status != GL_TRUE) { @@ -119,6 +126,8 @@ void OpenGLWidget::initializeGL() { trans.translate(0, 0, -5); glUniformMatrix4fv(view_attr, 1, GL_FALSE, trans.data()); + glUniform1i(glGetUniformLocation(shader_program, "tex"), 0); + glClearColor(1, 1, 1, 0); glEnable(GL_DEPTH_TEST); @@ -130,7 +139,7 @@ void OpenGLWidget::initializeGL() { void OpenGLWidget::resizeGL(int w, int h) { QMatrix4x4 projection; - projection.perspective(FOV, (float) w/h, .1, 100); + projection.perspective(FOV, (float) w/h, .1, 1000); glUniformMatrix4fv(proj_attr, 1, GL_FALSE, projection.data()); } @@ -141,9 +150,11 @@ void OpenGLWidget::paintGL() { trans.translate(0, 0, -cam_dist); QMatrix4x4 view = trans * rot; glUniformMatrix4fv(view_attr, 1, GL_FALSE, view.data()); + glActiveTexture(GL_TEXTURE0); for (const OpenGLMesh &mesh : meshes) { glUniformMatrix4fv(model_attr, 1, GL_FALSE, mesh.mat.data()); glBindVertexArray(mesh.vao); + mesh.tex->bind(); glDrawArrays(GL_TRIANGLES, 0, mesh.nverts); } } -- cgit v1.2.3-70-g09d2