redo look-at, test cube with lines

This commit is contained in:
Luca Conte 2025-04-20 00:52:53 +02:00
parent 548b167968
commit 5fc371cded
3 changed files with 72 additions and 34 deletions

View File

@ -136,14 +136,14 @@ void init(void) {
GLfloat vertices[] = { GLfloat vertices[] = {
// X // Y // X // Y
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, -1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, -1.0f, -1.0f,
0.5f, -0.5f, -0.5f, -1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, -1.0f, 1.0f, -1.0f,
0.5f, 0.5f, -0.5f, -1.0f, -1.0f, 1.0f,
0.5f, 0.5f, 0.5f, -1.0f, -1.0f, -1.0f,
}; };
GLuint restart = 128; GLuint restart = 128;
@ -151,8 +151,24 @@ void init(void) {
glPrimitiveRestartIndex(restart); glPrimitiveRestartIndex(restart);
GLuint indices[] = { GLuint indices[] = {
0, 1, 2, 0, 1,
0, 2, 5 0, 2,
0, 4,
1, 3,
1, 5,
2, 3,
2, 6,
3, 7,
4, 5,
4, 6,
5, 7,
6, 7
}; };
DEBUG("Creating vertext buffer"); DEBUG("Creating vertext buffer");
@ -204,23 +220,26 @@ GLfloat currentHue = 0.0f;
void draw(void) { void draw(void) {
updateStatusDisplay(); updateStatusDisplay();
currentHue += 0.005f; currentHue += 0.0005f;
if (currentHue > 1.0) currentHue = 0.0f; if (currentHue > 1.0) currentHue = 0.0f;
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program); glUseProgram(program);
mat4 transformMatrix; mat4 transformMatrix;
vec3 position = {0.5f, 0.5f, 0.0f}; vec3 scale = {0.2f, 0.2f, 0.2f};
mat4Identity(transformMatrix); mat4Identity(transformMatrix);
// mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2); mat4Scale(transformMatrix, transformMatrix, scale);
// mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2);
mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2);
mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2);
mat4 viewMatrix; mat4 viewMatrix;
mat4Identity(viewMatrix);
vec3 cameraPos = {0.0f, sin(currentHue * PI * 2), 0.0f}; vec3 cameraPos = {0.0f, sin(currentHue * PI * 2), 0.0f};
vec3 cameraLookAt = {0.0f, 0.0f, -1.0f}; vec3 cameraLookAt = {0.0f, 0.0f, -1.0f};
vec3 cameraUp = {0.0f, 1.0f, 0.0f}; vec3 cameraUp = {0.0f, 1.0f, 0.0f};
mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp); // mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp);
GLuint transformLocation = glGetUniformLocation(program, "uTransform"); GLuint transformLocation = glGetUniformLocation(program, "uTransform");
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix); glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
@ -230,7 +249,7 @@ void draw(void) {
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLES, 20, GL_UNSIGNED_INT, 0); glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {

View File

@ -76,6 +76,18 @@ void mat4Translate(mat4 out, mat4 in, vec3 v) {
mat4Multiply(out, T, in); mat4Multiply(out, T, in);
} }
void mat4Scale(mat4 out, mat4 in, vec3 v) {
mat4 T;
mat4Identity(T);
T[0] = v[0];
T[5] = v[1];
T[10] = v[2];
mat4Multiply(out, T, in);
}
void mat4RotateX(mat4 out, mat4 in, GLfloat a) { void mat4RotateX(mat4 out, mat4 in, GLfloat a) {
mat4 T; mat4 T;
mat4Identity(T); mat4Identity(T);
@ -130,42 +142,42 @@ void vec3CrossProduct(vec3 out, vec3 a, vec3 b) {
} }
void vec3Normalise(vec3 out, vec3 in) { void vec3Normalise(vec3 out, vec3 in) {
GLfloat length = sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]); GLfloat length = vec3Length(in);
out[0] = in[0] / length; out[0] = in[0] / length;
out[1] = in[1] / length; out[1] = in[1] / length;
out[2] = in[2] / length; out[2] = in[2] / length;
} }
GLfloat vec3Length(vec3 in) {
return sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]);
}
GLfloat vec3DotProduct(vec3 a, vec3 b) { GLfloat vec3DotProduct(vec3 a, vec3 b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
} }
void mat4BuildLookAt(mat4 out, vec3 eye, vec3 center, vec3 up) { void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) {
vec3 n; vec3 n;
vec3 u; vec3 u;
vec3 v; vec3 v;
vec3 t;
vec3Subtract(n, eye, center); vec3Subtract(n, eye, look);
vec3CrossProduct(u, up, n); vec3CrossProduct(u, up, n);
vec3CrossProduct(v, n, u); vec3CrossProduct(v, n, u);
mat4Identity(out); vec3Normalise(n, n);
vec3Normalise(u, u);
vec3Normalise(v, v);
out[0] = u[0]; t[0] = - vec3DotProduct(u, eye);
out[1] = v[0]; t[1] = - vec3DotProduct(v, eye);
out[2] = n[0]; t[2] = - vec3DotProduct(n, eye);
out[4] = u[1]; out[0] = u[0]; out[4] = u[1]; out[8] = u[2]; out[12] = t[0];
out[5] = v[1]; out[1] = v[0]; out[5] = v[1]; out[9] = v[2]; out[13] = t[1];
out[6] = n[1]; out[2] = n[0]; out[6] = n[1]; out[10] = n[2]; out[14] = t[2];
out[3] = 0; out[7] = 0; out[11] = 0; out[15] = 1;
out[8] = u[2];
out[9] = v[2];
out[10] = n[2];
out[12] = - vec3DotProduct(u, eye);
out[13] = - vec3DotProduct(v, eye);
out[14] = - vec3DotProduct(n, eye);
} }

View File

@ -18,10 +18,17 @@ extern void mat4Empty(mat4 mat);
extern void mat4Multiply(mat4 result, mat4 A, mat4 B); extern void mat4Multiply(mat4 result, mat4 A, mat4 B);
extern void mat4Print(mat4 m); extern void mat4Print(mat4 m);
extern void mat4Translate(mat4 out, mat4 in, vec3 v); extern void mat4Translate(mat4 out, mat4 in, vec3 v);
extern void mat4Scale(mat4 out, mat4 in, vec3 v);
extern void mat4RotateX(mat4 out, mat4 in, GLfloat a); extern void mat4RotateX(mat4 out, mat4 in, GLfloat a);
extern void mat4RotateY(mat4 out, mat4 in, GLfloat a); extern void mat4RotateY(mat4 out, mat4 in, GLfloat a);
extern void mat4RotateZ(mat4 out, mat4 in, GLfloat a); extern void mat4RotateZ(mat4 out, mat4 in, GLfloat a);
extern void mat4BuildLookAt(mat4 out, vec3 eye, vec3 center, vec3 up); extern void mat4BuildLookAt(mat4 out, vec3 eye, vec3 center, vec3 up);
extern void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat f, GLfloat n); extern void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat f, GLfloat n);
extern void vec3Subtract(vec3 out, vec3 a, vec3 b);
extern void vec3CrossProduct(vec3 out, vec3 a, vec3 b);
extern void vec3Normalise(vec3 out, vec3 in);
extern GLfloat vec3Length(vec3 in);
extern GLfloat vec3DotProduct(vec3 a, vec3 b);
#endif #endif