repeat parameter for textures

This commit is contained in:
DaniTheSkunk 2022-10-06 05:37:54 +00:00
parent 721185c413
commit 009f80dd3a
5 changed files with 71 additions and 33 deletions

View File

@ -33,25 +33,27 @@ public class Test {
while(!window.shouldClose()) { while(!window.shouldClose()) {
window.tick(); window.tick();
var renderContext = window.renderStart(); var renderContext = window.renderStart();
renderContext.drawTextureRectangle( //renderContext.drawTextureRectangle(
new Recti(0, 0, 1280, 720), // new Recti(0, 0, 1280, 720),
tex // tex
); //);
renderContext.drawTextureRectangle( //renderContext.drawTextureRectangle(
new Recti(new Vec2i(200, 100), tex2.getSize()), // new Recti(new Vec2i(200, 100), tex2.getSize()),
tex2 // tex2,
); // true
//);
renderContext.drawTextureRectangle( renderContext.drawTextureRectangle(
new Recti(new Vec2i(400, 400), new Vec2i(16, 16)), new Recti(new Vec2i(400, 400), new Vec2i(160, 160)),
tileset.get(0x30) tileset.get(0x30),
true
); );
renderContext.drawString(new Vec2i(100, 100), "hello world mew", font); renderContext.drawString(new Vec2i(100, 100), "hello world mew", font);
renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2); renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2);
renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520)); //renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520));
window.renderFinish(renderContext); window.renderFinish(renderContext);
} }

View File

