ui update,many fixes

This commit is contained in:
lllzebralll 2022-12-08 01:03:12 +03:00
parent f04bac6b43
commit a1f9155498
12 changed files with 312 additions and 208 deletions

View File

@ -62,7 +62,7 @@ void Batch2D::vertex(vec2 point,
void Batch2D::texture(Texture* new_texture){ void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture) if (_texture == new_texture)
return; return;
render(); render(GL_TRIANGLES);
_texture = new_texture; _texture = new_texture;
if (new_texture == nullptr) if (new_texture == nullptr)
blank->bind(); blank->bind();
@ -70,21 +70,39 @@ void Batch2D::texture(Texture* new_texture){
new_texture->bind(); new_texture->bind();
} }
void Batch2D::point(float x, float y, float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vertex(x, y, 0, 0, r,g,b,a);
render(GL_POINTS);
}
void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vertex(x1, y1, 0, 0, r,g,b,a);
vertex(x2, y2, 1, 1, r,g,b,a);
render(GL_LINES);
}
void Batch2D::rect(float x, float y, float w, float h){ void Batch2D::rect(float x, float y, float w, float h){
const float r = color.r; const float r = color.r;
const float g = color.g; const float g = color.g;
const float b = color.b; const float b = color.b;
const float a = color.a; const float a = color.a;
if (index + 6*VERTEX_SIZE >= capacity) // if (index + 6*VERTEX_SIZE >= capacity)
render(); // render(GL_TRIANGLES);
vertex(x, y, 0, 0, r,g,b,a); vertex(x, y, 0, 0, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x, y+h, 0, 1, r,g,b,a); vertex(x, y+h, 0, 1, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x, y, 0, 0, r,g,b,a); vertex(x, y, 0, 0, r,g,b,a);
vertex(x+w, y, 1, 0, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a); vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x+w, y, 1, 0, r,g,b,a);
render(GL_TRIANGLES);
} }
void Batch2D::rect( void Batch2D::rect(
@ -96,8 +114,8 @@ void Batch2D::rect(
bool flippedX, bool flippedX,
bool flippedY, bool flippedY,
vec4 tint) { vec4 tint) {
if (index + 6*VERTEX_SIZE >= capacity) // if (index + 6*VERTEX_SIZE >= capacity)
render(); // render(GL_TRIANGLES);
float centerX = w*ox; float centerX = w*ox;
float centerY = h*oy; float centerY = h*oy;
@ -179,96 +197,14 @@ void Batch2D::rect(
vertex(x1, y1, u1, v1, tint.r, tint.g, tint.b, tint.a); vertex(x1, y1, u1, v1, tint.r, tint.g, tint.b, tint.a);
vertex(x3, y3, u3, v3, tint.r, tint.g, tint.b, tint.a); vertex(x3, y3, u3, v3, tint.r, tint.g, tint.b, tint.a);
vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a); vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a);
} render(GL_TRIANGLES);
void Batch2D::sprite(Sprite* sprite) {
vec2 position = sprite->position;
vec2 size = sprite->size;
vec2 origin = sprite->origin;
texture(sprite->texture);
rect(
position.x, position.y,
size.x, size.y,
origin.x, origin.y,
sprite->angle,
sprite->region,
sprite->flippedX,
sprite->flippedY,
sprite->color);
}
void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint){
float scale = 1.0f / (float)atlasRes;
float u = (index % atlasRes) * scale;
float v = 1.0f - ((index / atlasRes) * scale) - scale;
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint){
float scale = 1.0f / (float)atlasRes;
float uu = (index[3] % atlasRes) * scale;
float vu = 1.0f - ((index[3] / atlasRes) * scale) - scale;
float uf = (index[0] % atlasRes) * scale;
float vf = 1.0f - ((index[0] / atlasRes) * scale) - scale;
// if (this->index + 18*VERTEX_SIZE >= capacity)
render();
float ar = 0.88f;
float ox = x + (w * 0.5f);
float sx = w * 0.5f * ar;
vec2 points[7] = {vec2(ox, y+(h*0.5f)),
vec2(ox-sx, y+(h*0.25f)),
vec2(ox, y),
vec2(ox+sx, y+(h*0.25f)),
vec2(ox+sx, y+(h*0.75f)),
vec2(ox, y+h),
vec2(ox-sx, y+(h*0.75f))};
vec2 uvpoints[8] = {vec2(uu, vu),
vec2(uu+scale, vu),
vec2(uu+scale, vu+scale),
vec2(uu, vu+scale),
vec2(uf, vf),
vec2(uf+scale, vf),
vec2(uf+scale, vf+scale),
vec2(uf, vf+scale)};
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[0], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[2], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
glDisable(GL_MULTISAMPLE);
render();
glEnable(GL_MULTISAMPLE);
} }
void Batch2D::rect(float x, float y, float w, float h, void Batch2D::rect(float x, float y, float w, float h,
float u, float v, float tx, float ty, float u, float v, float tx, float ty,
float r, float g, float b, float a){ float r, float g, float b, float a){
if (index + 6*VERTEX_SIZE >= capacity) // if (index + 6*VERTEX_SIZE >= capacity)
render(); // render(GL_TRIANGLES);
vertex(x, y, u, v+ty, r,g,b,a); vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v, r,g,b,a); vertex(x, y+h, u, v, r,g,b,a);
@ -276,6 +212,7 @@ void Batch2D::rect(float x, float y, float w, float h,
vertex(x, y, u, v+ty, r,g,b,a); vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y, u+tx, v+ty, r,g,b,a); vertex(x+w, y, u+tx, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a);
render(GL_TRIANGLES);
} }
void Batch2D::rect(float x, float y, float w, float h, void Batch2D::rect(float x, float y, float w, float h,
@ -284,8 +221,8 @@ void Batch2D::rect(float x, float y, float w, float h,
float r2, float g2, float b2, float r2, float g2, float b2,
float r3, float g3, float b3, float r3, float g3, float b3,
float r4, float g4, float b4, int sh){ float r4, float g4, float b4, int sh){
if (index + 30*VERTEX_SIZE >= capacity) // if (index + 30*VERTEX_SIZE >= capacity)
render(); // render(GL_TRIANGLES);
vec2 v0 = vec2(x+h/2,y+h/2); vec2 v0 = vec2(x+h/2,y+h/2);
vec2 v1 = vec2(x+w-sh,y); vec2 v1 = vec2(x+w-sh,y);
vec2 v2 = vec2(x+sh,y); vec2 v2 = vec2(x+sh,y);
@ -336,10 +273,94 @@ void Batch2D::rect(float x, float y, float w, float h,
vertex(v6, vec2(0, 0), r2,g2,b2,1.0f); vertex(v6, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v9, vec2(0, 0), r2,g2,b2,1.0f); vertex(v9, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v1, vec2(0, 0), r2,g2,b2,1.0f); vertex(v1, vec2(0, 0), r2,g2,b2,1.0f);
render(GL_TRIANGLES);
} }
void Batch2D::render() { void Batch2D::sprite(Sprite* sprite) {
vec2 position = sprite->position;
vec2 size = sprite->size;
vec2 origin = sprite->origin;
texture(sprite->texture);
rect(
position.x, position.y,
size.x, size.y,
origin.x, origin.y,
sprite->angle,
sprite->region,
sprite->flippedX,
sprite->flippedY,
sprite->color);
}
void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint){
float scale = 1.0f / (float)atlasRes;
float u = (index % atlasRes) * scale;
float v = 1.0f - ((index / atlasRes) * scale) - scale;
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint){
float scale = 1.0f / (float)atlasRes;
float uu = (index[3] % atlasRes) * scale;
float vu = 1.0f - ((index[3] / atlasRes) * scale) - scale;
float uf = (index[0] % atlasRes) * scale;
float vf = 1.0f - ((index[0] / atlasRes) * scale) - scale;
// if (this->index + 18*VERTEX_SIZE >= capacity)
// render();
float ar = 0.88f;
float ox = x + (w * 0.5f);
float sx = w * 0.5f * ar;
vec2 points[7] = {vec2(ox, y+(h*0.5f)),
vec2(ox-sx, y+(h*0.25f)),
vec2(ox, y),
vec2(ox+sx, y+(h*0.25f)),
vec2(ox+sx, y+(h*0.75f)),
vec2(ox, y+h),
vec2(ox-sx, y+(h*0.75f))};
vec2 uvpoints[8] = {vec2(uu, vu),
vec2(uu+scale, vu),
vec2(uu+scale, vu+scale),
vec2(uu, vu+scale),
vec2(uf, vf),
vec2(uf+scale, vf),
vec2(uf+scale, vf+scale),
vec2(uf, vf+scale)};
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[0], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[2], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
glDisable(GL_MULTISAMPLE);
render(GL_TRIANGLES);
glEnable(GL_MULTISAMPLE);
}
void Batch2D::render(unsigned int gl_primitive) {
mesh->reload(buffer, index / VERTEX_SIZE); mesh->reload(buffer, index / VERTEX_SIZE);
mesh->draw(GL_TRIANGLES); mesh->draw(gl_primitive);
index = 0; index = 0;
} }

View File

@ -40,6 +40,8 @@ public:
void sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint); void sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint);
void sprite(Sprite* sprite); void sprite(Sprite* sprite);
void blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint); void blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint);
void point(float x, float y, float r, float g, float b, float a);
void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
void rect(float x, float y, void rect(float x, float y,
float w, float h, float w, float h,
float ox, float oy, float ox, float oy,
@ -58,7 +60,7 @@ public:
float r2, float g2, float b2, float r2, float g2, float b2,
float r3, float g3, float b3, float r3, float g3, float b3,
float r4, float g4, float b4, int sh); float r4, float g4, float b4, int sh);
void render(); void render(unsigned int gl_primitive);
}; };
#endif /* SRC_GRAPHICS_BATCH2D_H_ */ #endif /* SRC_GRAPHICS_BATCH2D_H_ */

