diff options
author | Michael Lutz <michi@icosahedron.de> | 2021-01-16 16:43:21 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2021-02-22 22:16:07 +0100 |
commit | 90fd8f8cda72d8ce985f11c0f7bc1b3bcab68a6e (patch) | |
tree | 0b5f9651ebde6c713bf43a9cada854dc1f26a615 | |
parent | acf59f6b68a3bdd068b4eddccd06749c5b411189 (diff) | |
download | openttd-90fd8f8cda72d8ce985f11c0f7bc1b3bcab68a6e.tar.xz |
Codechange: [OpenGL] Use generic vertex attributes in the shader program.
-rw-r--r-- | src/table/opengl_shader.h | 9 | ||||
-rw-r--r-- | src/video/opengl.cpp | 28 |
2 files changed, 29 insertions, 8 deletions
diff --git a/src/table/opengl_shader.h b/src/table/opengl_shader.h index 43b2ca512..c59bcbcab 100644 --- a/src/table/opengl_shader.h +++ b/src/table/opengl_shader.h @@ -12,9 +12,11 @@ /** Vertex shader that just passes colour and tex coords through. */ static const char *_vertex_shader_direct[] = { "#version 110\n", + "attribute vec2 position, colour_uv;", + "varying vec2 colour_tex_uv;", "void main() {", - " gl_TexCoord[0] = gl_MultiTexCoord0;", - " gl_Position = gl_Vertex;", + " colour_tex_uv = colour_uv;", + " gl_Position = vec4(position, 0.0, 1.0);", "}", }; @@ -22,7 +24,8 @@ static const char *_vertex_shader_direct[] = { static const char *_frag_shader_direct[] = { "#version 110\n", "uniform sampler2D colour_tex;", + "varying vec2 colour_tex_uv;", "void main() {", - " gl_FragColor = texture2D(colour_tex, gl_TexCoord[0].st);", + " gl_FragColor = texture2D(colour_tex, colour_tex_uv);", "}", }; diff --git a/src/video/opengl.cpp b/src/video/opengl.cpp index dbf196164..2f92b2717 100644 --- a/src/video/opengl.cpp +++ b/src/video/opengl.cpp @@ -64,6 +64,11 @@ static PFNGLGETSHADERINFOLOGPROC _glGetShaderInfoLog; static PFNGLGETUNIFORMLOCATIONPROC _glGetUniformLocation; static PFNGLUNIFORM1IPROC _glUniform1i; +static PFNGLGETATTRIBLOCATIONPROC _glGetAttribLocation; +static PFNGLENABLEVERTEXATTRIBARRAYPROC _glEnableVertexAttribArray; +static PFNGLDISABLEVERTEXATTRIBARRAYPROC _glDisableVertexAttribArray; +static PFNGLVERTEXATTRIBPOINTERARBPROC _glVertexAttribPointer; + /** A simple 2D vertex with just position and texture. */ struct Simple2DVertex { float x, y; @@ -209,6 +214,11 @@ static bool BindShaderExtensions() _glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)GetOGLProcAddress("glGetShaderInfoLog"); _glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)GetOGLProcAddress("glGetUniformLocation"); _glUniform1i = (PFNGLUNIFORM1IPROC)GetOGLProcAddress("glUniform1i"); + + _glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)GetOGLProcAddress("glGetAttribLocation"); + _glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glEnableVertexAttribArray"); + _glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glDisableVertexAttribArray"); + _glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERARBPROC)GetOGLProcAddress("glVertexAttribPointer"); } else { /* In the ARB extension programs and shaders are in the same object space. */ _glCreateProgram = (PFNGLCREATEPROGRAMPROC)GetOGLProcAddress("glCreateProgramObjectARB"); @@ -226,11 +236,17 @@ static bool BindShaderExtensions() _glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)GetOGLProcAddress("glGetInfoLogARB"); _glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)GetOGLProcAddress("glGetUniformLocationARB"); _glUniform1i = (PFNGLUNIFORM1IPROC)GetOGLProcAddress("glUniform1iARB"); + + _glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)GetOGLProcAddress("glGetAttribLocationARB"); + _glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glEnableVertexAttribArrayARB"); + _glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glDisableVertexAttribArrayARB"); + _glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERARBPROC)GetOGLProcAddress("glVertexAttribPointerARB"); } return _glCreateProgram != nullptr && _glDeleteProgram != nullptr && _glLinkProgram != nullptr && _glGetProgramiv != nullptr && _glGetProgramInfoLog != nullptr && _glCreateShader != nullptr && _glDeleteShader != nullptr && _glShaderSource != nullptr && _glCompileShader != nullptr && _glAttachShader != nullptr && - _glGetShaderiv != nullptr && _glGetShaderInfoLog != nullptr && _glGetUniformLocation != nullptr && _glUniform1i != nullptr; + _glGetShaderiv != nullptr && _glGetShaderInfoLog != nullptr && _glGetUniformLocation != nullptr && _glUniform1i != nullptr && + _glGetAttribLocation != nullptr && _glEnableVertexAttribArray != nullptr && _glDisableVertexAttribArray != nullptr && _glVertexAttribPointer != nullptr; } /** Callback to receive OpenGL debug messages. */ @@ -417,10 +433,12 @@ const char *OpenGLBackend::Init() if (glGetError() != GL_NO_ERROR) return "Can't generate VBO for fullscreen quad"; /* Set vertex state. */ - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(2, GL_FLOAT, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, x)); - glTexCoordPointer(2, GL_FLOAT, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, u)); + GLint loc_position = _glGetAttribLocation(this->vid_program, "position"); + GLint colour_position = _glGetAttribLocation(this->vid_program, "colour_uv"); + _glEnableVertexAttribArray(loc_position); + _glEnableVertexAttribArray(colour_position); + _glVertexAttribPointer(loc_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, x)); + _glVertexAttribPointer(colour_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, u)); _glBindVertexArray(0); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |