crosshair

This commit is contained in:
onezplpl 2024-07-18 17:04:40 -04:00
parent 328be44ed0
commit e799d50f77
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
5 changed files with 94 additions and 18 deletions

46
main.c
View file

@ -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);