245 lines
8.5 KiB
C
245 lines
8.5 KiB
C
#include "rendering.h"
|
|
#include <string.h>
|
|
#include "cube.h"
|
|
#include "matrix.h"
|
|
#include "util.h"
|
|
|
|
GLuint gen_plant_buffer(float x, float y, float z, float n, int w) {
|
|
GLfloat *data = malloc_faces(10, 4);
|
|
float ao = 0;
|
|
float light = 1;
|
|
make_plant(data, ao, light, x, y, z, n, w, 45);
|
|
return gen_faces(10, 4, data);
|
|
}
|
|
|
|
GLuint gen_player_buffer(float x, float y, float z, float rx, float ry) {
|
|
GLfloat *data = malloc_faces(10, 6);
|
|
make_player(data, x, y, z, rx, ry);
|
|
return gen_faces(10, 6, data);
|
|
}
|
|
|
|
GLuint gen_text_buffer(float x, float y, float n, char *text) {
|
|
int length = strlen(text);
|
|
GLfloat *data = malloc_faces(4, length);
|
|
for (int i = 0; i < length; i++) {
|
|
make_character(data + i * 24, x, y, n / 2, n, text[i]);
|
|
x += n;
|
|
}
|
|
return gen_faces(4, length, data);
|
|
}
|
|
|
|
GLuint gen_ui_buffer(float x, float y, float n, char spritesheet_index) {
|
|
/** Thanks CraftNG :D **/
|
|
GLfloat *data = malloc_faces(4, 1);
|
|
//make_character(data, x, y, n, n * 2, spritesheet_index);
|
|
make_ui_quad(data, x, y, n, n, spritesheet_index);
|
|
return gen_faces(4, 1, data);
|
|
}
|
|
|
|
GLuint gen_logo_buffer(float x, float y, float n, char spritesheet_index) {
|
|
GLfloat *data = malloc_faces(4, 1);
|
|
//make_character(data, x, y, n, n * 2, spritesheet_index);
|
|
make_logo_quad(data, x, y, n, n, spritesheet_index);
|
|
return gen_faces(4, 1, data);
|
|
}
|
|
|
|
/*GLuint gen_background_buffer(float x, float y, float n, char spritesheet_index) {
|
|
GLfloat *data = malloc_faces(4, 1);
|
|
//make_character(data, x, y, n, n * 2, spritesheet_index);
|
|
make_logo_quad(data, x, y, n, n, spritesheet_index);
|
|
return gen_faces(4, 1, data);
|
|
}*/
|
|
|
|
void draw_triangles_3d_ao(Attrib *attrib, GLuint buffer, int count) {
|
|
glEnable(GL_BLEND); //*
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
glEnable(GL_CULL_FACE);
|
|
glBlendEquation(GL_FUNC_ADD);
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glEnableVertexAttribArray(attrib->position);
|
|
glEnableVertexAttribArray(attrib->normal);
|
|
glEnableVertexAttribArray(attrib->uv);
|
|
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 10, 0);
|
|
glVertexAttribPointer(attrib->normal, 3, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 10, (GLvoid *)(sizeof(GLfloat) * 3));
|
|
glVertexAttribPointer(attrib->uv, 4, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 10, (GLvoid *)(sizeof(GLfloat) * 6));
|
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
|
glDisableVertexAttribArray(attrib->position);
|
|
glDisableVertexAttribArray(attrib->normal);
|
|
glDisableVertexAttribArray(attrib->uv);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glDisable(GL_CULL_FACE);
|
|
glDisable(GL_BLEND);
|
|
}
|
|
|
|
void draw_triangles_3d_text(Attrib *attrib, GLuint buffer, int count) {
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glEnableVertexAttribArray(attrib->position);
|
|
glEnableVertexAttribArray(attrib->uv);
|
|
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 5, 0);
|
|
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 5, (GLvoid *)(sizeof(GLfloat) * 3));
|
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
|
glDisableVertexAttribArray(attrib->position);
|
|
glDisableVertexAttribArray(attrib->uv);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
}
|
|
|
|
void draw_triangles_3d(Attrib *attrib, GLuint buffer, int count) {
|
|
glEnable(GL_BLEND); //*
|
|
glEnable(GL_CULL_FACE);
|
|
glBlendEquation(GL_FUNC_ADD);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glEnableVertexAttribArray(attrib->position);
|
|
glEnableVertexAttribArray(attrib->normal);
|
|
glEnableVertexAttribArray(attrib->uv);
|
|
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 8, 0);
|
|
glVertexAttribPointer(attrib->normal, 3, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 3));
|
|
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 6));
|
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
|
glDisableVertexAttribArray(attrib->position);
|
|
glDisableVertexAttribArray(attrib->normal);
|
|
glDisableVertexAttribArray(attrib->uv);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glDisable(GL_CULL_FACE);
|
|
glDisable(GL_BLEND);
|
|
}
|
|
|
|
void draw_triangles_2d(Attrib *attrib, GLuint buffer, int count) {
|
|
glEnable(GL_BLEND); //*
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glEnableVertexAttribArray(attrib->position);
|
|
glEnableVertexAttribArray(attrib->uv);
|
|
glVertexAttribPointer(attrib->position, 2, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 4, 0);
|
|
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
|
|
sizeof(GLfloat) * 4, (GLvoid *)(sizeof(GLfloat) * 2));
|
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
|
glDisableVertexAttribArray(attrib->position);
|
|
glDisableVertexAttribArray(attrib->uv);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glDisable(GL_BLEND);
|
|
}
|
|
|
|
void draw_lines(Attrib *attrib, GLuint buffer, int components, int count) {
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glEnableVertexAttribArray(attrib->position);
|
|
glVertexAttribPointer(
|
|
attrib->position, components, GL_FLOAT, GL_FALSE, 0, 0);
|
|
glDrawArrays(GL_LINES, 0, count);
|
|
glDisableVertexAttribArray(attrib->position);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
}
|
|
|
|
void draw_chunk(Attrib *attrib, Chunk *chunk) {
|
|
draw_triangles_3d_ao(attrib, chunk->buffer, chunk->faces * 6);
|
|
}
|
|
|
|
void draw_item(Attrib *attrib, GLuint buffer, int count) {
|
|
draw_triangles_3d_ao(attrib, buffer, count);
|
|
}
|
|
|
|
void draw_text(Attrib *attrib, GLuint buffer, int length) {
|
|
glEnable(GL_BLEND);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
draw_triangles_2d(attrib, buffer, length * 6);
|
|
glDisable(GL_BLEND);
|
|
}
|
|
|
|
void draw_signs(Attrib *attrib, Chunk *chunk) {
|
|
glEnable(GL_POLYGON_OFFSET_FILL);
|
|
glPolygonOffset(-8, -1024);
|
|
draw_triangles_3d_text(attrib, chunk->sign_buffer, chunk->sign_faces * 6);
|
|
glDisable(GL_POLYGON_OFFSET_FILL);
|
|
}
|
|
|
|
void draw_sign(Attrib *attrib, GLuint buffer, int length) {
|
|
glEnable(GL_POLYGON_OFFSET_FILL);
|
|
glPolygonOffset(-8, -1024);
|
|
draw_triangles_3d_text(attrib, buffer, length * 6);
|
|
glDisable(GL_POLYGON_OFFSET_FILL);
|
|
}
|
|
|
|
void draw_cube(Attrib *attrib, GLuint buffer) {
|
|
//glEnable(GL_BLEND);
|
|
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
//glBlendEquation(GL_FUNC_ADD);
|
|
draw_item(attrib, buffer, 36);
|
|
//glDisable(GL_BLEND);
|
|
}
|
|
|
|
void draw_plant(Attrib *attrib, GLuint buffer) {
|
|
draw_item(attrib, buffer, 24);
|
|
}
|
|
|
|
void draw_player(Attrib *attrib, Player *player) {
|
|
draw_cube(attrib, player->buffer);
|
|
}
|
|
|
|
void draw_ui(Attrib *attrib, GLuint buffer) {
|
|
glEnable(GL_BLEND); //*
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
draw_triangles_2d(attrib, buffer, 6); //wtf is 6.
|
|
glDisable(GL_BLEND);
|
|
}
|
|
|
|
void draw_logo(Attrib *attrib, GLuint buffer) {
|
|
glEnable(GL_BLEND);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
//glUseProgram(attrib->program);
|
|
//glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
|
|
//glUniform1i(attrib->sampler, 1);
|
|
//glUniform1i(attrib->extra1, 0);
|
|
draw_triangles_2d(attrib, buffer, 6); //wtf is 6.
|
|
glDisable(GL_BLEND);
|
|
}
|
|
|
|
GLuint gen_crosshair_buffer(int width, int height, int scale) {
|
|
int x = width / 2;
|
|
int y = height / 2;
|
|
int p = 11 * scale;
|
|
float data[] = {
|
|
x, y - p, x, y + p,
|
|
x - p, y, x + p, y
|
|
};
|
|
return gen_buffer(sizeof(data), data);
|
|
}
|
|
|
|
void render_text(
|
|
Attrib *attrib, int justify, float x, float y, float n, char *text,
|
|
int win_width, int win_height)
|
|
{
|
|
float matrix[16];
|
|
set_matrix_2d(matrix, win_width, win_height);
|
|
glUseProgram(attrib->program);
|
|
glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
|
|
glUniform1i(attrib->sampler, 1);
|
|
glUniform1i(attrib->extra1, 0);
|
|
int length = strlen(text);
|
|
x -= n * justify * (length - 1) / 2;
|
|
GLuint buffer = gen_text_buffer(x, y, n, text);
|
|
draw_text(attrib, buffer, length);
|
|
del_buffer(buffer);
|
|
}
|
|
|
|
void render_crosshairs(Attrib *attrib, int width, int height, int scale) {
|
|
float matrix[16];
|
|
set_matrix_2d(matrix, width, height);
|
|
glUseProgram(attrib->program);
|
|
glLineWidth(4 * scale);
|
|
glEnable(GL_COLOR_LOGIC_OP);
|
|
glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
|
|
GLuint crosshair_buffer = gen_crosshair_buffer(width, height, scale);
|
|
draw_lines(attrib, crosshair_buffer, 2, 4);
|
|
del_buffer(crosshair_buffer);
|
|
glDisable(GL_COLOR_LOGIC_OP);
|
|
}
|