added textures to 2D objects

This commit is contained in:
Alexandre 2025-05-11 23:00:58 +02:00
parent 34d70154e0
commit 99566d2cee
6 changed files with 27 additions and 17 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -410,10 +410,13 @@ const char *fragmentShaderSource = "#version 330 core\n"
const char *vertexShaderSourceR = "#version 330 core\n" const char *vertexShaderSourceR = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" "layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTex;\n"
"uniform mat4 scale;\n" "uniform mat4 scale;\n"
"uniform mat4 slide;\n" "uniform mat4 slide;\n"
"out vec2 texCoord;\n"
"void main() {\n" "void main() {\n"
" gl_Position = slide * scale * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" " gl_Position = slide * scale * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
" texCoord = aTex;\n"
"}\0"; "}\0";
const char *fragmentShaderSourceR = "#version 330 core\n" const char *fragmentShaderSourceR = "#version 330 core\n"
@ -422,7 +425,7 @@ const char *fragmentShaderSourceR = "#version 330 core\n"
"in vec2 texCoord;\n" "in vec2 texCoord;\n"
"uniform sampler2D tex0;\n" "uniform sampler2D tex0;\n"
"void main() {\n" "void main() {\n"
" FragColor = u_color2;\n" " FragColor = texture(tex0, texCoord) * u_color2;\n"
"}\0"; "}\0";
typedef enum file_extension {PNG, JPG, JPEG} file_extension; typedef enum file_extension {PNG, JPG, JPEG} file_extension;
@ -459,7 +462,7 @@ void generate_itemTexture_2D(int id, char* filename, file_extension ext) {
unsigned char* bytes = stbi_load(filename, &widthImg, &heightImg, &numColCh, 0); unsigned char* bytes = stbi_load(filename, &widthImg, &heightImg, &numColCh, 0);
if(bytes == 0) {fprintf(stderr, "ERROR : cannot load texture [%s]\n", filename); exit(1);} if(bytes == 0) {fprintf(stderr, "ERROR : cannot load texture [%s]\n", filename); exit(1);}
glGenTextures(1, &itemTexts[id]); glGenTextures(1, &itemTexts[id]);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, itemTexts[id]); glBindTexture(GL_TEXTURE_2D, itemTexts[id]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -620,6 +623,7 @@ int main_alt() {
// 32 max // 32 max
generate_itemTexture_2D(0, "res/white.png", PNG); generate_itemTexture_2D(0, "res/white.png", PNG);
generate_itemTexture_2D(1, "res/sq_full.png", PNG);
printf("-----------------------------------------------------------------------------------------------\n"); fflush(stdout); printf("-----------------------------------------------------------------------------------------------\n"); fflush(stdout);
@ -664,21 +668,26 @@ int main_alt() {
glDeleteShader(vertexShaderR); glDeleteShader(vertexShaderR);
glDeleteShader(fragmentShaderR); glDeleteShader(fragmentShaderR);
// rectangle VAO/VBO
initMenus();
unsigned int RVBO, RVAO; unsigned int RVBO, RVAO;
glGenVertexArrays(1, &RVAO); glGenVertexArrays(1, &RVAO);
glGenBuffers(1, &RVBO); glGenBuffers(1, &RVBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(RVAO); glBindVertexArray(RVAO);
// rectangle VAO/VBO
initMenus();
glBindBuffer(GL_ARRAY_BUFFER, RVBO); glBindBuffer(GL_ARRAY_BUFFER, RVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(rectDefault), rectDefault, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(rectDefault), rectDefault, 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); glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -737,9 +746,8 @@ int main_alt() {
generate_nearby_chunks(render_distance); generate_nearby_chunks(render_distance);
fflush(stdout); fflush(stdout);
glUseProgram(shaderProgram);
// draw the map // draw the map
glUseProgram(shaderProgram);
glBindVertexArray(VAO); glBindVertexArray(VAO);
//printf("1\n"); fflush(stdout); //printf("1\n"); fflush(stdout);
@ -754,6 +762,7 @@ int main_alt() {
glUseProgram(shaderProgramR); glUseProgram(shaderProgramR);
glBindVertexArray(RVAO); glBindVertexArray(RVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, itemTexts[0]); glBindTexture(GL_TEXTURE_2D, itemTexts[0]);
gl_drawInteger(shaderProgramR, (int)(1.0f/(real_T)), 0.0f, -0.92f, 0.05, 32, 255, 32, 0.005, -1); gl_drawInteger(shaderProgramR, (int)(1.0f/(real_T)), 0.0f, -0.92f, 0.05, 32, 255, 32, 0.005, -1);
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1); gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);

View File

@ -19,7 +19,7 @@
#include "maeth.h" #include "maeth.h"
#include "menus.h" #include "menus.h"
float rectDefault[18] ; float rectDefault[30] ;
float incr; float incr;
int creativeIntID; int creativeIntID;
@ -44,13 +44,13 @@ char* modString;
static mat4 scale, slide; static mat4 scale, slide;
void initMenus() { void initMenus() {
rectDefault[0] = -0.5f; rectDefault[1] = -0.5f; rectDefault[2] = -0.99f; rectDefault[0] = -0.5f; rectDefault[1] = -0.5f; rectDefault[2] = -0.99f; rectDefault[3] = 0.0f; rectDefault[4] = 1.0f;
rectDefault[3] = -0.5f; rectDefault[4] = 0.5f; rectDefault[5] = -0.99f; rectDefault[5] = -0.5f; rectDefault[6] = 0.5f; rectDefault[7] = -0.99f; rectDefault[8] = 0.0f; rectDefault[9] = 0.0f;
rectDefault[6] = 0.5f; rectDefault[7] = 0.5f; rectDefault[8] = -0.99f; rectDefault[10] = 0.5f; rectDefault[11] = 0.5f; rectDefault[12] = -0.99f; rectDefault[13] = 1.0f; rectDefault[14] = 0.0f;
rectDefault[9] = -0.5f; rectDefault[10] = -0.5f; rectDefault[11] = -0.99f; rectDefault[15] = -0.5f; rectDefault[16] = -0.5f; rectDefault[17] = -0.99f; rectDefault[18] = 0.0f; rectDefault[19] = 1.0f;
rectDefault[12] = 0.5f; rectDefault[13] = 0.5f; rectDefault[14] = -0.99f; rectDefault[20] = 0.5f; rectDefault[21] = 0.5f; rectDefault[22] = -0.99f; rectDefault[23] = 1.0f; rectDefault[24] = 0.0f;
rectDefault[15] = 0.5f; rectDefault[16] = -0.5f; rectDefault[17] = -0.99f; rectDefault[25] = 0.5f; rectDefault[26] = -0.5f; rectDefault[27] = -0.99f; rectDefault[28] = 1.0f; rectDefault[29] = 1.0f;
incr = 0.0f; incr = 0.0f;
@ -512,9 +512,10 @@ void display_button(int but_id, unsigned int fragShader) {
} else { } else {
float size = minf(buttonList[but_id].h/2.3f, (buttonList[but_id].w)/(get_string_len(buttonList[but_id].text))); float size = minf(buttonList[but_id].h/2.3f, (buttonList[but_id].w)/(get_string_len(buttonList[but_id].text)));
int retcol = mn_get_color(buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue); int retcol = mn_get_color(buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue);
glBindTexture(GL_TEXTURE_2D, itemTexts[1]);
gl_drawRect(fragShader, buttonList[but_id].x, buttonList[but_id].y, buttonList[but_id].w, buttonList[but_id].h, buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue); gl_drawRect(fragShader, buttonList[but_id].x, buttonList[but_id].y, buttonList[but_id].w, buttonList[but_id].h, buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue);
glBindTexture(GL_TEXTURE_2D, itemTexts[0]);
gl_drawString(fragShader, buttonList[but_id].text, buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, 0.7f*size, retcol, retcol, retcol, 0.7f*size/10.0f, 0); gl_drawString(fragShader, buttonList[but_id].text, buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, 0.7f*size, retcol, retcol, retcol, 0.7f*size/10.0f, 0);
} }
} }

View File

@ -160,7 +160,7 @@ extern int render_distance;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
extern float vertices[180]; extern float vertices[180];
extern float rectDefault[18]; extern float rectDefault[30];
extern int triCount; extern int triCount;