@ -8,10 +8,14 @@ import static org.lwjgl.opengl.GL46.*;
class RenderContext extends BaseRenderContext implements IRenderContext { class RenderContext extends BaseRenderContext implements IRenderContext {
private final TextureAtlas atlas; private final TextureAtlas atlas;
private final int texCoordIndex; private final int texCoordIndex;
private final int texOffsetIndex;
private final int texSizeIndex;
public RenderContext(TextureAtlas atlas, int texCoordIndex) { public RenderContext(TextureAtlas atlas, int texCoordIndex, int texOffsetIndex, int texSizeIndex) {
this.atlas = atlas; this.atlas = atlas;
this.texCoordIndex = texCoordIndex; this.texCoordIndex = texCoordIndex;
this.texOffsetIndex = texOffsetIndex;
this.texSizeIndex = texSizeIndex;
} }
@Override @Override
@ -52,21 +56,21 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
@Override @Override
public void drawTexture(Vec2i pos, ITexture texture) { public void drawTexture(Vec2i pos, ITexture texture) {
drawTextureRectangle(new Recti(pos, texture.getSize()), texture); drawTextureRectangle(new Recti(pos, texture.getSize()), texture, true);
} }
@Override @Override
public void drawTexture(Vec2i pos, ITexture texture, Color color) { public void drawTexture(Vec2i pos, ITexture texture, Color color) {
drawTextureRectangle(new Recti(pos, texture.getSize()), texture, color); drawTextureRectangle(new Recti(pos, texture.getSize()), texture, color, true);
} }
@Override @Override
public void drawTextureRectangle(Recti rect, ITexture texture) { public void drawTextureRectangle(Recti rect, ITexture texture, boolean repeat) {
drawTextureRectangle(rect, texture, Color.WHITE); drawTextureRectangle(rect, texture, Color.WHITE, repeat);
} }
@Override @Override
public void drawTextureRectangle(Recti rect, ITexture texture, Color color) { public void drawTextureRectangle(Recti rect, ITexture texture, Color color, boolean repeat) {
var tex = (Texture)texture; var tex = (Texture)texture;
var tl = rect.getTopLeft(); var tl = rect.getTopLeft();
@ -74,10 +78,30 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
var bl = rect.getBottomLeft(); var bl = rect.getBottomLeft();
var br = rect.getBottomRight(); var br = rect.getBottomRight();
/*
var ttl = Vec2i.divf(tex.getTexArea().getTopLeft(), atlas.getSize()); var ttl = Vec2i.divf(tex.getTexArea().getTopLeft(), atlas.getSize());
var ttr = Vec2i.divf(tex.getTexArea().getTopRight(), atlas.getSize()); var ttr = Vec2i.divf(tex.getTexArea().getTopRight(), atlas.getSize());
var tbl = Vec2i.divf(tex.getTexArea().getBottomLeft(), atlas.getSize()); var tbl = Vec2i.divf(tex.getTexArea().getBottomLeft(), atlas.getSize());
var tbr = Vec2i.divf(tex.getTexArea().getBottomRight(), atlas.getSize()); var tbr = Vec2i.divf(tex.getTexArea().getBottomRight(), atlas.getSize());
*/
var topleft = tex.getTexArea().getTopLeft();
var size = repeat ? tex.getSize() : rect.getSize();
/*var ttl = Vec2i.ZERO;
var ttr = Vec2i.sub(tex.getTexArea().getTopRight(), topleft);
var tbl = Vec2i.sub(tex.getTexArea().getBottomLeft(), topleft);
var tbr = Vec2i.sub(tex.getTexArea().getBottomRight(), topleft);
*/
var ttl = Vec2i.ZERO;
var ttr = new Vec2i(rect.getWidth(), 0);
var tbl = new Vec2i(0, rect.getHeight());
var tbr = new Vec2i(rect.getWidth(), rect.getHeight());
if(!repeat) {
ttr = Vec2i.sub(tex.getTexArea().getTopRight(), topleft);
tbl = Vec2i.sub(tex.getTexArea().getBottomLeft(), topleft);
tbr = Vec2i.sub(tex.getTexArea().getBottomRight(), topleft);
}
glColor4f( glColor4f(
color.getR() / 255.0f, color.getR() / 255.0f,
@ -86,19 +110,23 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
color.getA() / 255.0f color.getA() / 255.0f
); );
glUniform2i(texOffsetIndex, topleft.getX(), topleft.getY());
glUniform2i(texSizeIndex, size.getX(), size.getY());
glBegin(GL_TRIANGLES);
//counterclockwise triangles //counterclockwise triangles
glVertexAttrib2d(texCoordIndex, tbl.getX(), tbl.getY()); glVertexAttribI2i(texCoordIndex, tbl.getX(), tbl.getY());
glVertex2i(bl.getX(), bl.getY()); glVertex2i(bl.getX(), bl.getY());
glVertexAttrib2d(texCoordIndex, ttr.getX(), ttr.getY()); glVertexAttribI2i(texCoordIndex, ttr.getX(), ttr.getY());
glVertex2i(tr.getX(), tr.getY()); glVertex2i(tr.getX(), tr.getY());
glVertexAttrib2d(texCoordIndex, ttl.getX(), ttl.getY()); glVertexAttribI2i(texCoordIndex, ttl.getX(), ttl.getY());
glVertex2i(tl.getX(), tl.getY()); glVertex2i(tl.getX(), tl.getY());
glVertexAttrib2d(texCoordIndex, ttr.getX(), ttr.getY()); glVertexAttribI2i(texCoordIndex, ttr.getX(), ttr.getY());
glVertex2i(tr.getX(), tr.getY()); glVertex2i(tr.getX(), tr.getY());
glVertexAttrib2d(texCoordIndex, tbl.getX(), tbl.getY()); glVertexAttribI2i(texCoordIndex, tbl.getX(), tbl.getY());
glVertex2i(bl.getX(), bl.getY()); glVertex2i(bl.getX(), bl.getY());
glVertexAttrib2d(texCoordIndex, tbr.getX(), tbr.getY()); glVertexAttribI2i(texCoordIndex, tbr.getX(), tbr.getY());
glVertex2i(br.getX(), bl.getY()); glVertex2i(br.getX(), bl.getY());
glEnd();
} }
} }

View File

