code cleanup
This commit is contained in:
parent
8f7ca3ae38
commit
8885b40709
@ -1,5 +1,9 @@
|
|||||||
float tri_vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f,
|
float tri_vertices[] = {
|
||||||
0.0f, 0.0f, 0.5f, 0.0f};
|
-0.5f, -0.5f, 0.0f, /**/
|
||||||
|
0.5f, -0.5f, 0.0f, /**/
|
||||||
|
0.0f, 0.5f, 0.0f /**/
|
||||||
|
};
|
||||||
|
|
||||||
float tri_rgb_vertices[] = {
|
float tri_rgb_vertices[] = {
|
||||||
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, /**/
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, /**/
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, /**/
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, /**/
|
||||||
@ -27,48 +31,48 @@ float sqr_tex_vertices[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
float cube_vertices[] = {
|
float cube_vertices[] = {
|
||||||
/*positions*texture cords*/
|
/*positions*texture cords*/
|
||||||
-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, 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, 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, 1.0f, /**/
|
||||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, /**/
|
||||||
|
|
||||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|
||||||
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, 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, 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, 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, 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, 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, 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, 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, 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, 0.0f, /**/
|
||||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f /**/
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *basicVertexShaderSource =
|
const char *basicVertexShaderSource =
|
||||||
|
|||||||
119
src/main.cpp
119
src/main.cpp
@ -1,10 +1,9 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h> /*Must be before GLFW*/
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <cassert>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
#include <cassert>
|
|
||||||
#include <iostream>
|
|
||||||
#include <shader.hpp>
|
#include <shader.hpp>
|
||||||
#include <shapes.h>
|
#include <shapes.h>
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
@ -15,125 +14,109 @@ void processInput(GLFWwindow *window);
|
|||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||||
|
|
||||||
glm::vec3 cubePositions[] = {
|
glm::vec3 cubePositions[] = {
|
||||||
glm::vec3( 0.0f, 0.0f, 0.0f),
|
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.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(-1.5f, -2.2f, -2.5f),
|
glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f),
|
||||||
glm::vec3( 2.4f, -0.4f, -3.5f),
|
glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.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)
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int tmp = 0;
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
assert(window != NULL && "Window Failed");
|
assert(window && "Window Failed");
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
tmp = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
int is_ok = 0;
|
||||||
assert(tmp && "Failed to init glad");
|
is_ok = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||||
|
assert(is_ok && "Failed to init glad");
|
||||||
|
|
||||||
glViewport(0, 0, 800, 600);
|
glViewport(0, 0, 800, 600);
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
|
||||||
unsigned int VBO, VAO, EBO;
|
unsigned int VBO, VAO;
|
||||||
glGenVertexArrays(1, &VAO);
|
glGenVertexArrays(1, &VAO);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
glGenBuffers(1, &VBO);
|
glGenBuffers(1, &VBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices,
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices),cube_vertices,
|
|
||||||
GL_STATIC_DRAW);
|
GL_STATIC_DRAW);
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * 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, 5 * sizeof(float), (void *)0);
|
||||||
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
|
||||||
(void *)0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
||||||
(void *)(3 * sizeof(float)));
|
(void *)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
|
|
||||||
// glGenBuffers(1, &EBO);
|
glEnableVertexAttribArray(0); /*pos*/
|
||||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
glEnableVertexAttribArray(1); /*rgb*/
|
||||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices,
|
glEnableVertexAttribArray(2); /*tex_cord*/
|
||||||
// GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
Shader *shaderProgram =
|
|
||||||
new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl");
|
|
||||||
|
|
||||||
|
Shader *shaderProgram;
|
||||||
unsigned int texture;
|
unsigned int texture;
|
||||||
|
shaderProgram =
|
||||||
|
new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl");
|
||||||
glGenTextures(1, &texture);
|
glGenTextures(1, &texture);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
|
||||||
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_WRAP_T, GL_REPEAT);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||||
GL_LINEAR_MIPMAP_LINEAR);
|
GL_LINEAR_MIPMAP_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
|
|
||||||
stbi_set_flip_vertically_on_load(true);
|
|
||||||
int width, height, nrChannels;
|
int width, height, nrChannels;
|
||||||
unsigned char *data =
|
unsigned char *data;
|
||||||
stbi_load("./texture/container.jpg", &width, &height, &nrChannels, 0);
|
stbi_set_flip_vertically_on_load(true);
|
||||||
if (data) {
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
data = stbi_load("./texture/container.jpg", &width, &height, &nrChannels, 0);
|
||||||
GL_UNSIGNED_BYTE, data);
|
assert(data && "Failed to load image");
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
} else {
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||||
std::cout << "Failed to load texture" << std::endl;
|
GL_UNSIGNED_BYTE, data);
|
||||||
}
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
glm::mat4 view = glm::mat4(1.0f); /*move camera*/
|
glm::mat4 view = glm::mat4(1.0f); /*move camera*/
|
||||||
|
glm::mat4 projection = glm::perspective(
|
||||||
|
glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); /*camera settings*/
|
||||||
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
||||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); /*camera settings*/
|
|
||||||
|
|
||||||
processInput(window);
|
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
shaderProgram->use();
|
shaderProgram->use();
|
||||||
shaderProgram->setInt("Tex0",0);
|
shaderProgram->setInt("Tex0", 0);
|
||||||
shaderProgram->setMat4("view",view);
|
shaderProgram->setMat4("view", view);
|
||||||
shaderProgram->setMat4("projection",projection);
|
shaderProgram->setMat4("projection", projection);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
for(unsigned int i = 0; i < 10; i++)
|
for (unsigned int i = 0; i < 10; i++) {
|
||||||
{
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
model = glm::translate(model, cubePositions[i]);
|
||||||
model = glm::translate(model, cubePositions[i]);
|
|
||||||
float angle = 20.0f * i;
|
float angle = 20.0f * i;
|
||||||
model = glm::rotate(model, glm::radians(angle),
|
model =
|
||||||
glm::vec3(1.0f, 0.3f, 0.5f));
|
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||||
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(55.0f),
|
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(55.0f),
|
||||||
glm::vec3(1.0f, 0.5f, 0.0f));
|
glm::vec3(1.0f, 0.5f, 0.0f));
|
||||||
shaderProgram->setMat4("model", model);
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
shaderProgram->setMat4("model", model);
|
||||||
}
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
}
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
||||||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
processInput(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete (shaderProgram);
|
delete (shaderProgram);
|
||||||
|
|||||||
@ -46,8 +46,8 @@ void Shader::setFloat(const std::string &name, float value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setMat4(const std::string &name, glm::mat4 value) {
|
void Shader::setMat4(const std::string &name, glm::mat4 value) {
|
||||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
|
||||||
|
glm::value_ptr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*priv*/
|
/*priv*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user