292 lines
8.3 KiB
C++
292 lines
8.3 KiB
C++
#include <stdio.h>
|
|
#include <cmath>
|
|
|
|
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
#include "stb_image.h"
|
|
#include "shader.h"
|
|
|
|
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
|
glViewport(0,0, width, height);
|
|
}
|
|
|
|
void processInput( GLFWwindow* window){
|
|
if(glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
|
}
|
|
|
|
int main() {
|
|
printf("Hello OpenGL!\n");
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
|
|
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
|
if( window == NULL){
|
|
printf("Failed to create GLFW window!\n");
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
|
|
printf("Failed to initialize GLAD!\n");
|
|
return -1;
|
|
}
|
|
|
|
glViewport(0,0, 800, 600);
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
|
|
Shader shader("shader.vs", "shader.fs");
|
|
|
|
|
|
|
|
|
|
float vertices[] = {
|
|
-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,
|
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.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
|
|
};
|
|
|
|
/* unsigned int indices[] = {
|
|
0,1,3, // first triangle
|
|
1,2,3 // second triangle
|
|
}; */
|
|
|
|
|
|
|
|
// Vertex buffer objects!!
|
|
/* unsigned int VBO, VAO, EBO;
|
|
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);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); */
|
|
|
|
/* // Position attribute
|
|
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
|
glEnableVertexAttribArray(0);
|
|
// Color attribute
|
|
glVertexAttribPointer(1,3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3* sizeof(float)));
|
|
glEnableVertexAttribArray(1);
|
|
|
|
// texture attribute
|
|
glVertexAttribPointer(2,2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
|
|
glEnableVertexAttribArray(2); */
|
|
|
|
unsigned int VBO, VAO;
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
// position attribute
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
|
glEnableVertexAttribArray(0);
|
|
// texture coord attribute
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
|
int width, height, nrChannels;
|
|
unsigned int texture1;
|
|
glGenTextures(1, &texture1);
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
|
|
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
unsigned char* data = stbi_load("Textures/container.jpg", &width, &height, &nrChannels,0);
|
|
if(data){
|
|
std::cout<< width << " x " << height << std::endl;
|
|
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
} else{
|
|
std::cout << "Failed to load texture!" << std::endl;
|
|
}
|
|
|
|
stbi_image_free(data);
|
|
|
|
|
|
int width2, height2, nrChannels2;
|
|
unsigned int texture2;
|
|
glGenTextures(1, &texture2);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
|
|
|
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 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_set_flip_vertically_on_load(true);
|
|
unsigned char* data2 = stbi_load("Textures/awesomeface.png", &width2, &height2, &nrChannels2,0);
|
|
if(data2){
|
|
std::cout<< width2 << " x " << height2 << std::endl;
|
|
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
} else{
|
|
std::cout << "Failed to load texture!" << std::endl;
|
|
}
|
|
|
|
stbi_image_free(data2);
|
|
|
|
|
|
|
|
// WireFrame mode
|
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
|
|
|
|
shader.use();
|
|
shader.setInt("texture1",0);
|
|
shader.setInt("texture2",1);
|
|
|
|
glm::vec3 cubePositions[] = {
|
|
glm::vec3( 0.0f, 0.0f, 0.0f),
|
|
glm::vec3( 2.0f, 5.0f, -15.0f),
|
|
glm::vec3(-1.5f, -2.2f, -2.5f),
|
|
glm::vec3(-3.8f, -2.0f, -12.3f),
|
|
glm::vec3( 2.4f, -0.4f, -3.5f),
|
|
glm::vec3(-1.7f, 3.0f, -7.5f),
|
|
glm::vec3( 1.3f, -2.0f, -2.5f),
|
|
glm::vec3( 1.5f, 2.0f, -2.5f),
|
|
glm::vec3( 1.5f, 0.2f, -1.5f),
|
|
glm::vec3(-1.3f, 1.0f, -1.5f)
|
|
};
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
while(!glfwWindowShouldClose(window))
|
|
{
|
|
processInput(window);
|
|
|
|
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
|
|
|
|
|
// create transformations
|
|
glm::mat4 model = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
|
|
glm::mat4 view = glm::mat4(1.0f);
|
|
glm::mat4 projection = glm::mat4(1.0f);
|
|
model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f));
|
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
|
projection = glm::perspective(glm::radians(45.0f), (float)800 / (float)600, 0.1f, 100.0f);
|
|
|
|
|
|
// Pass a matrix to the shader
|
|
unsigned int ModelMatrixLoc = glGetUniformLocation(shader.ID, "model");
|
|
glUniformMatrix4fv(ModelMatrixLoc, 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
unsigned int ViewMatrixLoc = glGetUniformLocation(shader.ID, "view");
|
|
glUniformMatrix4fv(ViewMatrixLoc, 1, GL_FALSE, &view[0][0]);
|
|
|
|
unsigned int ProjectionMatrixLoc = glGetUniformLocation(shader.ID, "projection");
|
|
glUniformMatrix4fv(ProjectionMatrixLoc, 1, GL_FALSE, &projection[0][0]);
|
|
|
|
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
for(unsigned int i = 0; i < 10; i++){
|
|
glm::mat4 model = glm::mat4(1.0);
|
|
model = glm::translate(model, cubePositions[i]);
|
|
float angle = 20.0f * i;
|
|
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
|
shader.setMat4("model", model);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
}
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
glDeleteVertexArrays(1, &VAO);
|
|
glDeleteBuffers(1, &VBO);
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
|
|
} |