View File

@ -10,21 +10,21 @@ Font::~Font(){
delete texture; delete texture;
} }
int Font::getGlyphWidth(char c) { // int Font::getGlyphWidth(char c) {
switch (c){ // switch (c){
case 'l': // case 'l':
case 'i': // case 'i':
case 'j': // case 'j':
case '|': // case '|':
case '.': // case '.':
case ',': // case ',':
case ':': // case ':':
case ';': return 7; // case ';': return 7;
case 't': return 8; // case 't': return 8;
case ' ': return 7; // case ' ': return 7;
} // }
return 7; // return 7;
} // }
bool Font::isPrintableChar(int c) { bool Font::isPrintableChar(int c) {
@ -81,7 +81,7 @@ void Font::draw(Batch2D* batch, std::wstring text, int x, int y, int style) {
next = charpage; next = charpage;
} }
} }
x += getGlyphWidth(c); x += 8;//getGlyphWidth(c);
} }
page = next; page = next;
next = 10000; next = 10000;

View File

@ -17,7 +17,7 @@ public:
Font(std::vector<Texture*> pages); Font(std::vector<Texture*> pages);
~Font(); ~Font();
int getGlyphWidth(char c); // int getGlyphWidth(char c);
bool isPrintableChar(int c); bool isPrintableChar(int c);
void draw(Batch2D* batch, std::wstring text, int x, int y); void draw(Batch2D* batch, std::wstring text, int x, int y);
void draw(Batch2D* batch, std::wstring text, int x, int y, int style); void draw(Batch2D* batch, std::wstring text, int x, int y, int style);