@ -52,7 +52,12 @@ public class Window extends BaseWindow {
textureAtlas = new TextureAtlas(); textureAtlas = new TextureAtlas();
program = new Program(vertexSource, fragmentSource); program = new Program(vertexSource, fragmentSource);
program.use(); program.use();
renderContext = new RenderContext(textureAtlas, program.getAttribLocation("texCoord")); renderContext = new RenderContext(
textureAtlas,
program.getAttribLocation("texCoord"),
program.getUniformLocation("texOffset"),
program.getUniformLocation("texSize")
);
glProgramUniform2f(program.program, program.getUniformLocation("windowSize"), size.getX(), size.getY()); glProgramUniform2f(program.program, program.getUniformLocation("windowSize"), size.getX(), size.getY());
shouldClose = false; shouldClose = false;
@ -98,9 +103,9 @@ public class Window extends BaseWindow {
@Override @Override
public void renderFinish(IRenderContext context) { public void renderFinish(IRenderContext context) {
if(debug) { if(debug) {
context.drawTextureRectangle(new Recti(Vec2i.ZERO, size), textureAtlas.getAtlasTexture()); context.drawTextureRectangle(new Recti(Vec2i.ZERO, size), textureAtlas.getAtlasTexture(), true);
} }
glEnd(); //glEnd();
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -111,7 +116,7 @@ public class Window extends BaseWindow {
glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.use(); program.use();
glBegin(GL_TRIANGLES); //glBegin(GL_TRIANGLES);
return renderContext; return renderContext;
} }
@ -139,7 +144,7 @@ public class Window extends BaseWindow {
private static String vertexSource = """ private static String vertexSource = """
#version 450 #version 450
layout(location = 0) in vec2 pos; layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 texCoord; layout(location = 1) in ivec2 texCoord;
layout(location = 2) uniform vec2 windowSize; layout(location = 2) uniform vec2 windowSize;
layout(location = 1) out vec2 out_texCoord; layout(location = 1) out vec2 out_texCoord;
void main() { void main() {
@ -152,10 +157,13 @@ public class Window extends BaseWindow {
#version 450 #version 450
layout(location = 1) in vec2 texCoord; layout(location = 1) in vec2 texCoord;
layout(binding = 0) uniform sampler2D tex; layout(binding = 0) uniform sampler2D tex;
layout(location = 3) uniform ivec2 texOffset;
layout(location = 4) uniform ivec2 texSize;
out vec4 color; out vec4 color;
void main() { void main() {
//color = vec4(texCoord.x, texCoord.y, 0.7f, 1.0f); //color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
color = texture(tex, texCoord); //color = texture(tex, texCoord);
color = texelFetch(tex, ivec2(mod(texCoord, texSize)) + texOffset, 0);
} }
"""; """;
} }

View File

@ -22,7 +22,7 @@ abstract public class BaseRenderContext implements IRenderContext {
int ch = buf.get(i); int ch = buf.get(i);
var tex = font.getTexture(ch); var tex = font.getTexture(ch);
var off = font.getOffset(ch); var off = font.getOffset(ch);
drawTextureRectangle(new Recti(Vec2i.add(new Vec2i(x, y), off), tex.getSize()), tex); drawTextureRectangle(new Recti(Vec2i.add(new Vec2i(x, y), off), tex.getSize()), tex, true);
x += font.getXAdvance(ch); x += font.getXAdvance(ch);
} }
} }

View File

@ -9,7 +9,7 @@ public interface IRenderContext {
void drawString(Vec2i pos, String string, IFont font); void drawString(Vec2i pos, String string, IFont font);
void drawTexture(Vec2i pos, ITexture texture); void drawTexture(Vec2i pos, ITexture texture);
void drawTexture(Vec2i pos, ITexture texture, Color color); void drawTexture(Vec2i pos, ITexture texture, Color color);
void drawTextureRectangle(Recti rect, ITexture texture); void drawTextureRectangle(Recti rect, ITexture texture, boolean repeat);
void drawTextureRectangle(Recti rect, ITexture texture, Color color); void drawTextureRectangle(Recti rect, ITexture texture, Color color, boolean repeat);
//todo: drawTextureRectangleRepeat //todo: drawTextureRectangleRepeat
} }