working (?) textures

This commit is contained in:
Alexandre 2025-02-20 12:25:43 +01:00
parent 0bb2d0da9f
commit 90e0fd244d
28 changed files with 8099 additions and 56 deletions

View File

@ -17,6 +17,8 @@
"entities.h": "c",
"menus.h": "c",
"structure.h": "c",
"limits.h": "c"
"limits.h": "c",
"png.h": "c",
"stdlib.h": "c"
}
}

View File

@ -1,6 +1,6 @@
CC = gcc
FLAGS = -Wall -Wextra -g
LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm
LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm -lpng -lSOIL
all: bin/back

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
res/container.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 KiB

BIN
res/question_block.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -7,6 +7,7 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"

View File

@ -7,6 +7,7 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"

View File

@ -10,6 +10,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"
@ -33,6 +34,7 @@ GLint loc_view;
GLint loc_proj;
GLint loc_frag;
GLint loc_tex;
void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ;
@ -79,51 +81,51 @@ double right = 1.0 ;
mat4 model, view, projection;
mat4 scale;
float vertices[108];
float vertices[180];
// -x ; +x ; -y ; +y ; -z ; +z //
void init_vertices() {
vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = -0.5f;
vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = -0.5f;
vertices[6] = -0.5f; vertices[7] = 0.5f; vertices[8] = 0.5f;
vertices[9] = -0.5f; vertices[10] = -0.5f; vertices[11] = -0.5f;
vertices[12] = -0.5f; vertices[13] = 0.5f; vertices[14] = 0.5f;
vertices[15] = -0.5f; vertices[16] = -0.5f; vertices[17] = 0.5f;
vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = -0.5f; vertices[3] = 0.0; vertices[4] = 0.0;
vertices[5] = -0.5f; vertices[6] = 0.5f; vertices[7] = -0.5f; vertices[8] = 0.0; vertices[9] = 1.0;
vertices[10] = -0.5f; vertices[11] = 0.5f; vertices[12] = 0.5f; vertices[13] = 1.0; vertices[14] = 1.0;
vertices[15] = -0.5f; vertices[16] = -0.5f; vertices[17] = -0.5f; vertices[18] = 0.0; vertices[19] = 0.0;
vertices[20] = -0.5f; vertices[21] = 0.5f; vertices[22] = 0.5f; vertices[23] = 1.0; vertices[24] = 0.0;
vertices[25] = -0.5f; vertices[26] = -0.5f; vertices[27] = 0.5f; vertices[28] = 1.0; vertices[29] = 1.0;
vertices[18] = 0.5f; vertices[19] = -0.5f; vertices[20] = -0.5f;
vertices[21] = 0.5f; vertices[22] = 0.5f; vertices[23] = 0.5f;
vertices[24] = 0.5f; vertices[25] = 0.5f; vertices[26] = -0.5f;
vertices[27] = 0.5f; vertices[28] = -0.5f; vertices[29] = -0.5f;
vertices[30] = 0.5f; vertices[31] = -0.5f; vertices[32] = 0.5f;
vertices[33] = 0.5f; vertices[34] = 0.5f; vertices[35] = 0.5f;
vertices[30] = 0.5f; vertices[31] = -0.5f; vertices[32] = -0.5f; vertices[33] = 0.0; vertices[34] = 0.0;
vertices[35] = 0.5f; vertices[36] = 0.5f; vertices[37] = 0.5f; vertices[38] = 0.0; vertices[39] = 1.0;
vertices[40] = 0.5f; vertices[41] = 0.5f; vertices[42] = -0.5f; vertices[43] = 1.0; vertices[44] = 1.0;
vertices[45] = 0.5f; vertices[46] = -0.5f; vertices[47] = -0.5f; vertices[48] = 0.0; vertices[49] = 0.0;
vertices[50] = 0.5f; vertices[51] = -0.5f; vertices[52] = 0.5f; vertices[53] = 1.0; vertices[54] = 0.0;
vertices[55] = 0.5f; vertices[56] = 0.5f; vertices[57] = 0.5f; vertices[58] = 1.0; vertices[59] = 1.0;
vertices[36] = -0.5f; vertices[37] = -0.5f; vertices[38] = -0.5f;
vertices[39] = 0.5f; vertices[40] = -0.5f; vertices[41] = 0.5f;
vertices[42] = 0.5f; vertices[43] = -0.5f; vertices[44] = -0.5f;
vertices[45] = -0.5f; vertices[46] = -0.5f; vertices[47] = -0.5f;
vertices[48] = -0.5f; vertices[49] = -0.5f; vertices[50] = 0.5f;
vertices[51] = 0.5f; vertices[52] = -0.5f; vertices[53] = 0.5f;
vertices[60] = -0.5f; vertices[61] = -0.5f; vertices[62] = -0.5f; vertices[63] = 0.0; vertices[64] = 0.0;
vertices[65] = 0.5f; vertices[66] = -0.5f; vertices[67] = 0.5f; vertices[68] = 0.0; vertices[69] = 1.0;
vertices[70] = 0.5f; vertices[71] = -0.5f; vertices[72] = -0.5f; vertices[73] = 1.0; vertices[74] = 1.0;
vertices[75] = -0.5f; vertices[76] = -0.5f; vertices[77] = -0.5f; vertices[78] = 0.0; vertices[79] = 0.0;
vertices[80] = -0.5f; vertices[81] = -0.5f; vertices[82] = 0.5f; vertices[83] = 1.0; vertices[84] = 0.0;
vertices[85] = 0.5f; vertices[86] = -0.5f; vertices[87] = 0.5f; vertices[88] = 1.0; vertices[89] = 1.0;
vertices[54] = -0.5f; vertices[55] = 0.5f; vertices[56] = -0.5f;
vertices[57] = 0.5f; vertices[58] = 0.5f; vertices[59] = -0.5f;
vertices[60] = 0.5f; vertices[61] = 0.5f; vertices[62] = 0.5f;
vertices[63] = -0.5f; vertices[64] = 0.5f; vertices[65] = -0.5f;
vertices[66] = 0.5f; vertices[67] = 0.5f; vertices[68] = 0.5f;
vertices[69] = -0.5f; vertices[70] = 0.5f; vertices[71] = 0.5f;
vertices[90] = -0.5f; vertices[91] = 0.5f; vertices[92] = -0.5f; vertices[93] = 0.0; vertices[94] = 0.0;
vertices[95] = 0.5f; vertices[96] = 0.5f; vertices[97] = -0.5f; vertices[98] = 0.0; vertices[99] = 1.0;
vertices[100] = 0.5f; vertices[101] = 0.5f; vertices[102] = 0.5f; vertices[103] = 1.0; vertices[104] = 1.0;
vertices[105] = -0.5f; vertices[106] = 0.5f; vertices[107] = -0.5f; vertices[108] = 0.0; vertices[109] = 0.0;
vertices[110] = 0.5f; vertices[111] = 0.5f; vertices[112] = 0.5f; vertices[113] = 1.0; vertices[114] = 0.0;
vertices[115] = -0.5f; vertices[116] = 0.5f; vertices[117] = 0.5f; vertices[118] = 1.0; vertices[119] = 1.0;
vertices[72] = -0.5f; vertices[73] = -0.5f; vertices[74] = -0.5f;
vertices[75] = 0.5f; vertices[76] = -0.5f; vertices[77] = -0.5f;
vertices[78] = 0.5f; vertices[79] = 0.5f; vertices[80] = -0.5f;
vertices[81] = -0.5f; vertices[82] = -0.5f; vertices[83] = -0.5f;
vertices[84] = 0.5f; vertices[85] = 0.5f; vertices[86] = -0.5f;
vertices[87] = -0.5f; vertices[88] = 0.5f; vertices[89] = -0.5f;
vertices[120] = -0.5f; vertices[121] = -0.5f; vertices[122] = -0.5f; vertices[123] = 0.0; vertices[124] = 0.0;
vertices[125] = 0.5f; vertices[126] = -0.5f; vertices[127] = -0.5f; vertices[128] = 0.0; vertices[129] = 1.0;
vertices[130] = 0.5f; vertices[131] = 0.5f; vertices[132] = -0.5f; vertices[133] = 1.0; vertices[134] = 1.0;
vertices[135] = -0.5f; vertices[136] = -0.5f; vertices[137] = -0.5f; vertices[138] = 0.0; vertices[139] = 0.0;
vertices[140] = 0.5f; vertices[141] = 0.5f; vertices[142] = -0.5f; vertices[143] = 1.0; vertices[144] = 0.0;
vertices[145] = -0.5f; vertices[146] = 0.5f; vertices[147] = -0.5f; vertices[148] = 1.0; vertices[149] = 1.0;
vertices[90] = -0.5f; vertices[91] = -0.5f; vertices[92] = 0.5f;
vertices[93] = 0.5f; vertices[94] = 0.5f; vertices[95] = 0.5f;
vertices[96] = 0.5f; vertices[97] = -0.5f; vertices[98] = 0.5f;
vertices[99] = -0.5f; vertices[100] = -0.5f; vertices[101] = 0.5f;
vertices[102] = -0.5f; vertices[103] = 0.5f; vertices[104] = 0.5f;
vertices[105] = 0.5f; vertices[106] = 0.5f; vertices[107] = 0.5f;
vertices[150] = -0.5f; vertices[151] = -0.5f; vertices[152] = 0.5f; vertices[153] = 0.0; vertices[154] = 0.0;
vertices[155] = 0.5f; vertices[156] = 0.5f; vertices[157] = 0.5f; vertices[158] = 0.0; vertices[159] = 1.0;
vertices[160] = 0.5f; vertices[161] = -0.5f; vertices[162] = 0.5f; vertices[163] = 1.0; vertices[164] = 1.0;
vertices[165] = -0.5f; vertices[166] = -0.5f; vertices[167] = 0.5f; vertices[168] = 0.0; vertices[169] = 0.0;
vertices[170] = -0.5f; vertices[171] = 0.5f; vertices[172] = 0.5f; vertices[173] = 1.0; vertices[174] = 0.0;
vertices[175] = 0.5f; vertices[176] = 0.5f; vertices[177] = 0.5f; vertices[178] = 1.0; vertices[179] = 1.0;
}
void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, cube_0* c, double offx, double offy, double offz) {
@ -144,6 +146,7 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glUniform4f(loc_frag, c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
triCount += 12;
}
@ -225,14 +228,4 @@ void gl_drawData(unsigned int shaderProg) {
gl_printf(shaderProg, -0.95f, 0.9f, 0.04f, 0.004f, 255-player_hp/4, player_hp/4, 0, "HP %d", player_hp);
gl_printf(shaderProg, -0.95f, 0.8f, 0.04f, 0.004f, 255, 255, 0, "coins %d", coins);
//gl_drawInteger(shaderProg, player_hp, -0.95f, 0.9f, 0.05f, 255-player_hp/4, player_hp/4, 0, 0.005f, 1);
}
void read_ppm(char* filename) {
FILE* ptr = fopen(filename, "r");
char c = fgetc(ptr);
while(c != EOF) {
printf("%c\n", c);
}
printf("\n");
fclose(ptr);
}

View File

@ -10,6 +10,4 @@ void init_vertices();
void gl_drawData(unsigned int shaderProg);
void read_ppm(char* filename);
#endif

View File

@ -7,6 +7,7 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"

View File

@ -8,6 +8,7 @@
#include <limits.h>
#include <time.h>
#include <string.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"

View File

@ -7,6 +7,7 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <libpng/png.h>
#include "structure.h"
#include "hash.h"

View File

@ -9,6 +9,10 @@
#include <time.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <libpng/png.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "hash.h"
#include "structure.h"
@ -254,20 +258,26 @@ void processInput(GLFWwindow *window, float dtime) {
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTex;\n"
"uniform mat4 scale;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"out vec2 texCoord;\n"
"void main() {\n"
" gl_Position = projection * view * model * scale * vec4(aPos, 1.0);\n"
" texCoord = aTex;\n"
"}\0";
// Fragment Shader Source
const char *fragmentShaderSource = "#version 330 core\n"
"uniform vec4 u_color;\n"
"out vec4 FragColor;\n"
"in vec2 texCoord;\n"
"uniform sampler2D tex0;\n"
"void main() {\n"
" FragColor = u_color;\n"
// " FragColor = u_color;\n"
" FragColor = texture(tex0, texCoord);\n"
"}\0";
const char *vertexShaderSourceR = "#version 330 core\n"
@ -380,8 +390,37 @@ int main_alt() {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
printf("-------------------------------------------------------\n"); fflush(stdout);
int widthImg, heightImg, numColCh;
unsigned char* bytes = stbi_load("res/container.jpg", &widthImg, &heightImg, &numColCh, 0);
if(bytes == 0) {fprintf(stderr, "ERROR : cannot load texture\n"); exit(1);}
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widthImg, heightImg, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0);
printf("------------------------------------------------------- %d %d\n", widthImg, heightImg); fflush(stdout);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
@ -460,6 +499,10 @@ int main_alt() {
clock_t finish = clock();
clock_t origin = clock();
loc_tex = glGetUniformLocation(shaderProgram, "tex0");
glUseProgram(shaderProgram);
glUniform1i(loc_tex, 0);
while(!glfwWindowShouldClose(window) && player_hp > 0) {
// input
// -----
@ -471,8 +514,10 @@ int main_alt() {
generate_nearby_chunks(1);
fflush(stdout);
// draw the map
glUseProgram(shaderProgram);
// draw the map
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
@ -528,6 +573,7 @@ int main_alt() {
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteTextures(1, &texture);
glDeleteProgram(shaderProgram);
// glfw: terminate, clearing all previously allocated GLFW resources.
@ -544,6 +590,6 @@ int main(int argc, char** argv) {
newRoomCount = 7;
switchRoom = false;
newRoomName = "templates/";
//read_ppm("res/smb-2.ppm");
//read_png_file("res/question_block.png");
return main_alt();
}

View File

@ -12,6 +12,7 @@
#include "../include/glad/glad.h"
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"

View File

@ -7,6 +7,7 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <libpng/png.h>
#include "hash.h"
#include "structure.h"

View File

@ -7,6 +7,7 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <libpng/png.h>
#include "structure.h"
#include "hash.h"

7988
src/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -148,7 +148,7 @@ extern double creative_speed;
extern double min_dist;
// ---------------------------------------------------------------------------------------------------- //
extern float vertices[108];
extern float vertices[180];
extern float rectDefault[18];
extern int triCount;
@ -170,4 +170,12 @@ extern char* newRoomName;
extern int newRoomCount;
extern bool switchRoom;
extern GLint loc_scale;
extern GLint loc_model;
extern GLint loc_view;
extern GLint loc_proj;
extern GLint loc_frag;
extern GLint loc_tex;
#endif