View File

@ -4,23 +4,45 @@
#include "Texture.h" #include "Texture.h"
Framebuffer::Framebuffer(int width, int height) : width(width), height(height) { Framebuffer::Framebuffer(int width, int height) : width(width), height(height) {
glGenFramebuffers(1, &fbo); // glGenFramebuffers(1, &fbo);
bind(); // bind();
GLuint tex; // GLuint tex;
glGenTextures(1, &tex); // glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex); // // glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); // glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, width, height, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0); // glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture = new Texture(tex, width, height); // glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenRenderbuffers(1, &depth); // glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, tex, 0);
glBindRenderbuffer(GL_RENDERBUFFER, depth); // texture = new Texture(tex, width, height);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); // glGenRenderbuffers(1, &depth);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth); // glBindRenderbuffer(GL_RENDERBUFFER, depth);
unbind(); // glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT24, width, height);
// // glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
// unbind();
// bind();
// glGenFramebuffers(1, &fbo);
// // create a multisampled color attachment texture
// unsigned int textureColorBufferMultiSampled;
// glGenTextures(1, &textureColorBufferMultiSampled);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, width, height, GL_TRUE);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled, 0);
// // create a (also multisampled) renderbuffer object for depth and stencil attachments
// unsigned int rbo;
// glGenRenderbuffers(1, &rbo);
// glBindRenderbuffer(GL_RENDERBUFFER, rbo);
// glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
// glBindRenderbuffer(GL_RENDERBUFFER, 0);
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
// unbind();
} }
Framebuffer::~Framebuffer() { Framebuffer::~Framebuffer() {

View File

@ -7,12 +7,17 @@ Texture::Texture(unsigned int id, int width, int height) : id(id), width(width),
Texture::Texture(unsigned char* data, int width, int height) : width(width), height(height) { Texture::Texture(unsigned char* data, int width, int height) : width(width), height(height) {
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data); GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, width, height, GL_TRUE);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
} }
Texture::~Texture() { Texture::~Texture() {
@ -21,11 +26,15 @@ Texture::~Texture() {
void Texture::bind(){ void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
} }
void Texture::reload(unsigned char* data){ void Texture::reload(unsigned char* data){
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data); GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, width, height, GL_TRUE);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
} }

