aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c207
1 files changed, 122 insertions, 85 deletions
diff --git a/src/main.c b/src/main.c
index c296691..09df1bb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,7 +8,9 @@
#include <math.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
-#include "../include/stb_image.h"
+
+#define STB_IMAGE_IMPLEMENTATION
+#include <stb_image.h>
#include "main.h"
#include "linear.h"
@@ -21,73 +23,54 @@ struct Camera {
};
float vertices[] = {
- -0.5,-0.5,-0.5, 0.0, 0.0,-1.0,
- 0.5,-0.5,-0.5, 0.0, 0.0,-1.0,
- 0.5, 0.5,-0.5, 0.0, 0.0,-0.0,
- 0.5, 0.5,-0.5, 0.0, 0.0,-0.0,
- -0.5, 0.5,-0.5, 0.0, 0.0,-1.0,
- -0.5,-0.5,-0.5, 0.0, 0.0,-1.0,
-
- -0.5,-0.5, 0.5, 0.0, 0.0, 1.0,
- 0.5,-0.5, 0.5, 0.0, 0.0, 1.0,
- 0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
- 0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
- -0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
- -0.5,-0.5, 0.5, 0.0, 0.0, 1.0,
-
- -0.5,-0.5,-0.5,-1.0, 0.0, 0.0,
- -0.5, 0.5,-0.5,-1.0, 0.0, 0.0,
- -0.5, 0.5, 0.5,-1.0, 0.0, 0.0,
- -0.5, 0.5, 0.5,-1.0, 0.0, 0.0,
- -0.5,-0.5, 0.5,-1.0, 0.0, 0.0,
- -0.5,-0.5,-0.5,-1.0, 0.0, 0.0,
-
- 0.5,-0.5,-0.5, 1.0, 0.0, 0.0,
- 0.5, 0.5,-0.5, 1.0, 0.0, 0.0,
- 0.5, 0.5, 0.5, 1.0, 0.0, 0.0,
- 0.5, 0.5, 0.5, 1.0, 0.0, 0.0,
- 0.5,-0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0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
+
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+ 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
+ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
+ -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
static void userError(const char *msg, const char *detail);
static void glfw_size_callback(GLFWwindow *window, int width, int height);
static void processInput(GLFWwindow *window);
static Mat4 processCameraInput(GLFWwindow *window, struct Camera *cameraObj, float deltaTime);
+static unsigned int loadTexture(char const *path);
void
userError(const char *msg, const char *detail)
@@ -194,6 +177,42 @@ processCameraInput(GLFWwindow *window, struct Camera *camObj, float deltaTime)
camObj->up);
}
+
+static unsigned int
+loadTexture(char const *path)
+{
+ unsigned int textureID;
+ glGenTextures(1, &textureID);
+
+ int width, height, channels;
+ unsigned char *data = stbi_load(path, &width, &height, &channels, 0);
+
+ if (data) {
+ GLenum format;
+ if (channels == 1)
+ format = GL_RED;
+ if (channels == 3)
+ format = GL_RED;
+ if (channels == 4)
+ format = GL_RGBA;
+
+ glBindTexture(GL_TEXTURE_2D, textureID);
+ glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ stbi_image_free(data);
+ return textureID;
+ }
+ char errorMsg[500];
+ sprintf(errorMsg, "file %200s not found", path);
+ userError("loadTexture() Error", errorMsg);
+ return textureID;
+}
+
int main()
{
GLFWwindow *window;
@@ -222,35 +241,41 @@ int main()
}
glEnable(GL_DEPTH_TEST);
+ stbi_set_flip_vertically_on_load(1);
unsigned int programColor = shaderCreateProgram("shaders/color.vsh",
"shaders/color.fsh");
unsigned int programLight = shaderCreateProgram("shaders/lightsource.vsh",
"shaders/lightsource.fsh");
- unsigned int VBO, VAO, EBO, lightVAO;
+ unsigned int VBO, VAO, lightVAO;
+ int vertexSize = 8;
+
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
-
glBindVertexArray(VAO);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (void *)(6 * sizeof(float)));
+ glEnableVertexAttribArray(2);
+
glBindVertexArray(lightVAO);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
+ unsigned int diffuseMap = loadTexture("textures/container2.png");
+ unsigned int specularMap = loadTexture("textures/container2_specular.png");
+
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
@@ -262,7 +287,7 @@ int main()
Mat4 model, view, proj;
Mat4 T, S, R;
- Vec3 lightPos = linearVec3(0.0, 3.0, -10.0);
+ Vec3 lightPos = linearVec3(0.0, 2.0, -7.0);
float dt, t, t0;
int width, height;
@@ -273,52 +298,64 @@ int main()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ /* -------------------
+ * frame calculations
+ * ------------------*/
t = (float)glfwGetTime();
dt = t - t0;
t0 = t;
- /* Color Object */
- T = linearTranslate(0.0, 0.0, -4.0);
+ // World Transformation
+ // --------------------
+ T = linearTranslate(-3.0, 0.0, -2.0);
R = linearRotate(180 * t / M_PI, 0, 1, 0);
S = linearScale(1.0, 1.0, 1.0);
-
model = linearMat4Muln(3, T, R, S);
+ // Camera and view transformations
+ // -------------------------------
view = processCameraInput(window, &mainCamera, dt);
-
glfwGetWindowSize(window, &width, &height);
proj = linearPerspective(35, (float)width / height, 0.1, 100);
+ /* ----------------------------
+ * Rendering
+ * ----------------------------
+ */
glUseProgram(programColor);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
-
shaderSetMatrixfv(programColor, "model", model.matrix[0], glUniformMatrix4fv);
shaderSetMatrixfv(programColor, "view", view.matrix[0], glUniformMatrix4fv);
shaderSetMatrixfv(programColor, "proj", proj.matrix[0], glUniformMatrix4fv);
shaderSetMatrixfv(programColor, "rotNormals", R.matrix[0], glUniformMatrix4fv);
- shaderSetfv(programColor, "material.ambient", vec3(1.0, 0.5, 0.31), glUniform3fv);
- shaderSetfv(programColor, "material.diffuse", vec3(1.0, 0.5, 0.31), glUniform3fv);
shaderSetfv(programColor, "material.specular", vec3(0.5, 0.5, 0.5), glUniform3fv);
- shaderSet1f(programColor, "material.shininess", 32.0f);
+ shaderSet1f(programColor, "material.shininess", 64.0f);
- shaderSetfv(programColor, "light.ambient", vec3(0.1, 0.1, 0.1), glUniform3fv);
+ shaderSetfv(programColor, "light.ambient", vec3(0.2, 0.2, 0.2), glUniform3fv);
shaderSetfv(programColor, "light.diffuse", vec3(0.5, 0.5, 0.5), glUniform3fv);
shaderSetfv(programColor, "light.specular", vec3(1.0, 1.0, 1.0), glUniform3fv);
shaderSetfv(programColor, "light.position", lightPos.vector, glUniform3fv);
shaderSetfv(programColor, "viewPos", mainCamera.position.vector, glUniform3fv);
+ shaderSet1i(programColor, "material.diffuse", 0);
+ shaderSet1i(programColor, "material.specular", 1);
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, diffuseMap);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, specularMap);
+ glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices) / sizeof(float));
- /* Light Source */
- model = linearMat4Mul(linearTranslatev(lightPos), linearScale(0.5, 0.5, 0.5));
+ /* -----------------
+ * Light Source
+ * -----------------*/
+ model = linearMat4Mul(linearTranslatev(lightPos), linearScale(0.2, 0.2, 0.2));
glUseProgram(programLight);
glBindVertexArray(lightVAO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
shaderSetMatrixfv(programColor, "model", model.matrix[0], glUniformMatrix4fv);
shaderSetMatrixfv(programColor, "view", view.matrix[0], glUniformMatrix4fv);
Feel free to download, copy and edit any repo