commit f50e8a885327cfe783ecc0593222c212dca160a3
parent 3c40752cea5fe396bdfb46eb32a8ca1dda78050c
Author: bsandro <email@bsandro.tech>
Date: Mon, 10 Nov 2025 23:28:23 +0200
Loading texture from command line argument
Diffstat:
| M | texture.c | | | 41 | +++++++++++++++++++++++++++++------------ |
1 file changed, 29 insertions(+), 12 deletions(-)
diff --git a/texture.c b/texture.c
@@ -24,6 +24,9 @@ x;
} \
}
+#define ASTC_EXT ".astc"
+#define ASTC_LEN 5
+
/* ASTC header declaration. */
typedef struct
{
@@ -107,9 +110,10 @@ static GLuint mkTexture(const char *filename) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int w, h, numchan;
- stbi_set_flip_vertically_on_load(1);
- uint8_t *data = stbi_load(filename, &w, &h, &numchan, 0);
+ //stbi_set_flip_vertically_on_load(1);
+ uint8_t *data = stbi_load(filename, &w, &h, &numchan, 3);
if (data) {
+ printf("texture %dx%d %dbpp\n", w, h, numchan*8);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
@@ -184,11 +188,11 @@ static GLuint mkTextureAstc(const char *filename) {
GLuint tex;
GL_CHECK(glGenTextures(1, &tex));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, tex));
- //GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
- //GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
+ GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)); // GL_REPEAT
+ GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)); // GL_CLAMP_TO_EDGE
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
- GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
+ GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
+ GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D, 0, astc_format, xsize, ysize, 0, astc_size, (const GLvoid *)astc_data));
//GL_CHECK(glGenerateMipmap(GL_TEXTURE_2D));
GL_CHECK(glEnable(GL_TEXTURE_2D));
@@ -208,6 +212,19 @@ void __attribute((destructor)) end() {
}
int main(int argc, char *argv[]) {
+ const char *tex_fname;
+ bool is_astc = false;
+ if (argc>1) {
+ tex_fname = argv[1];
+ int len = strlen(tex_fname);
+ // ".astc" extension = 5 bytes
+ is_astc = (len>ASTC_LEN&&strncmp(tex_fname+len-ASTC_LEN, ASTC_EXT, ASTC_LEN)==0);
+ } else {
+ tex_fname = "hina.astc";
+ is_astc = true;
+ }
+ printf("texture file: %s (%s)\n", tex_fname, is_astc ? "astc" : "plain");
+
Display *dpy = XOpenDisplay(NULL);
XSetWindowAttributes swa;
Window win;
@@ -238,20 +255,21 @@ int main(int argc, char *argv[]) {
GLXContext ctx = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, ctx);
- GLint extn;
+ // USEFUL DEBUG INFO
+ //@todo validate ASTC support
+ /*GLint extn;
GL_CHECK(glGetIntegerv(GL_NUM_EXTENSIONS, &extn));
printf("%d GL Extensions:\n", extn);
for (GLint i=0; i<extn; ++i) {
printf("\t%s\n", glGetStringi(GL_EXTENSIONS, i));
- }
-
+ }*/
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// @todo geh
- const GLfloat c = 0.099f;
+ const GLfloat c = 0; //0.099f;
// x,y,tx,ty
static const GLfloat vertices[] = {
@@ -284,8 +302,7 @@ int main(int argc, char *argv[]) {
printf("shader link status: %s\n", prgStatus == GL_TRUE ? "ok" : "error");
// texture
- //GLuint tex = mkTexture("hina.png");
- GLuint tex = mkTextureAstc("hina.astc");
+ GLuint tex = is_astc ? mkTextureAstc(tex_fname) : mkTexture(tex_fname);
GLint position = glGetAttribLocation(shaderPrg, "position");
glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(position);