diff --git a/control.c b/control.c index fc89c10..09cabce 100644 --- a/control.c +++ b/control.c @@ -134,8 +134,10 @@ struct v3f control_rtrace(double cx, double cy, vec3 pos, int mode) { vec3_add(temp_pos, temp_pos, temp_dir); - if (gen_cube(temp_pos[0],temp_pos[1],temp_pos[2],placeChunk,0,-1) != 0) { - if (mode == 0) vec3_sub(temp_pos,temp_pos,temp_dir); + if (gen_cube(temp_pos[0], temp_pos[1], temp_pos[2], placeChunk, 0, -1) != + 0) { + if (mode == 0) + vec3_sub(temp_pos, temp_pos, temp_dir); break; } } @@ -149,15 +151,21 @@ struct v3f control_rtrace(double cx, double cy, vec3 pos, int mode) { void control_lclick(double cx, double cy, vec3 pos) { struct v3f highlighted = control_rtrace(cx, cy, pos, 1); struct chunk placeChunk; - int cube = gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,-1); - if (cube == 0) return; - gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,0); + int cube = gen_cube(highlighted.pos[0], highlighted.pos[1], + highlighted.pos[2], placeChunk, 0, -1); + if (cube == 0) + return; + gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2], + placeChunk, 0, 0); } void control_rclick(double cx, double cy, vec3 pos) { struct v3f highlighted = control_rtrace(cx, cy, pos, 0); struct chunk placeChunk; - int cube = gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,-1); - if (cube != 0) return; - gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,1); + int cube = gen_cube(highlighted.pos[0], highlighted.pos[1], + highlighted.pos[2], placeChunk, 0, -1); + if (cube != 0) + return; + gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2], + placeChunk, 0, 1); } \ No newline at end of file diff --git a/cubes.c b/cubes.c index 24d5119..3cda73e 100644 --- a/cubes.c +++ b/cubes.c @@ -22,6 +22,38 @@ int oz = -1024; struct chunk *chunk_old = NULL; +void cubes_rect(struct v3f *cube, int *i, double x1, double x2, double y1, + double y2, int is_text, int text_x, int text_y) { + struct v3f a2 = {{x1, y1, 0}}; + if (is_text) { + a2.pos[0] = TEXT_GAP_H * text_x; + a2.pos[1] = TEXT_GAP_V * text_y; + a2.pos[2] = 0; + } + struct v3f b = a2; + struct v3f c = a2; + struct v3f d = a2; + + if (is_text) { + b.pos[0] += TEXT_GAP_H; + c.pos[1] += TEXT_GAP_V; + d.pos[0] += TEXT_GAP_H; + d.pos[1] += TEXT_GAP_V; + } else { + b.pos[0]++; + c.pos[1]++; + d.pos[0]++; + d.pos[1]++; + } + + cube[*i] = a2; + cube[*i + 1] = b; + cube[*i + 2] = c; + cube[*i + 3] = c; + cube[*i + 4] = b; + cube[*i + 5] = d; +} + void cubes_face(struct v3f *cube, int i, int x, int y, int z, int x2, int y2, int z2, int *j, int is_text, struct chunk dat, int ci, int a3) { struct v3i a = {{x, y, z}}; @@ -137,11 +169,12 @@ void cubes_free() { free(chunk_old); } void cubes_refresh(int x, int y, int z, struct chunk dat) { for (int i = 0; i < CHUNK_ALL; i++) { if (chunk_old[i].exists != 0 && abs(((int)chunk_old[i].x) - x) <= 1 && - abs(((int)chunk_old[i].y) - y) <= 1 && abs(((int)chunk_old[i].z) - z) <= 1) { + abs(((int)chunk_old[i].y) - y) <= 1 && + abs(((int)chunk_old[i].z) - z) <= 1) { chunk_old[i].exists = 0; } } - gen_save(dat); + gen_save(dat); } int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO, diff --git a/cubes.h b/cubes.h index eb2f12d..0f72abe 100644 --- a/cubes.h +++ b/cubes.h @@ -7,7 +7,7 @@ #define CHUNK_RADIUS_V 4 // 4 #define TEXT_GAP_H 1 / 6.0 -#define TEXT_GAP_V 1 / 6.0 +#define TEXT_GAP_V 1 / 7.0 #define CHUNK_DIAMETER_H (CHUNK_RADIUS_H * 2 + 1) #define CHUNK_DIAMETER_V (CHUNK_RADIUS_V * 2 + 1) @@ -15,6 +15,7 @@ #define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH #define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL #define CBUF_ALL 2 +#define UI_ALL 1024 #define CTRI_ALL CBLOCK_ALL * 18 * sizeof(struct v3f) @@ -43,4 +44,6 @@ int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO, void cubes_free(); void cubes_refresh(int x, int y, int z, struct chunk dat); +void cubes_rect(struct v3f *cube, int *i, double x1, double x2, double y1, + double y2, int is_text, int text_x, int text_y); #endif \ No newline at end of file diff --git a/main.c b/main.c index 9f8b4c8..ea8174a 100644 --- a/main.c +++ b/main.c @@ -37,13 +37,16 @@ static const char *fragment_shader_text = "in vec3 v_pos_out;\n" "uniform sampler2D sampler_tex;\n" "uniform vec3 center;\n" + "uniform int use_shading;\n" "float dist;\n" "void main()\n" "{\n" //" color=vec4(v_tex.rg,0.5,0.5);\n" - " if (v_tex.x < 0.0 || v_tex.y < 0.0) { discard; }\n" + " if (v_tex.x < 0.0 || v_tex.y < 0.0 || texture(sampler_tex, v_tex).a " + "== 0.0) { discard; }\n" " dist = distance(v_pos_out, center + vec3(8,8,8))/65;\n" " if (dist > 1.0) { dist = 1.0; }\n" + " if (use_shading == 0) { dist = 0.0; }\n" " color=vec4(texture(sampler_tex, v_tex).rgb,1.0)*(1.0-(dist*dist)) + " "vec4(0.2,0.5,0.7,1.0)*(dist*dist);\n" "}\n"; @@ -144,8 +147,8 @@ int main(void) { struct v3f *cube = malloc(CTRI_ALL); struct v3f *text = malloc(CTRI_ALL); - struct v3f *cubeO = malloc(CTRI_ALL); - struct v3f *textO = malloc(CTRI_ALL); + struct v3f *cubeO = malloc(CTRI_ALL + UI_ALL * 18 * sizeof(struct v3f)); + struct v3f *textO = malloc(CTRI_ALL + UI_ALL * 18 * sizeof(struct v3f)); int bleh = (CHUNK_DIAMETER_H) * (CHUNK_DIAMETER_V); int *i2 = calloc(CHUNK_ALL, sizeof(int)); @@ -158,13 +161,13 @@ int main(void) { glGenBuffers(2, vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); - glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, text, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, CTRI_ALL + UI_ALL* 18 * sizeof(struct v3f), textO, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct v3f), (void *)0); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); - glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, cube, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, CTRI_ALL + UI_ALL * 18 * sizeof(struct v3f), cubeO, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct v3f), (void *)0); @@ -185,11 +188,12 @@ int main(void) { GLint tex_location = glGetUniformLocation(program, "sampler_tex"); GLint mvp_location = glGetUniformLocation(program, "mvp"); GLint center_location = glGetUniformLocation(program, "center"); + GLint use_shading = glGetUniformLocation(program, "use_shading"); unsigned char *pixels; int tx, ty, ch; - pixels = stbi_load("./test.png", &tx, &ty, &ch, 3); + pixels = stbi_load("./test.png", &tx, &ty, &ch, 4); glClearColor(0.2f, 0.5f, 0.7f, 1.0f); glEnable(GL_TEXTURE_2D); @@ -326,7 +330,7 @@ int main(void) { glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tx, ty, 0, GL_RGB, GL_UNSIGNED_BYTE, + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tx, ty, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -336,7 +340,35 @@ int main(void) { glBindVertexArray(vertex_array); + glUniform1i(use_shading, 1); + glDrawArrays(GL_TRIANGLES, 0, cube_count); + + glClear(GL_DEPTH_BUFFER_BIT); + + glUniform1i(use_shading, 0); + + int iz = CBLOCK_ALL * 18; + cubes_rect(cubeO, &iz, -0.5,0.5, -0.5, 0.5, 0, 0, 6); + cubes_rect(textO, &iz, -0.5, 0.5, -0.5, 0.5, 1, 0, 6); + + mat4x4_identity(v); + mat4x4_ortho(p, -ratio*15, ratio*15, -1.f*15, 1.f*15, 1.0f, -1.0f); + mat4x4_mul(mvp, p, v); + mat4x4_mul(mvp, mvp, m); + + glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp); + + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); + glBufferSubData(GL_ARRAY_BUFFER, CTRI_ALL, + (long)6 * sizeof(struct v3f), &textO[iz]); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); + glBufferSubData(GL_ARRAY_BUFFER, CTRI_ALL, + (long)6 * sizeof(struct v3f), &cubeO[iz]); + + glBindVertexArray(vertex_array); + + glDrawArrays(GL_TRIANGLES, CBLOCK_ALL * 18, 6); } free(cube); diff --git a/test.png b/test.png index a6a41c8..587bcde 100644 Binary files a/test.png and b/test.png differ