Skip to content

Commit

Permalink
Added support for OpenGL 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlegiantJGC committed Feb 17, 2021
1 parent 24704b9 commit 0509d2d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 23 deletions.
46 changes: 34 additions & 12 deletions amulet_map_editor/api/opengl/canvas/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
GL_ONE_MINUS_SRC_ALPHA,
glDeleteTextures,
)
from OpenGL.error import GLError

import uuid
import sys

from amulet_map_editor import log


class BaseCanvas(glcanvas.GLCanvas):
def __init__(self, parent: wx.Window):
Expand All @@ -30,22 +34,40 @@ def __init__(self, parent: wx.Window):
style=wx.WANTS_CHARS,
)

# create a UUID for the context. Used to get shaders
self._context_identifier = str(uuid.uuid4())

if sys.platform == "linux":
# setup the OpenGL context. This apparently fixes #84
self._context = glcanvas.GLContext(self)
self.SetCurrent(self._context)
self._gl_texture_atlas = glGenTextures(1) # Create the atlas texture location
else:
context_attributes = wx.glcanvas.GLContextAttrs()
context_attributes.CoreProfile().OGLVersion(
3, 3
).Robust().ResetIsolation().EndList()
self._context = glcanvas.GLContext(
self, ctxAttrs=context_attributes
) # setup the OpenGL context
self.SetCurrent(self._context)
self._context_identifier = str(
uuid.uuid4()
) # create a UUID for the context. Used to get shaders
self._gl_texture_atlas = glGenTextures(1) # Create the atlas texture location
try:
context_attributes = wx.glcanvas.GLContextAttrs()
context_attributes.CoreProfile().OGLVersion(
3, 3
).Robust().ResetIsolation().EndList()
self._context = glcanvas.GLContext(
self, ctxAttrs=context_attributes
) # setup the OpenGL context
self.SetCurrent(self._context)
self._gl_texture_atlas = glGenTextures(1) # Create the atlas texture location
except GLError:
log.info("Failed setting up modern OpenGL. Falling back to legacy OpenGL.")
try:
context_attributes = wx.glcanvas.GLContextAttrs()
context_attributes.CoreProfile().OGLVersion(
2, 1
).Robust().ResetIsolation().EndList()
self._context = glcanvas.GLContext(
self, ctxAttrs=context_attributes
) # setup the OpenGL context
self.SetCurrent(self._context)
self._gl_texture_atlas = glGenTextures(1) # Create the atlas texture location
except GLError as e:
log.error("Failed setting up legacy OpenGL.")
raise e
self._setup_opengl() # set some OpenGL states

@property
Expand Down
20 changes: 18 additions & 2 deletions amulet_map_editor/api/opengl/shaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,44 @@
glGenVertexArrays,
GL_VERTEX_SHADER,
GL_FRAGMENT_SHADER,
glGetString,
GL_SHADING_LANGUAGE_VERSION,
GL_VERSION,
)
import OpenGL.GL.shaders
import os
from typing import Dict, Tuple, Any
import re

shader_dir = os.path.join(os.path.dirname(__file__))
_shaders: Dict[Tuple[str, str], Any] = {}

GL_VERSION_MATCH = re.compile(r"^(?P<major>\d+)\.(?P<minor>\d+)?")


def get_shader(
context_identifier: str, shader_name: str
) -> OpenGL.GL.shaders.ShaderProgram:
shader_key = (context_identifier, shader_name)
if shader_key not in _shaders:
gl_version_match = GL_VERSION_MATCH.match(glGetString(GL_SHADING_LANGUAGE_VERSION).decode("utf-8"))
if gl_version_match:
gl_version_tuple = tuple(int(v) for v in gl_version_match.groups())
if gl_version_tuple >= (3, 30):
gl_version = "330" # opengl 3.3
else:
gl_version = "120" # opengl 2.1
else:
# in theory this shouldn't happen if the version is correctly formatted.
gl_version = "330"

def compile_shader():
return OpenGL.GL.shaders.compileProgram(
_load_shader(
os.path.join(shader_dir, f"{shader_name}.vert"), GL_VERTEX_SHADER
os.path.join(shader_dir, f"{shader_name}_{gl_version}.vert"), GL_VERTEX_SHADER
),
_load_shader(
os.path.join(shader_dir, f"{shader_name}.frag"), GL_FRAGMENT_SHADER
os.path.join(shader_dir, f"{shader_name}_{gl_version}.frag"), GL_FRAGMENT_SHADER
),
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# version 330
# version 120
in vec2 fTexCoord;
in vec4 fTexOffset;
in vec3 fTint;

out vec4 outColor;

uniform sampler2D image;

void main(){
vec4 texColor = texture(
vec4 texColor = texture2D(
image,
vec2(
mix(fTexOffset.x, fTexOffset.z, mod(fTexCoord.x, 1.0)),
Expand All @@ -18,5 +16,5 @@ void main(){
if(texColor.a < 0.02)
discard;
texColor.xyz = texColor.xyz * fTint * 0.85;
outColor = texColor;
gl_FragColor = texColor;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# version 330
# version 120
#extension GL_ARB_explicit_attrib_location : enable

layout(location = 0) in vec3 positions;
layout(location = 1) in vec2 vTexCoord;
layout(location = 2) in vec4 vTexOffset;
layout(location = 3) in vec3 vTint;

out vec2 fTexCoord;
out vec4 fTexOffset;
out vec3 fTint;
varying vec2 fTexCoord;
varying vec4 fTexOffset;
varying vec3 fTint;

uniform mat4 transformation_matrix;

Expand Down
20 changes: 20 additions & 0 deletions amulet_map_editor/api/opengl/shaders/render_chunk_330.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# version 120
in vec2 fTexCoord;
in vec4 fTexOffset;
in vec3 fTint;

uniform sampler2D image;

void main(){
vec4 texColor = texture2D(
image,
vec2(
mix(fTexOffset.x, fTexOffset.z, mod(fTexCoord.x, 1.0)),
mix(fTexOffset.y, fTexOffset.w, mod(fTexCoord.y, 1.0))
)
);
if(texColor.a < 0.02)
discard;
texColor.xyz = texColor.xyz * fTint * 0.85;
gl_FragColor = texColor;
}
20 changes: 20 additions & 0 deletions amulet_map_editor/api/opengl/shaders/render_chunk_330.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# version 120
#extension GL_ARB_explicit_attrib_location : enable

layout(location = 0) in vec3 positions;
layout(location = 1) in vec2 vTexCoord;
layout(location = 2) in vec4 vTexOffset;
layout(location = 3) in vec3 vTint;

varying vec2 fTexCoord;
varying vec4 fTexOffset;
varying vec3 fTint;

uniform mat4 transformation_matrix;

void main(){
gl_Position = transformation_matrix * vec4(positions, 1.0);
fTexCoord = vTexCoord;
fTexOffset = vTexOffset;
fTint = vTint;
}

0 comments on commit 0509d2d

Please sign in to comment.