Finish Coordinate Systems

This commit is contained in:
Joseph Ferano 2023-10-23 22:31:48 +07:00
parent f5cfcb9fdc
commit bc5ca68189
5 changed files with 135 additions and 60 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/opengl
/game/main
/game/texturepacker
/.idea/

58
lib.h
View File

@ -112,3 +112,61 @@ unsigned int compileShaderProgram(char* vertSrcPath, char* fragSrcPath, char* ge
return program;
}
float quadVerts[] = {
// Position // Color // UV
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int quadIndices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
float cubeVerts[] = {
-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
};

112
main.c
View File

@ -14,6 +14,8 @@
int SCREEN_WIDTH = 1024;
int SCREEN_HEIGHT = 768;
vec2 direction = {0};
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
(void)window;
@ -26,6 +28,20 @@ void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
direction[0] = 0;
direction[1] = 0;
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
direction[0] = 1;
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
direction[0] = -1;
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
direction[1] = -1;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
direction[1] = 1;
}
}
int main(void) {
@ -86,39 +102,28 @@ int main(void) {
stbi_image_free(container);
stbi_image_free(smiley);
float v1[] = {
// Position // Color // UV
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
glEnable(GL_DEPTH_TEST);
GLuint vaos[2];
GLuint vbos[2];
unsigned int EBO;
// unsigned int EBO;
glGenVertexArrays(2, vaos);
glGenBuffers(1, &EBO);
// glGenBuffers(1, &EBO);
glGenBuffers(2, vbos);
glBindVertexArray(vaos[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(v1), v1, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVerts), cubeVerts, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
// glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
// glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
@ -129,32 +134,46 @@ int main(void) {
int t2Loc = glGetUniformLocation(shaderProgram, "t2");
glUniform1i(t2Loc, 1);
mat4 trans;
glm_mat4_identity(trans);
// glm_rotate(trans, GLM_PI_2, (vec3){0.0f, 0.0f, 1.0f});
glm_scale(trans, (vec3){0.5f, 0.5f, 0.5f});
mat4 view;
glm_mat4_identity(view);
glm_translate(view, (vec3){0.0f, 0.0f, -0.6f});
mat4 projection;
glm_mat4_identity(projection);
float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
glm_perspective(45.0f, aspectRatio, 0.1f, 100.0f, projection);
mat4 trans2;
glm_mat4_identity(trans2);
glm_scale(trans2, (vec3){0.1f, 0.1f, 0.1f});
glm_translate(trans2, (vec3){-5.1f, 1.1f, 0.0f});
int cubeCount = 10;
vec3 cubePositions[cubeCount];
srand(arc4random());
for (int i = 0; i < cubeCount; i++) {
float x = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f;
float y = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f;
float z = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f;
cubePositions[i][0] = x;
cubePositions[i][1] = y;
cubePositions[i][2] = z;
}
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window)) {
float currentFrame = (float)glfwGetTime();
float deltaTime = currentFrame - lastFrame;
float dt = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// float spin = (sin(timeValue) / 2.0f) + 0.5f;
printf("%f\n", deltaTime);
glm_rotate(trans, (float)deltaTime * -1.0f, (vec3){0.0f, 0.0f, 1.0f});
// float spin = (sin(currentFrame) / 2.0f) + 0.5f;
// printf("%f\n", deltaTime);
int transLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transLoc, 1, GL_FALSE, trans[0]);
float speed = 0.5 * dt;
glm_translate(view, (vec3){direction[0] * speed, 0.0f, direction[1] * speed});
int matrixLocation;
matrixLocation = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, view[0]);
matrixLocation = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, projection[0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
@ -162,16 +181,19 @@ int main(void) {
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(vaos[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
for (int i = 0; i < cubeCount; i++) {
mat4 model;
glm_mat4_identity(model);
glm_scale(model, (vec3){0.1f, 0.1f, 0.1f});
glm_translate(model, cubePositions[i]);
glm_rotate(model, (i+1) * currentFrame, (vec3){1.0f, 1.0f, 0.0f});
mat4 trans2;
glm_mat4_identity(trans2);
glm_scale(trans2, (vec3){0.1f, 0.1f, 0.1f});
glm_translate(trans2, (vec3){cos(currentFrame * 4.0f) * 5.0f,
sin(currentFrame * 4.0f) * 5.0f,
0.0f});
glUniformMatrix4fv(transLoc, 1, GL_FALSE, trans2[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
matrixLocation = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, model[0]);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();

View File

@ -1,15 +1,12 @@
#version 330 core
in vec3 vPos;
in vec3 vCol;
in vec2 tCoord;
out vec4 FragColor;
uniform sampler2D t1;
uniform sampler2D t2;
uniform float mixAmount;
void main() {
vec2 flipped = vec2(-tCoord.x, tCoord.y);
// vec2 flipped = vec2(-tCoord.x, tCoord.y);
FragColor = mix(texture(t1, tCoord), texture(t2, tCoord), 0.3);
}

View File

@ -1,17 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
layout (location = 1) in vec2 aTexCoord;
out vec3 vPos;
out vec3 vCol;
out vec2 tCoord;
uniform float offset;
uniform mat4 transform;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = transform * vec4(aPos, 1.0);
vPos = aPos;
vCol = aColor;
gl_Position = projection * view * model * vec4(aPos, 1.0);
tCoord = aTexCoord;
}