View File

@ -18,18 +18,18 @@
HudRenderer::HudRenderer() { HudRenderer::HudRenderer() {
float vertices[] = { // float vertices[] = {
// x y // // x y
-0.01f,-0.01f, // -0.01f,-0.01f,
0.01f, 0.01f, // 0.01f, 0.01f,
-0.01f, 0.01f, // -0.01f, 0.01f,
0.01f,-0.01f, // 0.01f,-0.01f,
}; // };
int attrs[] = { // int attrs[] = {
2, 0 //null terminator // 2, 0 //null terminator
}; // };
crosshair = new Mesh(vertices, 4, attrs); // crosshair = new Mesh(vertices, 4, attrs);
batch = new Batch2D(1024); batch = new Batch2D(1024);
uicamera = new Camera(glm::vec3(), Window::height / 1.0f); uicamera = new Camera(glm::vec3(), Window::height / 1.0f);
@ -38,7 +38,7 @@ HudRenderer::HudRenderer() {
} }
HudRenderer::~HudRenderer() { HudRenderer::~HudRenderer() {
delete crosshair; // delete crosshair;
delete batch; delete batch;
delete uicamera; delete uicamera;
} }
@ -61,7 +61,7 @@ void HudRenderer::drawDebug(Level* level, Assets* assets, int fps, bool occlusio
font->draw(batch, L"fps:", 16, 42, STYLE_OUTLINE); font->draw(batch, L"fps:", 16, 42, STYLE_OUTLINE);
font->draw(batch, std::to_wstring(fps), 44, 42, STYLE_OUTLINE); font->draw(batch, std::to_wstring(fps), 44, 42, STYLE_OUTLINE);
font->draw(batch, L"occlusion: "+std::to_wstring(occlusion), 16, 54, STYLE_OUTLINE); font->draw(batch, L"occlusion: "+std::to_wstring(occlusion), 16, 54, STYLE_OUTLINE);
batch->render(); // batch->render();
} }
@ -79,12 +79,17 @@ void HudRenderer::draw(Level* level, Assets* assets, bool devdata){
Texture* blocks = assets->getTexture("block"); Texture* blocks = assets->getTexture("block");
Texture* sprite = assets->getTexture("slot"); Texture* sprite = assets->getTexture("slot");
if (!Events::_cursor_locked) { batch->texture(nullptr);
batch->color = vec4(1.0f);
if (Events::_cursor_locked && !devdata) {
glLineWidth(2);
batch->line(Window::width/2, Window::height/2-6, Window::width/2, Window::height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(Window::width/2+6, Window::height/2, Window::width/2-6, Window::height/2, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(Window::width/2-5, Window::height/2-5, Window::width/2+5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(Window::width/2+5, Window::height/2-5, Window::width/2-5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
} }
batch->color = vec4(1.0f);
// batch->texture(sprite); // batch->texture(sprite);
batch->texture(nullptr);
// batch->sprite(Window::width/2-32, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f)); // batch->sprite(Window::width/2-32, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f));
// batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8, // batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
// 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f, // 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f,
@ -208,14 +213,14 @@ void HudRenderer::draw(Level* level, Assets* assets, bool devdata){
} }
} }
batch->render(); // batch->render();
if (Events::_cursor_locked && !devdata){ if (Events::_cursor_locked && !devdata){
Shader* crosshairShader = assets->getShader("crosshair"); // Shader* crosshairShader = assets->getShader("crosshair");
crosshairShader->use(); // crosshairShader->use();
crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width); // crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f)); // crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
glLineWidth(2.0f); // glLineWidth(2.0f);
crosshair->draw(GL_LINES); // crosshair->draw(GL_LINES);
} }
} }

View File

