Added texture loading.
This commit is contained in:
@ -22,7 +22,7 @@ file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c)
|
||||
file(GLOB_RECURSE HEADER_FILES ${CMAKE_SOURCE_DIR}/src/*.h)
|
||||
file(COPY ${CMAKE_CURRENT_LIST_DIR}/assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
##################################### LIBS ################ #####################
|
||||
##################################### LIBS #####################################
|
||||
include_directories(${CMAKE_SOURCE_DIR}/lib/stb)
|
||||
|
||||
################################## EXECUTABLE ##################################
|
||||
@ -55,12 +55,4 @@ endif()
|
||||
target_link_libraries(${PROJECT_NAME} cglm)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
target_link_libraries(${PROJECT_NAME} OpenGL::GL)
|
||||
|
||||
# OpenMP
|
||||
# find_package(OpenMP)
|
||||
# if (OPENMP_FOUND)
|
||||
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
|
||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
|
||||
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
|
||||
# endif()
|
||||
target_link_libraries(${PROJECT_NAME} OpenGL::GL)
|
BIN
assets/bruh.png
Normal file
BIN
assets/bruh.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 209 B |
@ -11,12 +11,12 @@
|
||||
#include "display/texture.h"
|
||||
#include "display/shader.h"
|
||||
#include "world/chunklist.h"
|
||||
#include "file/asset.h"
|
||||
|
||||
camera_t *camera;
|
||||
shader_t *shader;
|
||||
chunklist_t *list;
|
||||
// primitive_t *primitive;
|
||||
// texture_t *texture;
|
||||
primitive_t *primitive;
|
||||
texture_t *texture;
|
||||
|
||||
engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
|
||||
// Create the engine instance.
|
||||
@ -51,21 +51,6 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
|
||||
cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 3.0f, 100.0f);
|
||||
shaderUseCamera(shader, camera);
|
||||
|
||||
list = chunkListCreate(3, 3, 3);
|
||||
chunkListShift(list, 1, 0, 0);
|
||||
|
||||
// Test
|
||||
// primitive = quadCreate(
|
||||
// -1, -1, 0, 0,
|
||||
// 1, 1, 1, 1
|
||||
// );
|
||||
|
||||
// int32_t w = 1, h = 1;
|
||||
// pixel_t *p = calloc(w * h, sizeof(pixel_t));
|
||||
// p[0].r=0xFF, p[0].g=0x00, p[0].b=0x00, p[0].a = 0xFF;
|
||||
// texture = textureCreate(w, h, NULL);
|
||||
// textureBufferPixels(texture, 0, 0, w, h, p);
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
@ -73,10 +58,9 @@ uint32_t engineUpdate(engine_t *engine) {
|
||||
shaderUse(shader);
|
||||
renderFrame(engine->render);
|
||||
|
||||
// primitiveDraw(primitive, 0, 6);
|
||||
primitiveDraw(primitive, 0, 6);
|
||||
|
||||
inputUpdate(engine->input);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,11 @@
|
||||
|
||||
#include "asset.h"
|
||||
|
||||
#ifndef STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
#endif
|
||||
|
||||
char * assetStringLoad(char *assetName) {
|
||||
// Open a buffer.
|
||||
FILE *fptr = assetBufferOpen(assetName);
|
||||
@ -88,4 +93,37 @@ shader_t * assetShaderLoad(char *fileVertex, char *fileFragment) {
|
||||
free(fragmentShader);
|
||||
|
||||
return shader;//shader may be NULL if loading failed, but not our problem.
|
||||
}
|
||||
|
||||
texture_t * assetTextureLoad(char *fileName) {
|
||||
FILE *buffer;
|
||||
texture_t *texture;
|
||||
int channels, width, height;
|
||||
pixel_t *data;
|
||||
stbi_io_callbacks OPENGL_STBI_CALLBACKS;
|
||||
|
||||
buffer = assetBufferOpen(fileName);
|
||||
if(buffer == NULL) return NULL;
|
||||
|
||||
// Setup the interface for STBI
|
||||
OPENGL_STBI_CALLBACKS.read = &assetBufferRead;
|
||||
OPENGL_STBI_CALLBACKS.skip = &assetBufferSkip;
|
||||
OPENGL_STBI_CALLBACKS.eof = &assetBufferEnd;
|
||||
|
||||
// Buffer the image
|
||||
channels = 0;
|
||||
data = (pixel_t *)stbi_load_from_callbacks(
|
||||
&OPENGL_STBI_CALLBACKS, buffer,
|
||||
&width, &height,
|
||||
&channels, STBI_rgb_alpha
|
||||
);
|
||||
|
||||
// Close the buffer
|
||||
assetBufferClose(buffer);
|
||||
if(data == NULL) return NULL;
|
||||
|
||||
// Turn into a texture.
|
||||
texture = textureCreate(width, height, data);
|
||||
stbi_image_free(data);
|
||||
return texture;
|
||||
}
|
@ -75,4 +75,9 @@ void assetBufferSkip(FILE *buffer, int32_t n);
|
||||
* @param fileFragment The file path of the fragment shader
|
||||
* @return The loaded shader_t instance (From shaderCompile)
|
||||
*/
|
||||
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment);
|
||||
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment);
|
||||
|
||||
/**
|
||||
* Load a texture from a PNG file.
|
||||
*/
|
||||
texture_t * assetTextureLoad(char *fileName);
|
@ -96,18 +96,24 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
|
||||
// Load the chunk if we need to. We also use this to calculate new absolutes
|
||||
if(
|
||||
(ax = lx + nx) != chunk->x ||
|
||||
(ay = ly + ny) != chunk->y ||
|
||||
(az = lz + nz) != chunk->z
|
||||
(ly + ny) != chunk->y ||
|
||||
(lz + nz) != chunk->z
|
||||
) {
|
||||
// Calculate those things that may have not been done within the if
|
||||
ay = ly + ny;
|
||||
az = lz + nz;
|
||||
|
||||
// Load new chunk.
|
||||
chunkUnload(chunk);
|
||||
chunkLoad(chunk, ax, ay, az);
|
||||
|
||||
// Update the absolute coordinates.
|
||||
chunk->x = ax;
|
||||
chunk->y = ay;
|
||||
chunk->z = az;
|
||||
}
|
||||
|
||||
//Now, based off those new local positions, calculate the new index.
|
||||
// Now, based off those new local positions, calculate the new index.
|
||||
ni = (
|
||||
nx +
|
||||
(ny * list->width) +
|
||||
@ -116,6 +122,11 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
|
||||
chunkList[ni] = chunk;
|
||||
}
|
||||
|
||||
// Update Absolutes.
|
||||
list->x = lx;
|
||||
list->y = ly;
|
||||
list->z = lz;
|
||||
|
||||
// Now copy that array over.
|
||||
memcpy(list->chunkList, chunkList, sizeof(chunk_t *) * list->count);
|
||||
free(chunkList);
|
||||
|
@ -9,8 +9,6 @@
|
||||
#include "chunk.h"
|
||||
#include "./../util/math.h"
|
||||
|
||||
#define CHUNK_INDEX_NULL = 0
|
||||
|
||||
typedef struct {
|
||||
/** Dimensions of the chunk list */
|
||||
int32_t width, height, depth;
|
||||
|
Reference in New Issue
Block a user