more stuff
This commit is contained in:
parent
7bf40240fe
commit
48ecd8d9e8
5 changed files with 96 additions and 48 deletions
7
const.h
7
const.h
|
@ -5,15 +5,18 @@ typedef struct Vertex {
|
||||||
float pos[3];
|
float pos[3];
|
||||||
} Vertex;
|
} Vertex;
|
||||||
|
|
||||||
#define CHUNK_LENGTH 16
|
#define CHUNK_LENGTH 64 //32
|
||||||
#define CHUNK_RADIUS_H 8
|
#define CHUNK_RADIUS_H 8
|
||||||
#define CHUNK_RADIUS_V 4
|
#define CHUNK_RADIUS_V 4
|
||||||
|
|
||||||
|
#define CTRI_ALL BLOCK_ALL * 18 * sizeof(Vertex)
|
||||||
|
#define TEXT_GAP_H 1 / 3.0
|
||||||
|
#define TEXT_GAP_V 1 / 1.0
|
||||||
|
|
||||||
#define CHUNK_DIAMETER_H (CHUNK_RADIUS_H * 2 + 1)
|
#define CHUNK_DIAMETER_H (CHUNK_RADIUS_H * 2 + 1)
|
||||||
#define CHUNK_DIAMETER_V (CHUNK_RADIUS_V * 2 + 1)
|
#define CHUNK_DIAMETER_V (CHUNK_RADIUS_V * 2 + 1)
|
||||||
#define CHUNK_ALL CHUNK_DIAMETER_H *CHUNK_DIAMETER_H *CHUNK_DIAMETER_V
|
#define CHUNK_ALL CHUNK_DIAMETER_H *CHUNK_DIAMETER_H *CHUNK_DIAMETER_V
|
||||||
#define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH
|
#define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH
|
||||||
#define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL
|
#define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL
|
||||||
#define CTRI_ALL BLOCK_ALL * 18 * sizeof(Vertex)
|
|
||||||
|
|
||||||
#endif
|
#endif
|
60
cubes.c
60
cubes.c
|
@ -1,27 +1,50 @@
|
||||||
#include "const.h"
|
#include "const.h"
|
||||||
|
//#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
// placeholder function
|
// placeholder function
|
||||||
int cubeExists(int x, int y, int z) {
|
int cubeExists(int x, int y, int z) {
|
||||||
return ((x % 3) == (y % 4) && (y % 5) == (z % 6));
|
srand(x ^ y ^ z);
|
||||||
|
return ((rand() % 3 == 0) && y == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_face(Vertex *cube, int i, int x, int y, int z, int j) {
|
void gen_face(Vertex *cube, int i, int x, int y, int z, int j, int is_text) {
|
||||||
Vertex a = {{x,y,z}};
|
Vertex a = {{x, y, z}};
|
||||||
|
|
||||||
|
if (is_text) {
|
||||||
|
a.pos[i]--;
|
||||||
|
int exists =
|
||||||
|
cubeExists(x, y, z) != cubeExists(a.pos[0], a.pos[1], a.pos[2]);
|
||||||
|
a.pos[0] = exists ? TEXT_GAP_H * i : -1.0;
|
||||||
|
a.pos[1] = exists ? TEXT_GAP_V * i : -1.0;
|
||||||
|
a.pos[2] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
Vertex b = a;
|
Vertex b = a;
|
||||||
b.pos[(i==0)?1:0]++;
|
|
||||||
Vertex c = a;
|
Vertex c = a;
|
||||||
c.pos[(i==2)?1:2]++;
|
Vertex d = a;
|
||||||
Vertex d = c;
|
|
||||||
d.pos[(i==0)?1:0]++;
|
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[(i == 0) ? 1 : 0]++;
|
||||||
|
c.pos[(i == 2) ? 1 : 2]++;
|
||||||
|
d.pos[(i == 0) ? 1 : 0]++;
|
||||||
|
d.pos[(i == 2) ? 1 : 2]++;
|
||||||
|
}
|
||||||
|
|
||||||
cube[j] = a;
|
cube[j] = a;
|
||||||
cube[j+1] = b;
|
cube[j + 1] = b;
|
||||||
cube[j+2] = c;
|
cube[j + 2] = c;
|
||||||
cube[j+3] = c;
|
cube[j + 3] = c;
|
||||||
cube[j+4] = b;
|
cube[j + 4] = b;
|
||||||
cube[j+5] = d;
|
cube[j + 5] = d;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_cubes(Vertex *cube, int x, int y, int z) {
|
void gen_cubes(Vertex *cube, int x, int y, int z, int is_text) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (int y2 = y; y2 < y + CHUNK_LENGTH; y2++) {
|
for (int y2 = y; y2 < y + CHUNK_LENGTH; y2++) {
|
||||||
int y3 = y2;
|
int y3 = y2;
|
||||||
|
@ -29,9 +52,14 @@ void gen_cubes(Vertex *cube, int x, int y, int z) {
|
||||||
int x3 = x2;
|
int x3 = x2;
|
||||||
for (int z2 = z; z2 < z + CHUNK_LENGTH; z2++) {
|
for (int z2 = z; z2 < z + CHUNK_LENGTH; z2++) {
|
||||||
int z3 = z2;
|
int z3 = z2;
|
||||||
gen_face(cube, 0, x3, y3, z3, i);
|
|
||||||
gen_face(cube, 1, x3, y3, z3, i + 6);
|
if (is_text) {
|
||||||
gen_face(cube, 2, x3, y3, z3, i + 12);
|
x3 = y3 = z3 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
gen_face(cube, 0, x2, y2, z2, i, is_text);
|
||||||
|
gen_face(cube, 1, x2, y2, z2, i + 6, is_text);
|
||||||
|
gen_face(cube, 2, x2, y2, z2, i + 12, is_text);
|
||||||
i += 18;
|
i += 18;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
cubes.h
2
cubes.h
|
@ -1,3 +1,3 @@
|
||||||
#include "const.h"
|
#include "const.h"
|
||||||
|
|
||||||
void gen_cubes(Vertex *cube, int x, int y, int z);
|
void gen_cubes(Vertex *cube, int x, int y, int z, int is_text);
|
75
main.c
75
main.c
|
@ -5,11 +5,12 @@
|
||||||
#include "gl.h"
|
#include "gl.h"
|
||||||
#define GLFW_INCLUDE_NONE
|
#define GLFW_INCLUDE_NONE
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
#include "const.h"
|
#include "const.h"
|
||||||
#include "cubes.h"
|
#include "cubes.h"
|
||||||
#include "linmath.h"
|
#include "linmath.h"
|
||||||
#include "stb_image.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -17,13 +18,13 @@
|
||||||
static const char *vertex_shader_text =
|
static const char *vertex_shader_text =
|
||||||
"#version 330\n"
|
"#version 330\n"
|
||||||
"layout (location = 0) in vec3 vPos;"
|
"layout (location = 0) in vec3 vPos;"
|
||||||
//"layout (location = 1) in vec3 vOff;"
|
"layout (location = 1) in vec3 vOff;"
|
||||||
"uniform mat4 MVP;\n"
|
"uniform mat4 MVP;\n"
|
||||||
"out vec2 vTex;\n"
|
"out vec2 vTex;\n"
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" gl_Position = MVP * vec4(vPos, 1.0);\n"
|
" gl_Position = MVP * vec4(vPos, 1.0);\n"
|
||||||
" vTex=0.5*vPos.xy + vec2(0.5,0);\n"
|
" vTex=vec2(vOff);\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
static const char *fragment_shader_text =
|
static const char *fragment_shader_text =
|
||||||
|
@ -34,13 +35,22 @@ static const char *fragment_shader_text =
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
//" color=vec4(vTex.rg,0.5,0.5);\n"
|
//" color=vec4(vTex.rg,0.5,0.5);\n"
|
||||||
" color=vec4(texture(texSampler, vTex).rg+vTex/10.0,0.5,0.5);\n"
|
" if (vTex.x < 0.0 || vTex.y < 0.0) { discard; }\n"
|
||||||
|
" color=vec4(texture(texSampler, vTex).rgb,0.5);\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
|
int isCursor = 0;
|
||||||
|
|
||||||
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||||
int mods) {
|
int mods) {
|
||||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
/*if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||||
|
*/
|
||||||
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
||||||
|
isCursor = glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
|
||||||
|
glfwSetInputMode(window, GLFW_CURSOR,
|
||||||
|
isCursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double cx = 0;
|
double cx = 0;
|
||||||
|
@ -48,8 +58,10 @@ double cy = 0;
|
||||||
int width, height;
|
int width, height;
|
||||||
|
|
||||||
static void cursor_position_callback(GLFWwindow *window, double x, double y) {
|
static void cursor_position_callback(GLFWwindow *window, double x, double y) {
|
||||||
cx = (x-width/2.0) / -1000.0;
|
if (isCursor)
|
||||||
cy = (y-height/2.0) / -1000.0;
|
return;
|
||||||
|
cx = (x - width / 2.0) / -1000.0;
|
||||||
|
cy = (y - height / 2.0) / -1000.0;
|
||||||
cy = (cy > M_PI / 2) ? M_PI / 2 : ((cy < -M_PI / 2) ? -M_PI / 2 : cy);
|
cy = (cy > M_PI / 2) ? M_PI / 2 : ((cy < -M_PI / 2) ? -M_PI / 2 : cy);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -74,12 +86,27 @@ int main(void) {
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
Vertex *cube = malloc(CTRI_ALL);
|
Vertex *cube = malloc(CTRI_ALL);
|
||||||
gen_cubes(cube, 0, 0, 0);
|
Vertex *text = malloc(CTRI_ALL);
|
||||||
|
gen_cubes(cube, 0, 0, 0, 0);
|
||||||
|
gen_cubes(text, 0, 0, 0, 1);
|
||||||
|
|
||||||
GLuint vertex_buffer;
|
GLuint vertex_buffer[2];
|
||||||
glGenBuffers(1, &vertex_buffer);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
GLuint vertex_array;
|
||||||
|
glGenVertexArrays(1, &vertex_array);
|
||||||
|
glBindVertexArray(vertex_array);
|
||||||
|
|
||||||
|
glGenBuffers(2, vertex_buffer);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, text, GL_STATIC_DRAW);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (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, cube, GL_STATIC_DRAW);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
|
||||||
|
|
||||||
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
||||||
|
@ -95,22 +122,12 @@ int main(void) {
|
||||||
glLinkProgram(program);
|
glLinkProgram(program);
|
||||||
|
|
||||||
GLint tex_location = glGetUniformLocation(program, "texSampler");
|
GLint tex_location = glGetUniformLocation(program, "texSampler");
|
||||||
GLint vpos_location = 0;
|
|
||||||
GLint mvp_location = glGetUniformLocation(program, "MVP");
|
GLint mvp_location = glGetUniformLocation(program, "MVP");
|
||||||
|
|
||||||
GLuint vertex_array;
|
unsigned char *pixels;
|
||||||
glGenVertexArrays(1, &vertex_array);
|
|
||||||
glBindVertexArray(vertex_array);
|
|
||||||
|
|
||||||
glEnableVertexAttribArray(vpos_location);
|
int tx, ty, ch;
|
||||||
glVertexAttribPointer(vpos_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
pixels = stbi_load("./test.png", &tx, &ty, &ch, 3);
|
||||||
(void *)0);
|
|
||||||
|
|
||||||
unsigned char pixels[1024 * 1024 * 3];
|
|
||||||
|
|
||||||
for (int i = 0; i < 1024 * 1024 * 3; i++) {
|
|
||||||
pixels[i] = i * 69420;
|
|
||||||
}
|
|
||||||
|
|
||||||
glClearColor(0.2f, 0.5f, 0.7f, 1.0f);
|
glClearColor(0.2f, 0.5f, 0.7f, 1.0f);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
@ -122,9 +139,9 @@ int main(void) {
|
||||||
vec3 pos = {0, 0, 0};
|
vec3 pos = {0, 0, 0};
|
||||||
|
|
||||||
int x = 0, y = 25, z = 0;
|
int x = 0, y = 25, z = 0;
|
||||||
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
||||||
|
|
||||||
glfwGetFramebufferSize(window, &width, &height);
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
const float ratio = width / (float)height;
|
const float ratio = width / (float)height;
|
||||||
|
@ -136,8 +153,8 @@ int main(void) {
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
|
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tx, ty, 0, GL_RGB, GL_UNSIGNED_BYTE,
|
||||||
GL_UNSIGNED_BYTE, pixels);
|
pixels);
|
||||||
|
|
||||||
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_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
@ -168,8 +185,8 @@ int main(void) {
|
||||||
vec3 dir_pos;
|
vec3 dir_pos;
|
||||||
vec3_add(dir_pos, direction, pos);
|
vec3_add(dir_pos, direction, pos);
|
||||||
|
|
||||||
vec3 up = {0,1,0};
|
vec3 up = {0, 1, 0};
|
||||||
//vec3_mul_cross(up, right, direction);
|
// vec3_mul_cross(up, right, direction);
|
||||||
mat4x4_look_at(v, pos, dir_pos, up);
|
mat4x4_look_at(v, pos, dir_pos, up);
|
||||||
|
|
||||||
mat4x4_mul(mvp, p, v);
|
mat4x4_mul(mvp, p, v);
|
||||||
|
|
BIN
test.png
Normal file
BIN
test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 313 B |
Loading…
Add table
Add a link
Reference in a new issue