@ -10,7 +10,7 @@ class Mesh;
class HudRenderer { class HudRenderer {
Batch2D* batch; Batch2D* batch;
Camera* uicamera; Camera* uicamera;
Mesh* crosshair; // Mesh* crosshair;
public: public:
HudRenderer(); HudRenderer();
~HudRenderer(); ~HudRenderer();

View File

@ -20,6 +20,7 @@ int _png_load(const char* file, int* width, int* height){
png_bytepp row_pointers; png_bytepp row_pointers;
png_structp png_ptr; png_structp png_ptr;
GLuint texture; GLuint texture;
GLuint texturems;
int alpha; int alpha;
if ( !( f = fopen(file, "r" ) ) ) { if ( !( f = fopen(file, "r" ) ) ) {
@ -95,18 +96,53 @@ int _png_load(const char* file, int* width, int* height){
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
return 0; return 0;
} }
// configure second post-processing framebuffer
unsigned int framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(1, &texture); glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0,
alpha, GL_UNSIGNED_BYTE, (GLvoid *) image_data); alpha, GL_UNSIGNED_BYTE, (GLvoid *) image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
// glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); // we only need a color buffer
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
unsigned int framebufferms;
glGenFramebuffers(1, &framebufferms);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferms);
// create a multisampled color attachment texture
glGenTextures(1, &texturems);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texturems);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 16, GL_RGBA, t_width, t_height, GL_TRUE);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, texturems, 0);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAX_LEVEL, 3);
// glGenerateMipmap(GL_TEXTURE_2D_MULTISAMPLE);
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebufferms);
glBlitFramebuffer(0, 0, t_width, t_height, 0, 0, t_width, t_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
free( image_data ); free( image_data );

View File

@ -19,8 +19,8 @@
#define RUN_ZOOM 1.1f #define RUN_ZOOM 1.1f
#define C_ZOOM 0.1f #define C_ZOOM 0.1f
#define ZOOM_SPEED 16.0f #define ZOOM_SPEED 16.0f
#define DEFAULT_AIR_DAMPING 0.1f #define PLAYER_GROUNDED_DAMPING 1.0f
#define PLAYER_NOT_ONGROUND_DAMPING 10.0f #define PLAYER_NOT_GROUNDED_DAMPING 10.0f
#define CAMERA_SHAKING_OFFSET 0.025f #define CAMERA_SHAKING_OFFSET 0.025f
#define CAMERA_SHAKING_OFFSET_Y 0.031f #define CAMERA_SHAKING_OFFSET_Y 0.031f
#define CAMERA_SHAKING_SPEED 1.6f #define CAMERA_SHAKING_SPEED 1.6f
@ -82,7 +82,6 @@ void PlayerController::update_controls(float delta){
(Events::jpressed(GLFW_KEY_N) && player->flight == player->noclip)){ (Events::jpressed(GLFW_KEY_N) && player->flight == player->noclip)){
player->flight = !player->flight; player->flight = !player->flight;
if (player->flight){ if (player->flight){
hitbox->velocity.y += 1;
hitbox->grounded = false; hitbox->grounded = false;
} }
} }
@ -127,9 +126,9 @@ void PlayerController::update_controls(float delta){
dir.z -= camera->right.z; dir.z -= camera->right.z;
} }
hitbox->linear_damping = DEFAULT_AIR_DAMPING; hitbox->linear_damping = PLAYER_GROUNDED_DAMPING;
if (player->flight){ if (player->flight){
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING; hitbox->linear_damping = PLAYER_NOT_GROUNDED_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9; hitbox->velocity.y *= 1.0f - delta * 9;
if (Events::pressed(GLFW_KEY_SPACE)){ if (Events::pressed(GLFW_KEY_SPACE)){
hitbox->velocity.y += speed * delta * 9; hitbox->velocity.y += speed * delta * 9;
@ -142,27 +141,37 @@ void PlayerController::update_controls(float delta){
dir = normalize(dir); dir = normalize(dir);
if (!hitbox->grounded) if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING; hitbox->linear_damping = PLAYER_NOT_GROUNDED_DAMPING;
hitbox->velocity.x += dir.x * speed * delta * 9; hitbox->velocity.x += dir.x * speed * delta * 9;
hitbox->velocity.z += dir.z * speed * delta * 9; hitbox->velocity.z += dir.z * speed * delta * 9;
} }
if (Events::_cursor_locked){
player->camY += -Events::deltaY / Window::height * 2;
player->camX += -Events::deltaX / Window::height * 2;
if (player->camY < -radians(89.0f)){ // camera rotate
player->camY = -radians(89.0f);
if (Events::_cursor_locked){
float rotX = -Events::deltaX / Window::height * 2;
float rotY = -Events::deltaY / Window::height * 2;
if (zoom){
rotX /= 4;
rotY /= 4;
} }
if (player->camY > radians(89.0f)){ player->camX += rotX;
player->camY = radians(89.0f); player->camY += rotY;
if (player->camY < -radians(89.9f)){
player->camY = -radians(89.9f);
}
if (player->camY > radians(89.9f)){
player->camY = radians(89.9f);
} }
camera->rotation = mat4(1.0f); camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0); camera->rotate(player->camY, player->camX, 0);
} }
} }
// end camera rotate
void PlayerController::update_interaction(){ void PlayerController::update_interaction(){
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;

View File

@ -51,7 +51,7 @@ float calc_height(fnl_state *noise, int real_x, int real_z){
0.0f)*0.1f; 0.0f)*0.1f;
height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f; height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f;
height += fnlGetNoise3D(noise, real_x*0.4f*8+4565,real_z*0.4f*8+46456, 0.0f)*0.0625f; height += fnlGetNoise3D(noise, real_x*0.4f*8+4565,real_z*0.4f*8+46456, 0.0f)*0.0625f;
height += fnlGetNoise3D(noise, real_x*8,real_z*8, 0.0f)*0.03f*(fnlGetNoise3D(noise, -real_x*0.0125f*8-1000,real_z*0.0125f*8+2000, 0.0f)/2+0.5f); // height += fnlGetNoise3D(noise, real_x*8,real_z*8, 0.0f)*0.03f*(fnlGetNoise3D(noise, -real_x*0.0125f*8-1000,real_z*0.0125f*8+2000, 0.0f)/2+0.5f);
height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f; height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f;
height += 1.0f; height += 1.0f;
height *= 64.0f; height *= 64.0f;

View File

@ -135,43 +135,6 @@ void WorldRenderer::draw(World* world, Camera* camera, bool occlusion, bool devd
// draw 3D stuff here // draw 3D stuff here
batch3d->render(); batch3d->render();
if (devdata) {
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
vec3 point = vec3(camera->position.x+camera->front.x/1,
camera->position.y+camera->front.y/1,
camera->position.z+camera->front.z/1);
glDisable(GL_DEPTH_TEST);
glLineWidth(3.0f);
lineBatch->line(point.x, point.y, point.z,
point.x+0.1f, point.y, point.z,
0, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y, point.z+0.1f,
0, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y+0.1f, point.z,
0, 0, 0, 1);
lineBatch->render();
glLineWidth(1.0f);
lineBatch->line(point.x, point.y, point.z,
point.x+0.1f, point.y, point.z,
1, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y, point.z+0.1f,
0, 0, 1, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y+0.1f, point.z,
0, 1, 0, 1);
lineBatch->render();
glEnable(GL_DEPTH_TEST);
}
if (level->playerController->selectedBlockId != -1){ if (level->playerController->selectedBlockId != -1){
Block* selectedBlock = Block::blocks[level->playerController->selectedBlockId]; Block* selectedBlock = Block::blocks[level->playerController->selectedBlockId];
vec3 pos = level->playerController->selectedBlockPosition; vec3 pos = level->playerController->selectedBlockPosition;
@ -185,4 +148,41 @@ void WorldRenderer::draw(World* world, Camera* camera, bool occlusion, bool devd
} }
lineBatch->render(); lineBatch->render();
} }
if (devdata) {
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
vec3 point = vec3(camera->position.x+camera->front.x/1,
camera->position.y+camera->front.y/1,
camera->position.z+camera->front.z/1);
glDisable(GL_DEPTH_TEST);
glLineWidth(4.0f);
lineBatch->line(point.x, point.y, point.z,
point.x+0.1f, point.y, point.z,
0, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y, point.z+0.1f,
0, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y+0.1f, point.z,
0, 0, 0, 1);
lineBatch->render();
glLineWidth(2.0f);
lineBatch->line(point.x, point.y, point.z,
point.x+0.1f, point.y, point.z,
1, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y, point.z+0.1f,
0, 0, 1, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y+0.1f, point.z,
0, 1, 0, 1);
lineBatch->render();
glEnable(GL_DEPTH_TEST);
}
} }