commit c58e453b306bf1c372e59f8f481fc6e1705436bc
parent e0817a497de67eac9cf6389e02e3cc2a0f22c8d4
Author: bsandro <email@bsandro.tech>
Date: Sun, 23 Nov 2025 22:37:15 +0200
sphere with Mars texture
Diffstat:
3 files changed, 26 insertions(+), 15 deletions(-)
diff --git a/mars.astc b/mars.astc
Binary files differ.
diff --git a/moon.astc b/moon.astc
Binary files differ.
diff --git a/sphere.c b/sphere.c
@@ -10,8 +10,9 @@
#define GL_GLEXT_PROTOTYPES 1
#include <GL/gl.h>
#include <GL/glx.h>
-#include <GL/glu.h>
+#include <GL/glu.h> // tesselator
#include "../cglm/include/cglm/cglm.h"
+#include "astc.h"
#define RANDOM(x) (rand() % (x))
#define LEN(x) (sizeof(x)/sizeof(x[0]))
@@ -27,8 +28,8 @@ x;
}
// I can push that to the local variables but want to keep the array on stack for now.
-#define STACKS 24
-#define SECTORS 24
+#define STACKS 64
+#define SECTORS 64
static const double current_ts() {
struct timespec ts;
@@ -43,20 +44,25 @@ static const GLchar *vertexSrc = R"(
#version 420
layout(location=0) in vec3 position;
layout(location=1) in vec2 aTexCoord;
+out vec2 texCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(position, 1.0);
+ texCoord = aTexCoord;
}
)";
static const GLchar *fragmentSrc = R"(
#version 420
uniform double iTime;
+uniform sampler2D ourTex;
out vec4 outColor;
+in vec2 texCoord;
void main() {
- outColor = vec4(0.1f, 0.5f, 0.2f, 0.0f);
+ //outColor = vec4(0.1f, 0.5f, 0.2f, 0.0f);
+ outColor = texture(ourTex, texCoord);
}
)";
@@ -116,7 +122,6 @@ void generateSphere(float radius, int stacks, int sectors, GLfloat outVertices[]
float stackStep = M_PI/stacks;
float sectorStep = (2.f*M_PI)/sectors;
int offset = 0;
- // deliberate <=
for (int i=0;i<=stacks;++i) {
float stackAngle = M_PI_2 - (float)i*stackStep;
float xy = radius*cosf(stackAngle);
@@ -125,17 +130,19 @@ void generateSphere(float radius, int stacks, int sectors, GLfloat outVertices[]
float sectorAngle = (float)j*sectorStep;
float x = xy*cosf(sectorAngle);
float y = xy*sinf(sectorAngle);
- assert(offset < (stacks+1)*(sectors+1)*3 - 2);
+ float u = (float)j / sectors;
+ float v = (float)i / stacks;
outVertices[offset++] = x;
outVertices[offset++] = y;
outVertices[offset++] = z;
+ outVertices[offset++] = u;
+ outVertices[offset++] = v;
}
}
- printf("offset: %d\n", offset);
}
void generateElements(int stacks, int sectors, GLuint outElements[]) {
- GLuint offset = 0;
+ uint32_t offset = 0;
for (int i=0;i<stacks;++i) {
int k1 = i*(sectors+1);
int k2 = k1+sectors+1;
@@ -152,8 +159,7 @@ void generateElements(int stacks, int sectors, GLuint outElements[]) {
}
}
}
- printf("elements: %lu, total: %lu\n", offset, (stacks-1)*sectors*6);
- assert(offset <= (stacks-1)*sectors*6);
+ printf("elements offset: %lu\n", offset);
}
int main(int argc, char *argv[]) {
@@ -192,9 +198,9 @@ int main(int argc, char *argv[]) {
glBindVertexArray(vao);
const float radius = .9f;
- // x,y,z
- GLfloat vertices[(STACKS+1)*(SECTORS+1)*3];
- GLuint elements[(STACKS-1)*SECTORS*6];
+ // x,y,z,u,v
+ GLfloat vertices[(STACKS+1)*(SECTORS+1)*5];
+ GLuint elements[(STACKS-1)*(SECTORS)*6];
generateSphere(radius, STACKS, SECTORS, vertices);
generateElements(STACKS, SECTORS, elements);
@@ -222,9 +228,14 @@ int main(int argc, char *argv[]) {
glGetProgramiv(shaderPrg, GL_LINK_STATUS, &prgStatus);
printf("shader link status: %s\n", prgStatus == GL_TRUE ? "ok" : "error");
+ GLuint tex = mkTextureAstc("mars.astc");
+
GLint position = glGetAttribLocation(shaderPrg, "position");
- glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (void *)0);
+ glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(position);
+ GLint aTexCoord = glGetAttribLocation(shaderPrg, "aTexCoord");
+ glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void *)(3*sizeof(GLfloat)));
+ glEnableVertexAttribArray(aTexCoord);
mat4 model;
glm_mat4_identity(model);
@@ -288,7 +299,7 @@ int main(int argc, char *argv[]) {
// ultra useful debug
glLineWidth(1.0f);
glPointSize(4.0f);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
for (int i=0; i<LEN(positions); ++i) {
mat4 model;