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
|
||||
33
src/main.cpp
33
src/main.cpp
@ -1,9 +1,10 @@
|
||||
#include <glad/glad.h> /*Must be before GLFW*/
|
||||
#include "camera.hpp"
|
||||
#include <glad/glad.h> /*Must be before GLFW*/
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cassert>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <mesh.hpp>
|
||||
#include <shader.hpp>
|
||||
#include <shapes.h>
|
||||
#include <texture.hpp>
|
||||
@ -56,6 +57,9 @@ int main(void) {
|
||||
Shader *shaderProgram = new Shader("./shaders/MVPTexNoRGBVert.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 *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);
|
||||
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);
|
||||
texture2->bind(GL_TEXTURE1);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
for (unsigned int i = 0; i < 10; i++) {
|
||||
shaderProgram->setInt("tex0", i%2);
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, cubePositions[i]);
|
||||
mesh->setPosition(cubePositions[i]);
|
||||
mesh->getShader()->setInt("tex0", i % 2);
|
||||
|
||||
glm::mat4 rotate = glm::mat4(1);
|
||||
float angle = 20.0f * i;
|
||||
model =
|
||||
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(55.0f),
|
||||
glm::vec3(1.0f, 0.5f, 0.0f));
|
||||
|
||||
shaderProgram->setMat4("model", model);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
rotate =
|
||||
glm::rotate(rotate, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
rotate = glm::rotate(rotate, (float)glfwGetTime() * glm::radians(55.0f),
|
||||
glm::vec3(1.0f, 0.5f, 0.0f));
|
||||
mesh->setRotation(glm::quat_cast(rotate));
|
||||
mesh->draw(cam);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
@ -113,5 +110,5 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
||||
if (cam)
|
||||
cam->aspect = (float)width/height;
|
||||
cam->aspect = (float)width / height;
|
||||
}
|
||||
|
||||
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