This commit is contained in:
parent
78ba333164
commit
78f53fdd54
28
headers/mesh.hpp
Normal file
28
headers/mesh.hpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef MESH_H
|
||||||
|
#define MESH_H
|
||||||
|
#include "camera.hpp"
|
||||||
|
#include "shader.hpp"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/quaternion.hpp>
|
||||||
|
|
||||||
|
class Mesh {
|
||||||
|
public:
|
||||||
|
Mesh(Shader *s, unsigned int VAO, int count);
|
||||||
|
~Mesh();
|
||||||
|
void draw(Camera *c);
|
||||||
|
void setPosition(glm::vec3 p);
|
||||||
|
void setScale(glm::vec3 s);
|
||||||
|
void setRotation(glm::quat r);
|
||||||
|
void setOwned(bool o);
|
||||||
|
Shader *getShader();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool buffersOwned;
|
||||||
|
int count;
|
||||||
|
unsigned int VAO;
|
||||||
|
Shader *shader;
|
||||||
|
glm::vec3 position; /*0,0,0*/
|
||||||
|
glm::vec3 scale; /*1,1,1*/
|
||||||
|
glm::quat rotation; /*1,0,0,0*/
|
||||||
|
};
|
||||||
|
#endif
|
||||||
29
src/main.cpp
29
src/main.cpp
@ -1,9 +1,10 @@
|
|||||||
#include <glad/glad.h> /*Must be before GLFW*/
|
|
||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
|
#include <glad/glad.h> /*Must be before GLFW*/
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include <mesh.hpp>
|
||||||
#include <shader.hpp>
|
#include <shader.hpp>
|
||||||
#include <shapes.h>
|
#include <shapes.h>
|
||||||
#include <texture.hpp>
|
#include <texture.hpp>
|
||||||
@ -56,6 +57,9 @@ int main(void) {
|
|||||||
Shader *shaderProgram = new Shader("./shaders/MVPTexNoRGBVert.glsl",
|
Shader *shaderProgram = new Shader("./shaders/MVPTexNoRGBVert.glsl",
|
||||||
"./shaders/TexNoRGBFrag.glsl");
|
"./shaders/TexNoRGBFrag.glsl");
|
||||||
|
|
||||||
|
Mesh *mesh = new Mesh(shaderProgram, VAO, 36);
|
||||||
|
mesh->setOwned(true);
|
||||||
|
|
||||||
Texture *texture = new Texture(GL_TEXTURE_2D, "./texture/container.jpg");
|
Texture *texture = new Texture(GL_TEXTURE_2D, "./texture/container.jpg");
|
||||||
Texture *texture2 = new Texture(GL_TEXTURE_2D, "./texture/brick.jpg");
|
Texture *texture2 = new Texture(GL_TEXTURE_2D, "./texture/brick.jpg");
|
||||||
|
|
||||||
@ -69,28 +73,21 @@ int main(void) {
|
|||||||
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->setMat4("view", cam->getView());
|
|
||||||
shaderProgram->setMat4("projection", cam->getProjection());
|
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
|
|
||||||
texture->bind(GL_TEXTURE0);
|
texture->bind(GL_TEXTURE0);
|
||||||
texture2->bind(GL_TEXTURE1);
|
texture2->bind(GL_TEXTURE1);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
for (unsigned int i = 0; i < 10; i++) {
|
for (unsigned int i = 0; i < 10; i++) {
|
||||||
shaderProgram->setInt("tex0", i%2);
|
mesh->setPosition(cubePositions[i]);
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
mesh->getShader()->setInt("tex0", i % 2);
|
||||||
model = glm::translate(model, cubePositions[i]);
|
|
||||||
|
|
||||||
|
glm::mat4 rotate = glm::mat4(1);
|
||||||
float angle = 20.0f * i;
|
float angle = 20.0f * i;
|
||||||
model =
|
rotate =
|
||||||
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
glm::rotate(rotate, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||||
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(55.0f),
|
rotate = glm::rotate(rotate, (float)glfwGetTime() * glm::radians(55.0f),
|
||||||
glm::vec3(1.0f, 0.5f, 0.0f));
|
glm::vec3(1.0f, 0.5f, 0.0f));
|
||||||
|
mesh->setRotation(glm::quat_cast(rotate));
|
||||||
shaderProgram->setMat4("model", model);
|
mesh->draw(cam);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|||||||
33
src/mesh.cpp
Normal file
33
src/mesh.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "camera.hpp"
|
||||||
|
#include "shader.hpp"
|
||||||
|
#include <mesh.hpp>
|
||||||
|
|
||||||
|
Mesh::Mesh(Shader *s, unsigned int VAO, int count)
|
||||||
|
: buffersOwned(false), count(count), VAO(VAO), shader(s),
|
||||||
|
position(glm::vec3(0)), scale(glm::vec3(1)),
|
||||||
|
rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {}
|
||||||
|
|
||||||
|
Mesh::~Mesh() {}
|
||||||
|
|
||||||
|
void Mesh::draw(Camera *c) {
|
||||||
|
glm::mat4 model = glm::mat4(1);
|
||||||
|
|
||||||
|
shader->use();
|
||||||
|
shader->setMat4("view", c->getView());
|
||||||
|
shader->setMat4("projection", c->getProjection());
|
||||||
|
model = glm::translate(model, position);
|
||||||
|
model = glm::scale(model, scale);
|
||||||
|
model = model * glm::mat4_cast(rotation);
|
||||||
|
shader->setMat4("model", model);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mesh::setPosition(glm::vec3 p) { position = p; }
|
||||||
|
|
||||||
|
void Mesh::setScale(glm::vec3 s) { scale = s; }
|
||||||
|
|
||||||
|
void Mesh::setRotation(glm::quat r) { rotation = r; }
|
||||||
|
|
||||||
|
void Mesh::setOwned(bool o) { buffersOwned = o; }
|
||||||
|
|
||||||
|
Shader *Mesh::getShader() { return shader; }
|
||||||
Loading…
x
Reference in New Issue
Block a user