even more optimization
This commit is contained in:
parent
e66093d684
commit
04a03877a2
3 changed files with 109 additions and 40 deletions
65
main.c
65
main.c
|
@ -45,7 +45,8 @@ static const char *fragment_shader_text =
|
|||
" if (v_tex.x < 0.0 || v_tex.y < 0.0) { discard; }\n"
|
||||
" dist = distance(v_pos_out, center + vec3(8,8,8))/65;\n"
|
||||
" if (dist > 1.0) { dist = 1.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"
|
||||
" 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";
|
||||
|
||||
int isCursor = 0;
|
||||
|
@ -79,9 +80,12 @@ struct args {
|
|||
int *cube_count;
|
||||
Vertex **cube;
|
||||
Vertex **text;
|
||||
Vertex **cubeO;
|
||||
Vertex **textO;
|
||||
vec3 pos;
|
||||
GLuint *vertex_buffer;
|
||||
int *is_render;
|
||||
int *i2;
|
||||
};
|
||||
|
||||
void *render_chunks(void *args) {
|
||||
|
@ -94,8 +98,9 @@ void *render_chunks(void *args) {
|
|||
*args2.is_render = 0;
|
||||
|
||||
*cube_count =
|
||||
gen_cubes(*args2.cube, *args2.text, args2.pos[0] / CHUNK_LENGTH,
|
||||
args2.pos[1] / CHUNK_LENGTH, args2.pos[2] / CHUNK_LENGTH, 0, 0);
|
||||
gen_cubes(*args2.cube, *args2.text, *args2.cubeO, *args2.textO,
|
||||
args2.pos[0] / CHUNK_LENGTH, args2.pos[1] / CHUNK_LENGTH,
|
||||
args2.pos[2] / CHUNK_LENGTH, 0, 0, args2.i2);
|
||||
|
||||
*args2.is_render = 2;
|
||||
|
||||
|
@ -125,9 +130,13 @@ int main(void) {
|
|||
|
||||
Vertex *cube = malloc(CTRI_ALL);
|
||||
Vertex *text = malloc(CTRI_ALL);
|
||||
Vertex *cubeO = malloc(CTRI_ALL);
|
||||
Vertex *textO = malloc(CTRI_ALL);
|
||||
int bleh = (CHUNK_DIAMETER_H + MAX_R * 2) * (CHUNK_DIAMETER_V + MAX_R * 2);
|
||||
int *i2 = calloc(bleh, sizeof(int));
|
||||
|
||||
int cube_count = gen_cubes(cube, text, 10240 / CHUNK_LENGTH, 64 / CHUNK_LENGTH,
|
||||
10240 / CHUNK_LENGTH, 0, 1);
|
||||
int cube_count = gen_cubes(cube, text, cubeO, textO, 10240 / CHUNK_LENGTH,
|
||||
64 / CHUNK_LENGTH, 10240 / CHUNK_LENGTH, 0, 1, i2);
|
||||
GLuint vertex_buffer[2];
|
||||
|
||||
GLuint vertex_array;
|
||||
|
@ -176,13 +185,14 @@ int main(void) {
|
|||
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||
|
||||
vec3 pos = {10240, 48, 10240};
|
||||
vec3 oldPos = {0,0,0};
|
||||
vec3 oldPos = {0, 0, 0};
|
||||
Chunk ch7;
|
||||
ch7.exists = 0;
|
||||
int state = cube_exists(pos[0], pos[1], pos[2], ch7, 0, 0);
|
||||
|
||||
while (1) {
|
||||
if (state != cube_exists(pos[0], pos[1], pos[2], ch7, 0, 0)) break;
|
||||
if (state != cube_exists(pos[0], pos[1], pos[2], ch7, 0, 0))
|
||||
break;
|
||||
pos[1] += (state ? 1 : -1);
|
||||
}
|
||||
|
||||
|
@ -208,8 +218,10 @@ int main(void) {
|
|||
mat4x4_identity(m);
|
||||
mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000);
|
||||
|
||||
vec3 direction = {cosf(cy) * sinf(cx), sinf(cy), cosf(cy) * cosf(cx)};
|
||||
vec3 right = {sinf(cx - 3.14f / 2.0f), 0, cosf(cx - 3.14f / 2.0f)};
|
||||
vec3 direction = {cosf(cy) * sinf(cx) * 0.3, sinf(cy) * 0.3,
|
||||
cosf(cy) * cosf(cx) * 0.3};
|
||||
vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.3, 0,
|
||||
cosf(cx - 3.14f / 2.0f) * 0.3};
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
vec3_add(pos, pos, direction);
|
||||
|
@ -238,11 +250,11 @@ int main(void) {
|
|||
mat4x4_mul(mvp, mvp, m);
|
||||
|
||||
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
|
||||
glUniform3fv(center_location, 1, (const GLfloat *)(&(oldPos[0])));
|
||||
glUniform3fv(center_location, 1, (const GLfloat *)(&(pos[0])));
|
||||
|
||||
int ax = (int)(oldPos[0]/ CHUNK_LENGTH) - (int)(pos[0] / CHUNK_LENGTH);
|
||||
int ay = (int)(oldPos[1]/ CHUNK_LENGTH) - (int)(pos[1] / CHUNK_LENGTH);
|
||||
int az = (int)(oldPos[2]/ CHUNK_LENGTH) - (int)(pos[2] / CHUNK_LENGTH);
|
||||
int ax = (int)(oldPos[0] / CHUNK_LENGTH) - (int)(pos[0] / CHUNK_LENGTH);
|
||||
int ay = (int)(oldPos[1] / CHUNK_LENGTH) - (int)(pos[1] / CHUNK_LENGTH);
|
||||
int az = (int)(oldPos[2] / CHUNK_LENGTH) - (int)(pos[2] / CHUNK_LENGTH);
|
||||
|
||||
if ((ax != 0 || ay != 0 || az != 0) && last_render < 0) {
|
||||
struct args args;
|
||||
|
@ -250,18 +262,21 @@ int main(void) {
|
|||
|
||||
args.cube = &cube;
|
||||
args.text = &text;
|
||||
args.pos[0] = pos[0];
|
||||
args.cubeO = &cubeO;
|
||||
args.textO = &textO;
|
||||
args.pos[0] = pos[0] + direction[0] * CHUNK_LENGTH;
|
||||
args.pos[1] = pos[1];
|
||||
args.pos[2] = pos[2];
|
||||
args.pos[2] = pos[2] + direction[2] * CHUNK_LENGTH;
|
||||
args.vertex_buffer = vertex_buffer;
|
||||
args.is_render = &is_render;
|
||||
args.i2 = i2;
|
||||
|
||||
last_render = 120;
|
||||
last_render = 40;
|
||||
|
||||
if (thread_id == 0) {
|
||||
oldPos[0] = (int)(pos[0]/ CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[1] = (int)(pos[1]/ CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[2] = (int)(pos[2]/ CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[0] = (int)(args.pos[0] / CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[1] = (int)(args.pos[1] / CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[2] = (int)(args.pos[2] / CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
pthread_create(&(thread_id), NULL, render_chunks, (void *)&(args));
|
||||
}
|
||||
}
|
||||
|
@ -274,11 +289,11 @@ int main(void) {
|
|||
if (is_render == 2) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
|
||||
text);
|
||||
textO); // text);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
|
||||
cube);
|
||||
cubeO); // cube);
|
||||
if (thread_id != 0)
|
||||
pthread_join(thread_id, NULL);
|
||||
thread_id = 0;
|
||||
|
@ -299,6 +314,14 @@ int main(void) {
|
|||
glBindVertexArray(vertex_array);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, cube_count);
|
||||
|
||||
/*/
|
||||
for (int ch = 0; ch < bleh; ch++) {
|
||||
int o = ch * BLOCK_ALL * 18 * (CHUNK_DIAMETER_H);
|
||||
if (i2[ch] < o) continue;
|
||||
glDrawArrays(GL_TRIANGLES, o, i2[ch] - o);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
free(cube);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue