WIP objecthandler stuff
This commit is contained in:
parent
4aca5eead4
commit
73be72585c
541
src/main.c
541
src/main.c
|
@ -32,11 +32,11 @@ int flipFlag = 1;
|
||||||
|
|
||||||
GLuint textures[NUM_TEXTURES];
|
GLuint textures[NUM_TEXTURES];
|
||||||
char* textureFiles[NUM_TEXTURES] = {
|
char* textureFiles[NUM_TEXTURES] = {
|
||||||
"../texture/earth/day.png",
|
"../texture/earth/day.png",
|
||||||
"../texture/earth/night.png",
|
"../texture/earth/night.png",
|
||||||
"../texture/earth/clouds.png",
|
"../texture/earth/clouds.png",
|
||||||
"../texture/earth/ocean_mask.png",
|
"../texture/earth/ocean_mask.png",
|
||||||
"../texture/earth/normal.png"
|
"../texture/earth/normal.png"
|
||||||
};
|
};
|
||||||
|
|
||||||
ObjectData* objectData;
|
ObjectData* objectData;
|
||||||
|
@ -59,353 +59,362 @@ vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
|
||||||
|
|
||||||
int numModels = 4;
|
int numModels = 4;
|
||||||
char* models[] = {
|
char* models[] = {
|
||||||
"../obj/Yblock.obj",
|
"../obj/Yblock.obj",
|
||||||
"../obj/Zblock.obj",
|
"../obj/Zblock.obj",
|
||||||
"../obj/Yblock_rotated.obj",
|
"../obj/Yblock_rotated.obj",
|
||||||
"../obj/Xblock.obj"
|
"../obj/Xblock.obj"
|
||||||
};
|
};
|
||||||
|
|
||||||
// input handler for camera movement
|
// input handler for camera movement
|
||||||
void handleInputs(double deltaTime) {
|
void handleInputs(double deltaTime) {
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||||
cameraPosition.z += deltaTime * 10;
|
cameraPosition.z += deltaTime * 10;
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||||
cameraPosition.z -= deltaTime * 10;
|
cameraPosition.z -= deltaTime * 10;
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||||
cameraPosition.x += deltaTime * 10;
|
cameraPosition.x += deltaTime * 10;
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||||
cameraPosition.x -= deltaTime * 10;
|
cameraPosition.x -= deltaTime * 10;
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||||
cameraPosition.y += deltaTime * 10;
|
cameraPosition.y += deltaTime * 10;
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
|
||||||
cameraPosition.y -= deltaTime * 10;
|
cameraPosition.y -= deltaTime * 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// input handler to quit with ESC
|
// input handler to quit with ESC
|
||||||
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
if (key == GLFW_KEY_ESCAPE) {
|
if (key == GLFW_KEY_ESCAPE) {
|
||||||
exitRequested = true;
|
exitRequested = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadTexture(char* textureFile, GLuint* texture) {
|
void loadTexture(char* textureFile, GLuint* texture) {
|
||||||
int width, height, nrChannels;
|
int width, height, nrChannels;
|
||||||
unsigned char* image = stbi_load(textureFile, &width, &height, &nrChannels, 0);
|
unsigned char* image = stbi_load(textureFile, &width, &height, &nrChannels, 0);
|
||||||
|
|
||||||
// default: 3 channels, RGB
|
// default: 3 channels, RGB
|
||||||
|
|
||||||
GLenum channelFormats[] = {
|
GLenum channelFormats[] = {
|
||||||
0,
|
0,
|
||||||
GL_RED,
|
GL_RED,
|
||||||
GL_RG,
|
GL_RG,
|
||||||
GL_RGB,
|
GL_RGB,
|
||||||
GL_RGBA
|
GL_RGBA
|
||||||
};
|
};
|
||||||
GLenum format = channelFormats[nrChannels];
|
GLenum format = channelFormats[nrChannels];
|
||||||
|
|
||||||
glGenTextures(1, texture);
|
glGenTextures(1, texture);
|
||||||
glBindTexture(GL_TEXTURE_2D, *texture);
|
glBindTexture(GL_TEXTURE_2D, *texture);
|
||||||
|
|
||||||
printf("%s - %d\n", textureFile, nrChannels);
|
printf("%s - %d\n", textureFile, nrChannels);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, image);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, image);
|
||||||
// load texture using previously determined format ----- ^^^^^^
|
// load texture using previously determined format ----- ^^^^^^
|
||||||
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
stbi_image_free(image);
|
stbi_image_free(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void init(void) {
|
void init(void) {
|
||||||
// create and compile vertex shader
|
// create and compile vertex shader
|
||||||
const GLchar *vertexTextConst = vertexShader_glsl;
|
const GLchar *vertexTextConst = vertexShader_glsl;
|
||||||
|
|
||||||
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
glShaderSource(vertexShader, 1, &vertexTextConst, &vertexShader_glsl_len);
|
glShaderSource(vertexShader, 1, &vertexTextConst, &vertexShader_glsl_len);
|
||||||
glCompileShader(vertexShader);
|
glCompileShader(vertexShader);
|
||||||
|
|
||||||
|
|
||||||
GLint status;
|
GLint status;
|
||||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
|
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
|
||||||
|
|
||||||
if (!status) {
|
if (!status) {
|
||||||
printf("Error compiling vertex shader: ");
|
printf("Error compiling vertex shader: ");
|
||||||
GLchar infoLog[1024];
|
GLchar infoLog[1024];
|
||||||
glGetShaderInfoLog(vertexShader, 1024, NULL, infoLog);
|
glGetShaderInfoLog(vertexShader, 1024, NULL, infoLog);
|
||||||
printf("%s",infoLog);
|
printf("%s",infoLog);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
vertexTextConst = NULL;
|
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);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
glValidateProgram(program);
|
||||||
|
|
||||||
|
|
||||||
|
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
|
||||||
|
|
||||||
// create and compile fragment shader
|
if (!status) {
|
||||||
|
printf("Error validating program: ");
|
||||||
const GLchar *fragmentTextConst = fragmentShader_glsl;
|
GLchar infoLog[1024];
|
||||||
|
glGetProgramInfoLog(program, 1024, NULL, infoLog);
|
||||||
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
printf("%s",infoLog);
|
||||||
glShaderSource(fragmentShader, 1, &fragmentTextConst, &fragmentShader_glsl_len);
|
exit(1);
|
||||||
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);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
glValidateProgram(program);
|
|
||||||
|
|
||||||
|
|
||||||
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
|
// --------------- READ MODEL FILES
|
||||||
|
|
||||||
if (!status) {
|
objectData = malloc(numModels * sizeof(ObjectData));
|
||||||
printf("Error validating program: ");
|
for (int i = 0; i < numModels; i++) {
|
||||||
GLchar infoLog[1024];
|
ObjectData objData = readObjFiles(models, 1);
|
||||||
glGetProgramInfoLog(program, 1024, NULL, infoLog);
|
|
||||||
printf("%s",infoLog);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
glGenVertexArrays(1, &objData.vao);
|
||||||
|
glGenBuffers(1, &objData.vbo);
|
||||||
|
|
||||||
// --------------- READ MODEL FILES
|
glBindVertexArray(objData.vao);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, objData.vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, objData.numFaces * 3 * sizeof(Vertex), objData.vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
objectData = readObjFiles(models, numModels);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(3 * sizeof(GLfloat)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(6 * sizeof(GLfloat)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
stbi_set_flip_vertically_on_load(flipFlag);
|
objectData[i] = objData;
|
||||||
// -------------- READ TEXTURE FILES
|
}
|
||||||
for (int i = 0; i < NUM_TEXTURES; i++) {
|
|
||||||
loadTexture(textureFiles[i], &textures[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ENABLE BACKFACE CULLING
|
stbi_set_flip_vertically_on_load(flipFlag);
|
||||||
glFrontFace(GL_CCW);
|
// -------------- READ TEXTURE FILES
|
||||||
glEnable(GL_CULL_FACE);
|
for (int i = 0; i < NUM_TEXTURES; i++) {
|
||||||
|
loadTexture(textureFiles[i], &textures[i]);
|
||||||
|
}
|
||||||
|
|
||||||
// ENABLE DEPTH BUFFER
|
// ENABLE BACKFACE CULLING
|
||||||
glEnable(GL_DEPTH_TEST);
|
glFrontFace(GL_CCW);
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
// ENABLE DEPTH BUFFER
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateStats() {
|
void updateStats() {
|
||||||
printf("\rFPS: %.1f", framesSinceUpdate / timeSinceUpdate);
|
printf("\rFPS: %.1f", framesSinceUpdate / timeSinceUpdate);
|
||||||
printf(" - Camera Position: [%f, %f, %f]", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
printf(" - Camera Position: [%f, %f, %f]", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw(void) {
|
void draw(void) {
|
||||||
|
|
||||||
// FPS Counter
|
// FPS Counter
|
||||||
framesSinceUpdate++;
|
framesSinceUpdate++;
|
||||||
double deltaTime = glfwGetTime();
|
double deltaTime = glfwGetTime();
|
||||||
timeSinceUpdate += deltaTime;
|
timeSinceUpdate += deltaTime;
|
||||||
glfwSetTime(0.0f);
|
glfwSetTime(0.0f);
|
||||||
|
|
||||||
if (timeSinceUpdate >= timeBetweenUpdates) {
|
if (timeSinceUpdate >= timeBetweenUpdates) {
|
||||||
updateStats();
|
updateStats();
|
||||||
timeSinceUpdate = 0.0f;
|
timeSinceUpdate = 0.0f;
|
||||||
framesSinceUpdate = 0;
|
framesSinceUpdate = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// camera movement
|
// camera movement
|
||||||
handleInputs(deltaTime);
|
handleInputs(deltaTime);
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
|
|
||||||
|
// step for rotations
|
||||||
|
// counts up to 1.0 and then resets back to 0.0 forever
|
||||||
|
step += deltaTime / 15;
|
||||||
|
if (step > 1.0f) step -= 1.0f;
|
||||||
|
if (step < 0.0f) step += 1.0f;
|
||||||
|
|
||||||
// step for rotations
|
// step multiplied by pi * 2 for use in rotation and trig functions
|
||||||
// counts up to 1.0 and then resets back to 0.0 forever
|
GLfloat stepi = step * pi * 2;
|
||||||
step += deltaTime / 15;
|
|
||||||
if (step > 1.0f) step -= 1.0f;
|
|
||||||
if (step < 0.0f) step += 1.0f;
|
|
||||||
|
|
||||||
// step multiplied by pi * 2 for use in rotation and trig functions
|
// ------------- VIEWING TRANSFORMATION -------------------
|
||||||
GLfloat stepi = step * pi * 2;
|
vec3 origin = {0.0f, 0.0f, 0.0f};
|
||||||
|
vec3 up = {0.0f, 1.0f, 0.0f};
|
||||||
|
|
||||||
|
mat4 viewingTransformation;
|
||||||
|
lookAt(&viewingTransformation, &cameraPosition, &origin, &up);
|
||||||
|
|
||||||
// ------------- MODEL TRANSFORMATION ---------------------
|
// -------------- PROJECTION TRANSFORMATION ----------------
|
||||||
// SCALE -> ROTATE -> TRANSLATE
|
mat4 projectionTransformation;
|
||||||
|
GLfloat near = 0.1f;
|
||||||
|
GLfloat far = 20.0f;
|
||||||
|
perspectiveProjection(&projectionTransformation, near, far);
|
||||||
|
|
||||||
mat4 modelTransformation;
|
// -------------- NORMALISATION TRANSFORMATION -------------
|
||||||
identity(&modelTransformation);
|
mat4 normalisationTransformation;
|
||||||
|
GLfloat fovy = pi / 2;
|
||||||
|
normalisedDeviceCoordinatesFov(&normalisationTransformation, fovy, aspectRatio, near, far);
|
||||||
|
|
||||||
rotateY(&modelTransformation, &modelTransformation, stepi * 2);
|
mat4 projection;
|
||||||
rotateZ(&modelTransformation, &modelTransformation, -23.5f / 180 * pi);
|
identity(&projection);
|
||||||
|
multiply(&projection, &projectionTransformation, &projection);
|
||||||
|
multiply(&projection, &normalisationTransformation, &projection);
|
||||||
|
|
||||||
// ------------- VIEWING TRANSFORMATION -------------------
|
// ------------- DRAW EACH OBJECT SEPARATELY ----------------
|
||||||
vec3 origin = {0.0f, 0.0f, 0.0f};
|
for (int i = 0; i < numModels; i++) {
|
||||||
vec3 up = {0.0f, 1.0f, 0.0f};
|
// Reset model transformation for each object
|
||||||
|
mat4 modelTransformation;
|
||||||
|
identity(&modelTransformation);
|
||||||
|
|
||||||
mat4 viewingTransformation;
|
// Apply object-specific transformations
|
||||||
lookAt(&viewingTransformation, &cameraPosition, &origin, &up);
|
if (i == 0) {
|
||||||
|
// Example transformations for the first object
|
||||||
|
rotateY(&modelTransformation, &modelTransformation, stepi * 2);
|
||||||
|
} else if (i == 1) {
|
||||||
|
// Example transformations for the second object
|
||||||
|
rotateZ(&modelTransformation, &modelTransformation, -23.5f / 180 * pi);
|
||||||
|
} else if (i == 2) {
|
||||||
|
// Example transformations for the third object
|
||||||
|
rotateX(&modelTransformation, &modelTransformation, stepi);
|
||||||
|
} else if (i == 3) {
|
||||||
|
// Example transformations for the fourth object
|
||||||
|
translate(&modelTransformation, &modelTransformation, 1.0f, 0.0f, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
mat4 modelView;
|
||||||
|
identity(&modelView);
|
||||||
|
multiply(&modelView, &modelTransformation, &modelView);
|
||||||
|
multiply(&modelView, &viewingTransformation, &modelView);
|
||||||
|
|
||||||
|
// calculate matrix for normals
|
||||||
|
mat3 normalModelView;
|
||||||
|
mat3From4(&normalModelView, &modelView);
|
||||||
|
mat3Inverse(&normalModelView, &normalModelView);
|
||||||
|
mat3Transpose(&normalModelView, &normalModelView);
|
||||||
|
|
||||||
// -------------- PROJECTION TRANSFORMATION ----------------
|
// send transformation matrix to shader
|
||||||
mat4 projectionTransformation;
|
glUniformMatrix4fv(glGetUniformLocation(program, "modelView"), 1, GL_FALSE, (GLfloat*)&modelView);
|
||||||
GLfloat near = 0.1f;
|
glUniformMatrix3fv(glGetUniformLocation(program, "normalModelView"), 1, GL_FALSE, (GLfloat*)&normalModelView);
|
||||||
GLfloat far = 20.0f;
|
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)&projection);
|
||||||
perspectiveProjection(&projectionTransformation, near, far);
|
|
||||||
|
|
||||||
|
// SET MATERIAL DATA
|
||||||
|
glUniform1f(glGetUniformLocation(program, "shininess"), 60.0f * 4.0f);
|
||||||
|
|
||||||
|
// SET LIGHT DATA
|
||||||
|
glUniform4f(glGetUniformLocation(program, "lightColor"), 1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
glUniform4f(glGetUniformLocation(program, "ambientLight"), 0.05f, 0.05f, 0.05f, 1.0f);
|
||||||
|
|
||||||
// -------------- NORMALISATION TRANSFORMATION -------------
|
// BIND TEXTURES
|
||||||
mat4 normalisationTransformation;
|
GLuint textureLocation;
|
||||||
GLfloat fovy = pi / 2;
|
textureLocation = glGetUniformLocation(program, "day");
|
||||||
normalisedDeviceCoordinatesFov(&normalisationTransformation, fovy, aspectRatio, near, far);
|
glUniform1i(textureLocation, 0);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textures[DAY]);
|
||||||
|
|
||||||
|
textureLocation = glGetUniformLocation(program, "night");
|
||||||
|
glUniform1i(textureLocation, 1);
|
||||||
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textures[NIGHT]);
|
||||||
|
|
||||||
mat4 modelView;
|
textureLocation = glGetUniformLocation(program, "clouds");
|
||||||
identity(&modelView);
|
glUniform1i(textureLocation, 2);
|
||||||
multiply(&modelView, &modelTransformation, &modelView);
|
glActiveTexture(GL_TEXTURE2);
|
||||||
multiply(&modelView, &viewingTransformation, &modelView);
|
glBindTexture(GL_TEXTURE_2D, textures[CLOUDS]);
|
||||||
|
|
||||||
mat4 projection;
|
textureLocation = glGetUniformLocation(program, "ocean");
|
||||||
identity(&projection);
|
glUniform1i(textureLocation, 3);
|
||||||
multiply(&projection, &projectionTransformation, &projection);
|
glActiveTexture(GL_TEXTURE3);
|
||||||
multiply(&projection, &normalisationTransformation, &projection);
|
glBindTexture(GL_TEXTURE_2D, textures[OCEAN]);
|
||||||
|
|
||||||
// calculate matrix for normals
|
textureLocation = glGetUniformLocation(program, "normalMap");
|
||||||
mat3 normalModelView;
|
glUniform1i(textureLocation, 4);
|
||||||
mat3From4(&normalModelView, &modelView);
|
glActiveTexture(GL_TEXTURE4);
|
||||||
mat3Inverse(&normalModelView, &normalModelView);
|
glBindTexture(GL_TEXTURE_2D, textures[NORMAL]);
|
||||||
mat3Transpose(&normalModelView, &normalModelView);
|
|
||||||
|
|
||||||
// send transformation matrix to shader
|
// draw each object separately
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, "modelView"), 1, GL_FALSE, (GLfloat*)&modelView);
|
glBindVertexArray(objectData[i].vao);
|
||||||
glUniformMatrix3fv(glGetUniformLocation(program, "normalModelView"), 1, GL_FALSE, (GLfloat*)&normalModelView);
|
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)&projection);
|
}
|
||||||
|
|
||||||
|
|
||||||
//vec4 lightPosition = {cos(stepi) * 1000.0f, 0.0f, sin(stepi) * 1000.0f, 1.0f};
|
|
||||||
//multiplyAny((GLfloat *)&lightPosition, (GLfloat *)&viewingTransformation, (GLfloat *)&lightPosition, 4, 4, 1);
|
|
||||||
|
|
||||||
//glUniform3f(glGetUniformLocation(program, "lightPosition"), lightPosition.x, lightPosition.y, lightPosition.z);
|
|
||||||
|
|
||||||
|
|
||||||
// SET MATERIAL DATA
|
|
||||||
glUniform1f(glGetUniformLocation(program, "shininess"), 60.0f * 4.0f);
|
|
||||||
|
|
||||||
// SET LIGHT DATA
|
|
||||||
glUniform4f(glGetUniformLocation(program, "lightColor"), 1.0f, 1.0f, 1.0f, 1.0f);
|
|
||||||
glUniform4f(glGetUniformLocation(program, "ambientLight"), 0.05f, 0.05f, 0.05f, 1.0f);
|
|
||||||
|
|
||||||
|
|
||||||
// BIND TEXTURES
|
|
||||||
GLuint textureLocation;
|
|
||||||
textureLocation = glGetUniformLocation(program, "day");
|
|
||||||
glUniform1i(textureLocation, 0);
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textures[DAY]);
|
|
||||||
|
|
||||||
textureLocation = glGetUniformLocation(program, "night");
|
|
||||||
glUniform1i(textureLocation, 1);
|
|
||||||
glActiveTexture(GL_TEXTURE1);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textures[NIGHT]);
|
|
||||||
|
|
||||||
textureLocation = glGetUniformLocation(program, "clouds");
|
|
||||||
glUniform1i(textureLocation, 2);
|
|
||||||
glActiveTexture(GL_TEXTURE2);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textures[CLOUDS]);
|
|
||||||
|
|
||||||
textureLocation = glGetUniformLocation(program, "ocean");
|
|
||||||
glUniform1i(textureLocation, 3);
|
|
||||||
glActiveTexture(GL_TEXTURE3);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textures[OCEAN]);
|
|
||||||
|
|
||||||
textureLocation = glGetUniformLocation(program, "normalMap");
|
|
||||||
glUniform1i(textureLocation, 4);
|
|
||||||
glActiveTexture(GL_TEXTURE4);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textures[NORMAL]);
|
|
||||||
|
|
||||||
|
|
||||||
// draw!!1
|
|
||||||
for (int i = 0; i < numObj; i++)
|
|
||||||
{
|
|
||||||
glBindVertexArray(vao[i]);
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// change viewport size and adjust aspect ratio when changing window size
|
// change viewport size and adjust aspect ratio when changing window size
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
aspectRatio = (float)width / height;
|
aspectRatio = (float)width / height;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// initialise window
|
// initialise window
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
window = glfwCreateWindow(700, 700, "Computergrafik 1", NULL, NULL);
|
window = glfwCreateWindow(700, 700, "Computergrafik 1", NULL, NULL);
|
||||||
|
|
||||||
if (!window) {
|
if (!window) {
|
||||||
printf("Failed to create window\n");
|
printf("Failed to create window\n");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
// disable framerate cap
|
||||||
glfwMakeContextCurrent(window);
|
glfwSwapInterval(0);
|
||||||
|
|
||||||
// disable framerate cap
|
// register keyboard event handler
|
||||||
glfwSwapInterval(0);
|
glfwSetKeyCallback(window, keyboardHandler);
|
||||||
|
|
||||||
// register keyboard event handler
|
// initialise glew
|
||||||
glfwSetKeyCallback(window, keyboardHandler);
|
glewInit();
|
||||||
|
|
||||||
// initialise glew
|
printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION));
|
||||||
glewInit();
|
|
||||||
|
|
||||||
printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION));
|
init();
|
||||||
|
|
||||||
init();
|
// exit when window should close or exit is requested (ESC)
|
||||||
|
while (!glfwWindowShouldClose(window) && !exitRequested) {
|
||||||
|
draw();
|
||||||
|
|
||||||
// exit when window should close or exit is requested (ESC)
|
glfwSwapBuffers(window);
|
||||||
while (!glfwWindowShouldClose(window) && !exitRequested) {
|
glfwPollEvents();
|
||||||
draw();
|
}
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwTerminate();
|
||||||
glfwPollEvents();
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwTerminate();
|
return 0;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
|
@ -5,10 +5,11 @@
|
||||||
GLuint vao[2];
|
GLuint vao[2];
|
||||||
int numObj = 0;
|
int numObj = 0;
|
||||||
|
|
||||||
typedef struct ObjectData
|
typedef struct {
|
||||||
{
|
GLuint vao;
|
||||||
|
GLuint vbo;
|
||||||
|
int numFaces;
|
||||||
ParsedObjFile object;
|
ParsedObjFile object;
|
||||||
int numFaces;
|
|
||||||
} ObjectData;
|
} ObjectData;
|
||||||
|
|
||||||
void load_object(ParsedObjFile* object) {
|
void load_object(ParsedObjFile* object) {
|
||||||
|
@ -86,7 +87,8 @@ ObjectData* readObjFiles(char** path, int numModels) {
|
||||||
for (int i = 0; i < numModels; ++i) {
|
for (int i = 0; i < numModels; ++i) {
|
||||||
objects[i].object = readObjFile(path[i]);
|
objects[i].object = readObjFile(path[i]);
|
||||||
objects[i].numFaces = objects[i].object.length; // Assuming .length gives the number of faces
|
objects[i].numFaces = objects[i].object.length; // Assuming .length gives the number of faces
|
||||||
load_object(&objects[i].object);
|
objects[i].vbo =
|
||||||
|
load_object(&objects[i].object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return objects;
|
return objects;
|
||||||
|
|
Loading…
Reference in New Issue