u05-2 (FINALLY)
This commit is contained in:
parent
9608484ce1
commit
83b313919b
|
@ -0,0 +1,22 @@
|
|||
GLEW_LIBS=$(shell pkgconf glew --libs)
|
||||
GLFW_LIBS=$(shell pkgconf glfw3 --libs)
|
||||
|
||||
OBJ = main.o matrixMath.o transformation.o
|
||||
SHADERS = fragmentShader.c vertexShader.c
|
||||
|
||||
cg1.out: $(OBJ) $(SHADERS)
|
||||
gcc -o $@ $(OBJ) -lm $(GLEW_LIBS) $(GLFW_LIBS)
|
||||
|
||||
%Shader.c: %Shader.glsl
|
||||
xxd -i $? > $@
|
||||
|
||||
main.o: $(SHADERS) matrixMath.h transformation.h
|
||||
|
||||
%.o: %.c
|
||||
gcc -c $<
|
||||
|
||||
run: cg1.out
|
||||
./cg1.out
|
||||
|
||||
clean:
|
||||
rm $(SHADERS) $(OBJ) cg1.out
|
|
@ -0,0 +1,5 @@
|
|||
#version 330 core
|
||||
in vec3 color;
|
||||
void main() {
|
||||
gl_FragColor = vec4(color, 1.0);
|
||||
}
|
|
@ -0,0 +1,351 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "vertexShader.c"
|
||||
#include "fragmentShader.c"
|
||||
|
||||
#include "matrixMath.h"
|
||||
#include "transformation.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#define RESTART 345678
|
||||
|
||||
GLuint program;
|
||||
GLuint vao;
|
||||
GLuint cubeIndicesBufferObject;
|
||||
|
||||
GLFWwindow* window;
|
||||
|
||||
GLfloat aspectRatio = 1.0f;
|
||||
|
||||
GLfloat step = 0.0f;
|
||||
|
||||
GLfloat cameraPosition[3] = {0.0f, 0.0f, 2.0f};
|
||||
|
||||
GLfloat cube[] = {
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
1.0f, -1.0f, 1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
-1.0f, -1.0f, -1.0f
|
||||
};
|
||||
|
||||
GLfloat ground[] = {
|
||||
1.0f, 0.0f, 1.0f,
|
||||
1.0f, 0.0f, -1.0f,
|
||||
-1.0f, 0.0f, 0.0f,
|
||||
-1.0f, 0.0f, -1.0f
|
||||
};
|
||||
|
||||
GLuint cubeIndices[] = {
|
||||
0, 1, 2,
|
||||
1, 3, 2,
|
||||
|
||||
1, 7, 3,
|
||||
1, 5, 7,
|
||||
|
||||
4, 6, 5,
|
||||
5, 6, 7,
|
||||
|
||||
0, 2, 4,
|
||||
4, 2, 6,
|
||||
|
||||
7, 6, 3,
|
||||
6, 2, 3,
|
||||
|
||||
4, 5, 1,
|
||||
4, 1, 0
|
||||
};
|
||||
|
||||
GLuint groundIndices[] = {
|
||||
0, 1, 2,
|
||||
1, 3, 2
|
||||
};
|
||||
|
||||
void handleInputs(void) {
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
cameraPosition[2] += 0.02f;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
cameraPosition[2] -= 0.02f;
|
||||
}
|
||||
}
|
||||
|
||||
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||
// if (action == GLFW_PRESS) {
|
||||
// if (key == GLFW_KEY_W) {
|
||||
// cameraVelocity[0] += 0.1f;
|
||||
// }
|
||||
// if (key == GLFW_KEY_S) {
|
||||
// cameraVelocity[0] -= 0.1f;
|
||||
// }
|
||||
// if (key == GLFW_KEY_S)
|
||||
// }
|
||||
}
|
||||
|
||||
void init(void) {
|
||||
// create and compile vertex shader
|
||||
const GLchar *vertexTextConst = vertexShader_glsl;
|
||||
|
||||
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexTextConst, &vertexShader_glsl_len);
|
||||
glCompileShader(vertexShader);
|
||||
|
||||
|
||||
GLint status;
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
|
||||
|
||||
if (!status) {
|
||||
printf("Error compiling vertex shader: ");
|
||||
GLchar infoLog[1024];
|
||||
glGetShaderInfoLog(vertexShader, 1024, NULL, infoLog);
|
||||
printf("%s",infoLog);
|
||||
}
|
||||
|
||||
vertexTextConst = NULL;
|
||||
|
||||
|
||||
|
||||
// create and compile fragment shader
|
||||
|
||||
const GLchar *fragmentTextConst = fragmentShader_glsl;
|
||||
|
||||
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentTextConst, &fragmentShader_glsl_len);
|
||||
glCompileShader(fragmentShader);
|
||||
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
|
||||
|
||||
if (!status) {
|
||||
printf("Error compiling fragment shader: ");
|
||||
GLchar infoLog[1024];
|
||||
glGetShaderInfoLog(fragmentShader, 1024, NULL, infoLog);
|
||||
printf("%s",infoLog);
|
||||
}
|
||||
|
||||
// create and link shader program
|
||||
program = glCreateProgram();
|
||||
glAttachShader(program, vertexShader);
|
||||
glAttachShader(program, fragmentShader);
|
||||
glLinkProgram(program);
|
||||
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
||||
|
||||
if (!status) {
|
||||
printf("Error linking program: ");
|
||||
GLchar infoLog[1024];
|
||||
glGetProgramInfoLog(program, 1024, NULL, infoLog);
|
||||
printf("%s",infoLog);
|
||||
}
|
||||
glValidateProgram(program);
|
||||
|
||||
|
||||
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
|
||||
|
||||
if (!status) {
|
||||
printf("Error validating program: ");
|
||||
GLchar infoLog[1024];
|
||||
glGetProgramInfoLog(program, 1024, NULL, infoLog);
|
||||
printf("%s",infoLog);
|
||||
}
|
||||
|
||||
|
||||
GLuint triangleVertexBufferObject;
|
||||
glGenBuffers(1, &triangleVertexBufferObject);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cube), cube, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
|
||||
// create vertex array object
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
|
||||
glVertexAttribPointer(
|
||||
0,
|
||||
3,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
sizeof(GLfloat) * 3,
|
||||
0
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
|
||||
// ENABLE BACKFACE CULLING
|
||||
glFrontFace(GL_CW);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
|
||||
// ENABLE RESTARTING
|
||||
glEnable(GL_PRIMITIVE_RESTART);
|
||||
glPrimitiveRestartIndex(RESTART);
|
||||
|
||||
// ENABLE DEPTH TESTING
|
||||
// glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// DEFINE INDEX ARRAY FOR ELEMENT DRAWING
|
||||
glGenBuffers(1, &cubeIndicesBufferObject);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);
|
||||
|
||||
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
}
|
||||
|
||||
void draw(void) {
|
||||
|
||||
handleInputs();
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glUseProgram(program);
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject);
|
||||
|
||||
step += 0.002f;
|
||||
if (step > 1.0f) step -= 1.0f;
|
||||
GLfloat stepi = step * 3.14159 * 2;
|
||||
|
||||
|
||||
// ------------- MODEL TRANSFORMATION ---------------------
|
||||
// SCALE -> ROTATE -> TRANSLATE
|
||||
|
||||
GLfloat scaleFactor = 0.6f;
|
||||
// GLfloat cubePosition[3] = {0.0f, sin(3.14159f * 2 * step), 0.0f};
|
||||
GLfloat cubePosition[3] = {0.0f, 0.0f, 0.0f};
|
||||
GLfloat cubeScale[3] = {scaleFactor, scaleFactor, scaleFactor};
|
||||
|
||||
GLfloat modelTransformation[16];
|
||||
identity(modelTransformation);
|
||||
scale(modelTransformation, modelTransformation, cubeScale);
|
||||
|
||||
rotateY(modelTransformation, modelTransformation, stepi);
|
||||
rotateX(modelTransformation, modelTransformation, stepi + 1.0f);
|
||||
rotateZ(modelTransformation, modelTransformation, stepi + 0.5f);
|
||||
|
||||
translate(modelTransformation, modelTransformation, cubePosition);
|
||||
|
||||
|
||||
|
||||
// ------------- VIEWING TRANSFORMATION -------------------
|
||||
GLfloat origin[3] = {0.0f, 0.0f, 0.0f};
|
||||
GLfloat up[3] = {0.0f, 1.0f, 0.0f};
|
||||
|
||||
GLfloat viewingTransformation[16];
|
||||
lookAt(viewingTransformation, cameraPosition, origin, up);
|
||||
|
||||
|
||||
|
||||
// -------------- PROJECTION TRANSFORMATION ----------------
|
||||
GLfloat projectionTransformation[16];
|
||||
GLfloat near = 0.1f;
|
||||
GLfloat far = 5.0f;
|
||||
perspectiveProjection(projectionTransformation, near, far);
|
||||
|
||||
|
||||
|
||||
// -------------- NORMALISATION TRANSFORMATION -------------
|
||||
GLfloat normalisationTransformation[16];
|
||||
GLfloat r = 0.2f;
|
||||
GLfloat l = -0.2f;
|
||||
GLfloat t = 0.2f;
|
||||
GLfloat b = -0.2f;
|
||||
normalisedDeviceCoordinates(normalisationTransformation, r, l, t, b, near, far);
|
||||
|
||||
|
||||
// --------------- LAZY ASPECT RATIO ADJUSTMENT ------------
|
||||
GLfloat aspectScale[16];
|
||||
GLfloat aspectScaleVector[3] = {1.0f / aspectRatio, 1.0f, 1.0f};
|
||||
scaling(aspectScale, aspectScaleVector);
|
||||
|
||||
|
||||
GLfloat globalTransformation[16];
|
||||
identity(globalTransformation);
|
||||
|
||||
multiply(globalTransformation, viewingTransformation, globalTransformation);
|
||||
multiply(globalTransformation, projectionTransformation, globalTransformation);
|
||||
multiply(globalTransformation, normalisationTransformation, globalTransformation);
|
||||
multiply(globalTransformation, aspectScale, globalTransformation);
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, "globalTransformation"), 1, GL_FALSE, globalTransformation);
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, "modelTransformation"), 1, GL_FALSE, modelTransformation);
|
||||
glDrawElements(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(GLuint), GL_UNSIGNED_INT, NULL);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
aspectRatio = (float)width / height;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
GLfloat test[16] = {
|
||||
1.0f, 2.0f, 3.0f, 4.0f,
|
||||
5.0f, 6.0f, 7.0f, 8.0f,
|
||||
9.0f, 1.0f, 2.0f, 3.0f,
|
||||
4.0f, 5.0f, 6.0f, 7.0f
|
||||
};
|
||||
transpose(test, test);
|
||||
|
||||
GLfloat test2[16] = {
|
||||
0.0f, 1.0f, 0.0f, 1.0f,
|
||||
1.0f, 2.0f, 1.0f, 2.0f,
|
||||
2.0f, 3.0f, 2.0f, 3.0f,
|
||||
3.0f, 4.0f, 3.0f, 4.0f
|
||||
};
|
||||
transpose(test2, test2);
|
||||
|
||||
multiply(test, test, test2);
|
||||
// mat4Print(test);
|
||||
|
||||
// return 0;
|
||||
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
window = glfwCreateWindow(700, 700, "Computergrafik 1", NULL, NULL);
|
||||
|
||||
if (!window) {
|
||||
printf("Failed to create window\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// register keyboard event handler
|
||||
glfwSetKeyCallback(window, keyboardHandler);
|
||||
|
||||
glewInit();
|
||||
|
||||
printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION));
|
||||
|
||||
|
||||
init();
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
draw();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,198 @@
|
|||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <GL/glew.h>
|
||||
#include <string.h>
|
||||
|
||||
// MATRICES IN COLUMN MAJOR
|
||||
|
||||
void vec3Zero(GLfloat* out) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
out[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void vec3Add(GLfloat* out, GLfloat* a, GLfloat* b) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
out[i] = a[i] + b[i];
|
||||
}
|
||||
}
|
||||
|
||||
void vec3Multiply(GLfloat* out, GLfloat* a, GLfloat x) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
out[i] = a[i] * x;
|
||||
}
|
||||
}
|
||||
|
||||
void vec3Subtract(GLfloat* out, GLfloat* a, GLfloat* b) {
|
||||
GLfloat minusB[3];
|
||||
vec3Multiply(minusB, b, -1);
|
||||
vec3Add(out, a, minusB);
|
||||
}
|
||||
|
||||
void vec3Cross(GLfloat* out, GLfloat* a, GLfloat* b) {
|
||||
GLfloat result[3];
|
||||
result[0] = a[1] * b[2] - a[2] * b[1];
|
||||
result[1] = a[2] * b[0] - a[0] * b[2];
|
||||
result[2] = a[0] * b[1] - a[1] * b[0];
|
||||
memcpy(out, result, sizeof(GLfloat) * 3);
|
||||
}
|
||||
|
||||
GLfloat vec3Length(GLfloat* a) {
|
||||
return (GLfloat)sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
|
||||
}
|
||||
|
||||
GLfloat vec3Dot(GLfloat* a, GLfloat* b) {
|
||||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
||||
}
|
||||
|
||||
void vec3Normalise(GLfloat* out, GLfloat* a) {
|
||||
vec3Multiply(out, a, 1 / vec3Length(a));
|
||||
}
|
||||
// CREATE 4x4 IDENTITY MATRIX
|
||||
void identity(GLfloat* out) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
out[i] = (i % 4 == i / 4);
|
||||
}
|
||||
}
|
||||
|
||||
// CREATE 4x4 TRANSLATION MATRIX
|
||||
void translation(GLfloat* out, GLfloat* v) {
|
||||
identity(out);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
out[3 * 4 + i] = v[i];
|
||||
}
|
||||
}
|
||||
|
||||
// CREATE 4x4 SCALING MATRIX
|
||||
void scaling(GLfloat* out, GLfloat* v) {
|
||||
identity(out);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
out[i * 5] = v[i];
|
||||
}
|
||||
}
|
||||
|
||||
// CREATE 4x4 ROTATION MATRIX AROUND Z AXIS
|
||||
/* cos a -sin a 0 0
|
||||
* sin a cos a 0 0
|
||||
* 0 0 1 0
|
||||
* 0 0 0 1
|
||||
*/
|
||||
void rotationZ(GLfloat* out, GLfloat angle) {
|
||||
identity(out);
|
||||
out[0] = cos(angle);
|
||||
out[1] = sin(angle);
|
||||
out[4] = -sin(angle);
|
||||
out[5] = cos(angle);
|
||||
}
|
||||
|
||||
// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS
|
||||
void rotationY(GLfloat* out, GLfloat angle) {
|
||||
identity(out);
|
||||
out[0] = cos(angle);
|
||||
out[2] = -sin(angle);
|
||||
out[8] = sin(angle);
|
||||
out[10] = cos(angle);
|
||||
}
|
||||
|
||||
// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS
|
||||
void rotationX(GLfloat* out, GLfloat angle) {
|
||||
identity(out);
|
||||
out[5] = cos(angle);
|
||||
out[6] = sin(angle);
|
||||
out[9] = -sin(angle);
|
||||
out[10] = cos(angle);
|
||||
}
|
||||
|
||||
// MULTIPLY ANY TO MATRICES
|
||||
void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB) {
|
||||
int sizeOut = hA * wB;
|
||||
GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat));
|
||||
for (int i = 0; i < sizeOut; i++) {
|
||||
result[i] = 0;
|
||||
for (int j = 0; j < wA; j++) {
|
||||
result[i] += A[j * hA + i % hA] * B[j + i / hA * wB];
|
||||
}
|
||||
}
|
||||
memcpy(out, result, sizeOut * sizeof(GLfloat));
|
||||
free(result);
|
||||
result = NULL;
|
||||
}
|
||||
|
||||
// MULTIPLY TWO 4x4 MATRICES
|
||||
void multiply(GLfloat* out, GLfloat* A, GLfloat* B) {
|
||||
multiplyAny(out, A, B, 4, 4, 4);
|
||||
}
|
||||
|
||||
// MULTIPLY in WITH TRANSLATION MATRIX OF v
|
||||
void translate(GLfloat* out, GLfloat* in, GLfloat* v) {
|
||||
GLfloat translationMatrix[16];
|
||||
translation(translationMatrix, v);
|
||||
multiply(out, translationMatrix, in);
|
||||
}
|
||||
|
||||
// MULTIPLY in WITH SCALING MATRIX OF v
|
||||
void scale(GLfloat* out, GLfloat* in, GLfloat* v) {
|
||||
GLfloat scalingMatrix[16];
|
||||
scaling(scalingMatrix, v);
|
||||
multiply(out, scalingMatrix, in);
|
||||
}
|
||||
|
||||
// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Z AXIS
|
||||
void rotateZ(GLfloat* out, GLfloat* in, GLfloat angle) {
|
||||
GLfloat rotationMatrix[16];
|
||||
rotationZ(rotationMatrix, angle);
|
||||
multiply(out, rotationMatrix, in);
|
||||
}
|
||||
// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Y AXIS
|
||||
void rotateY(GLfloat* out, GLfloat* in, GLfloat angle) {
|
||||
GLfloat rotationMatrix[16];
|
||||
rotationY(rotationMatrix, angle);
|
||||
multiply(out, rotationMatrix, in);
|
||||
}
|
||||
// MULTIPLY in WITH ROTATION MATRIX OF a AROUND X AXIS
|
||||
void rotateX(GLfloat* out, GLfloat* in, GLfloat angle) {
|
||||
GLfloat rotationMatrix[16];
|
||||
rotationX(rotationMatrix, angle);
|
||||
multiply(out, rotationMatrix, in);
|
||||
}
|
||||
|
||||
void transposeAny(GLfloat* out, GLfloat* in, int w, int h) {
|
||||
int size = w * h;
|
||||
GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat));
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
result[i] = in[(i % w) * h + i / w];
|
||||
}
|
||||
|
||||
memcpy(out, result, size * sizeof(GLfloat));
|
||||
free(result);
|
||||
result = NULL;
|
||||
}
|
||||
|
||||
|
||||
void transpose(GLfloat* out, GLfloat* in) {
|
||||
transposeAny(out, in, 4, 4);
|
||||
}
|
||||
|
||||
void printAny(GLfloat* M, int w, int h) {
|
||||
GLfloat* transposed = (GLfloat*) malloc(w * h * sizeof(GLfloat));
|
||||
transposeAny(transposed, M, w, h);
|
||||
|
||||
for (int i = 0; i < h; i++) {
|
||||
for (int j = 0; j < w; j++) {
|
||||
printf("%.4f ", transposed[i * w + j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
free(transposed);
|
||||
transposed = NULL;
|
||||
}
|
||||
|
||||
void vec3Print(GLfloat* a) {
|
||||
printAny(a, 1, 3);
|
||||
}
|
||||
|
||||
void mat4Print(GLfloat* m) {
|
||||
printAny(m, 4, 4);
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef MATRIX_MATH
|
||||
#define MATRIX_MATH
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
extern void vec3Zero(GLfloat* out);
|
||||
extern void vec3Add(GLfloat* out, GLfloat* a, GLfloat* b);
|
||||
extern void vec3Multiply(GLfloat* out, GLfloat* a, GLfloat x);
|
||||
extern void vec3Subtract(GLfloat* out, GLfloat* a, GLfloat* b);
|
||||
extern void vec3Cross(GLfloat* out, GLfloat* a, GLfloat* b);
|
||||
extern void vec3Normalise(GLfloat* out, GLfloat* a);
|
||||
extern GLfloat vec3Length(GLfloat* a);
|
||||
extern GLfloat vec3Dot(GLfloat* a, GLfloat* b);
|
||||
|
||||
extern void identity(GLfloat* out);
|
||||
extern void translation(GLfloat* out, GLfloat* v);
|
||||
extern void scaling(GLfloat* out, GLfloat* v);
|
||||
extern void rotationZ(GLfloat* out, GLfloat angle);
|
||||
extern void rotationY(GLfloat* out, GLfloat angle);
|
||||
extern void rotationX(GLfloat* out, GLfloat angle);
|
||||
|
||||
extern void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB);
|
||||
extern void multiply(GLfloat* out, GLfloat* A, GLfloat* B);
|
||||
|
||||
extern void translate(GLfloat* out, GLfloat* in, GLfloat* v);
|
||||
extern void scale(GLfloat* out, GLfloat* in, GLfloat* v);
|
||||
extern void rotateZ(GLfloat* out, GLfloat* in, GLfloat angle);
|
||||
extern void rotateY(GLfloat* out, GLfloat* in, GLfloat angle);
|
||||
extern void rotateX(GLfloat* out, GLfloat* in, GLfloat angle);
|
||||
|
||||
extern void transposeAny(GLfloat* out, GLfloat* in, int w, int h);
|
||||
extern void transpose(GLfloat* out, GLfloat* in);
|
||||
|
||||
extern void printAny(GLfloat* M, int w, int h);
|
||||
extern void vec3Print(GLfloat* a);
|
||||
extern void mat4Print(GLfloat* m);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,80 @@
|
|||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <GL/glew.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "matrixMath.h"
|
||||
|
||||
void lookAt(GLfloat* out, GLfloat* eye, GLfloat* look, GLfloat* up) {
|
||||
|
||||
|
||||
GLfloat n[3];
|
||||
vec3Subtract(n, eye, look);
|
||||
|
||||
GLfloat u[3];
|
||||
vec3Cross(u, up, n);
|
||||
|
||||
GLfloat v[3];
|
||||
vec3Cross(v, n, u);
|
||||
|
||||
|
||||
vec3Normalise(n, n);
|
||||
vec3Normalise(u, u);
|
||||
vec3Normalise(v, v);
|
||||
|
||||
|
||||
GLfloat Mr[16];
|
||||
identity(Mr);
|
||||
|
||||
memcpy(&Mr[0], u, sizeof(GLfloat) * 3);
|
||||
memcpy(&Mr[4], v, sizeof(GLfloat) * 3);
|
||||
memcpy(&Mr[8], n, sizeof(GLfloat) * 3);
|
||||
transpose(Mr, Mr);
|
||||
|
||||
|
||||
GLfloat t[3];
|
||||
vec3Multiply(u, u, -1);
|
||||
vec3Multiply(v, v, -1);
|
||||
vec3Multiply(n, n, -1);
|
||||
|
||||
|
||||
|
||||
t[0] = vec3Dot(u, eye);
|
||||
t[1] = vec3Dot(v, eye);
|
||||
t[2] = vec3Dot(n, eye);
|
||||
|
||||
|
||||
memcpy(&Mr[12], t, sizeof(GLfloat) * 3);
|
||||
|
||||
memcpy(out, Mr, sizeof(GLfloat) * 16);
|
||||
}
|
||||
|
||||
void perspectiveProjection(GLfloat* out, GLfloat near, GLfloat far) {
|
||||
identity(out);
|
||||
|
||||
out[10] = 1 + (far / near);
|
||||
out[11] = - 1.0f / near;
|
||||
out[14] = far;
|
||||
}
|
||||
|
||||
void normalisedDeviceCoordinates(GLfloat* out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f) {
|
||||
identity(out);
|
||||
|
||||
out[0] = 2 / (r - l);
|
||||
out[5] = 2 / (t - b);
|
||||
out[10] = -2 / (f - n);
|
||||
|
||||
out[12] = - (r + l) / (r - l);
|
||||
out[13] = - (t + b) / (t - b);
|
||||
out[14] = - (f + n) / (f - n);
|
||||
}
|
||||
|
||||
void normalisedPerspective(GLfloat* out, GLfloat near, GLfloat far, GLfloat r, GLfloat l, GLfloat t, GLfloat b) {
|
||||
GLfloat ndc[16];
|
||||
|
||||
perspectiveProjection(out, near, far);
|
||||
normalisedDeviceCoordinates(ndc, r, l, t, b, near, far);
|
||||
|
||||
multiply(out, ndc, out);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef TRANSFORMATION_H
|
||||
#define TRANSFORMATION_H
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
extern void lookAt(GLfloat* out, GLfloat* eye, GLfloat* look, GLfloat* up);
|
||||
extern void perspectiveProjection(GLfloat* out, GLfloat near, GLfloat far);
|
||||
extern void normalisedDeviceCoordinates(GLfloat* out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f);
|
||||
extern void normalisedPerspective(GLfloat* out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,9 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
uniform mat4 globalTransformation;
|
||||
uniform mat4 modelTransformation;
|
||||
out vec3 color;
|
||||
void main() {
|
||||
color = aPosition / 2 + vec3(0.5, 0.5, 0.5);
|
||||
gl_Position = globalTransformation * modelTransformation * vec4(aPosition, 1.0);
|
||||
}
|
Loading…
Reference in New Issue