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()) {
window.tick();
var renderContext = window.renderStart();
renderContext.drawTextureRectangle(
new Recti(0, 0, 1280, 720),
tex
);
renderContext.drawTextureRectangle(
new Recti(new Vec2i(200, 100), tex2.getSize()),
tex2
);
//renderContext.drawTextureRectangle(
// new Recti(0, 0, 1280, 720),
// tex
//);
//renderContext.drawTextureRectangle(
// new Recti(new Vec2i(200, 100), tex2.getSize()),
// tex2,
// true
//);
renderContext.drawTextureRectangle(
new Recti(new Vec2i(400, 400), new Vec2i(16, 16)),
tileset.get(0x30)
new Recti(new Vec2i(400, 400), new Vec2i(160, 160)),
tileset.get(0x30),
true
);
renderContext.drawString(new Vec2i(100, 100), "hello world mew", font);
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);
}

View File

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

View File

@ -52,7 +52,12 @@ public class Window extends BaseWindow {
textureAtlas = new TextureAtlas();
program = new Program(vertexSource, fragmentSource);
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());
shouldClose = false;
@ -98,9 +103,9 @@ public class Window extends BaseWindow {
@Override
public void renderFinish(IRenderContext context) {
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);
}
@ -111,7 +116,7 @@ public class Window extends BaseWindow {
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.use();
glBegin(GL_TRIANGLES);
//glBegin(GL_TRIANGLES);
return renderContext;
}
@ -139,7 +144,7 @@ public class Window extends BaseWindow {
private static String vertexSource = """
#version 450
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 = 1) out vec2 out_texCoord;
void main() {
@ -152,10 +157,13 @@ public class Window extends BaseWindow {
#version 450
layout(location = 1) in vec2 texCoord;
layout(binding = 0) uniform sampler2D tex;
layout(location = 3) uniform ivec2 texOffset;
layout(location = 4) uniform ivec2 texSize;
out vec4 color;
void main() {
//color = vec4(texCoord.x, texCoord.y, 0.7f, 1.0f);
color = texture(tex, texCoord);
//color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
//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);
var tex = font.getTexture(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);
}
}

View File

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