commit be62803f07e44f72e881c172bcb3bfd1766ce519
parent b6f08a045db3b6dd48ee900b6d56c15926c2b3bf
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 28 Jun 2023 10:12:59 +0900
add cube
Diffstat:
M | main.c | | | 108 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------- |
1 file changed, 74 insertions(+), 34 deletions(-)
diff --git a/main.c b/main.c
@@ -50,25 +50,60 @@ processInput(GLFWwindow *window)
int
main(void)
{
- float delta = 0.01;
float vertices[] = {
- // positions, texture coords
- 0.5, 0.5, 0.5, 1.0, 1.0,
- -0.5, 0.5, 0.5, 0.0, 1.0,
- -0.5, -0.5, 0.5, 0.0, 0.0,
- 0.5, -0.5, 0.5, 1.0, 0.0,
- 0.5, 0.5, -0.5, 0.0, 1.0,
- 0.5, -0.5, -0.5, 0.0, 0.0,
- };
- unsigned int indices[] = {
- 0, 1, 2,
- 0, 2, 3,
- 0, 3, 5,
- 0, 4, 5,
+ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
+ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
+
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
+ -0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
+
+ -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+ -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
+ -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+
+ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
+ 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
+ 0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+
+ -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
+ 0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
+ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
+
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
+ -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
+
+ int numCube = 2;
+ vec *cubePositions[numCube];
+ cubePositions[0] = vecMake((float []){0.0, 0.0, 0.0}, 3);
+ cubePositions[1] = vecMake((float []){0.5, 0.5, 0.0}, 3);
+ mat *modelMat[numCube];
+ for (int i = 0; i < numCube; i++)
+ modelMat[i] = translate(matMakeIdent(4), cubePositions[i]);
+
vec *axis0 = vecMake((float[]){1.41421356/2, 1.41421356/2, 0.0}, 3);
vec *axis1 = vecMake((float[]){0.0, 0.0, 1.0}, 3);
- vec *transvec = vecMake((float[]){0.5, -0.5, 0.0}, 3);
// initialize glfw
glfwInit();
@@ -103,10 +138,9 @@ main(void)
ShaderUse(shader);
// setup vbo
- unsigned int VAO, VBO, EBO;
+ unsigned int VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
@@ -116,9 +150,6 @@ main(void)
(void *)(3*sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
- GL_STATIC_DRAW);
// setup texture
int nTex = 2;
@@ -150,35 +181,44 @@ main(void)
stbi_image_free(data);
}
-
+ glEnable(GL_DEPTH_TEST);
+
+ float lastTime = (float) glfwGetTime();
+ float rotation = 0;
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(1.0, 1.0, 0.8, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- mat *transform = matMakeIdent(4);
- if (rotating) {
- rotate(transform, (float)glfwGetTime(), axis0);
- }
- translate(transform, transvec);
- if (rotating) {
- rotate(transform, (float)glfwGetTime(), axis1);
- }
ShaderSetFloat(shader, "alpha", alpha);
glUniform1i(glGetUniformLocation(shader->ID, "texture0"), 0);
glUniform1i(glGetUniformLocation(shader->ID, "texture1"), 1);
unsigned int tloc = glGetUniformLocation(shader->ID, "transform");
- glUniformMatrix4fv(tloc, 1, GL_FALSE, matDataPtr(transform));
- glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
- matFree(transform);
-
+ float now = (float) glfwGetTime();
+ for (int i = 0; i < numCube; i++) {
+ mat *transform = matCopy(modelMat[i]);
+ if (rotating) {
+ rotation += now-lastTime;
+ }
+ rotate(transform, rotation, axis0);
+ glUniformMatrix4fv(tloc, 1, GL_FALSE,
+ matDataPtr(transform));
+ glDrawArrays(GL_TRIANGLES, 0, 36);
+ matFree(transform);
+ }
+ lastTime = now;
+
glfwPollEvents();
glfwSwapBuffers(window);
}
vecFree(axis0);
vecFree(axis1);
+ for (int i = 0; i < sizeof(cubePositions)/sizeof(cubePositions[0]); i++) {
+ vecFree(cubePositions[i]);
+ matFree(modelMat[i]);
+ }
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);