Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <glew.h>
- #include <GLFW\glfw3.h>
- #include <glm\gtc\matrix_transform.hpp>
- #include <glm\gtc\type_ptr.hpp>
- #include <SOIL.h>
- #include "utils.h"
- #include "enums.h"
- #include <iostream>
- #include <vector>
- using namespace std;
- // 3D object
- class object
- {
- public:
- GLuint vbo;
- GLuint texture;
- GLuint prog; // shader program to use
- glm::vec3 position;
- float* vertices;
- int count; // number of vertices
- const char* texName; // texture file name
- glm::mat4 model; //
- glm::mat4 view; // matrices
- glm::mat4 proj; //
- GLint posAttrib; //
- GLint colAttrib; // attributes
- GLint texAttrib; //
- GLint normalAttrib; //
- GLint uniModel; //
- GLint uniView; // uniforms
- GLint uniProj; //
- bool useTexture;
- bool useNormals;
- int n;
- object() { }
- object(float* _vertices, int _count, int _dataPerVertex, const char* _texName, GLuint _prog, glm::vec3 _pos, glm::mat4 _view, glm::mat4 _proj, bool _useColor = true, bool _useNormals = true)
- {
- vertices = _vertices;
- prog = _prog;
- position = _pos;
- count = _count;
- texName = _texName;
- model = glm::mat4(1.0f) * glm::translate(model, position);
- view = _view;
- proj = _proj;
- useTexture = true;
- useNormals = _useNormals;
- n = _dataPerVertex;
- }
- object(float* _vertices, int _count, int _dataPerVertex, GLuint _prog, glm::vec3 _pos, glm::mat4 _view, glm::mat4 _proj, bool _useColor = true, bool _useNormals = true)
- {
- vertices = _vertices;
- prog = _prog;
- position = _pos;
- count = _count;
- model = glm::mat4(1.0f) * glm::translate(model, position);
- view = _view;
- proj = _proj;
- useTexture = false;
- useNormals = _useNormals;
- n = _dataPerVertex;
- }
- void init()
- {
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * count * n, vertices, GL_STATIC_DRAW);
- glUseProgram(prog);
- posAttrib = glGetAttribLocation(prog, "pos"); // position
- glEnableVertexAttribArray(posAttrib);
- glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), nullptr);
- colAttrib = glGetAttribLocation(prog, "vColor"); // color
- glEnableVertexAttribArray(colAttrib);
- glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (3 * sizeof(float)));
- uniModel = glGetUniformLocation(prog, "model");
- uniView = glGetUniformLocation(prog, "view");
- uniProj = glGetUniformLocation(prog, "proj");
- if (useTexture)
- {
- texAttrib = glGetAttribLocation(prog, "tex_coord"); // tex coords
- glEnableVertexAttribArray(texAttrib);
- glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
- int w, h;
- unsigned char* texData = SOIL_load_image(texName, &w, &h, nullptr, SOIL_LOAD_RGB);
- if (texData == nullptr)
- {
- cout << "The texture is nullptr" << endl;
- system("pause");
- }
- else
- {
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);
- SOIL_free_image_data(texData);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glGenerateMipmap(GL_TEXTURE_2D);
- }
- }
- if (useNormals)
- {
- normalAttrib = glGetAttribLocation(prog, "normal");
- glEnableVertexAttribArray(normalAttrib);
- if (useTexture)
- {
- glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (8 * sizeof(float)));
- }
- else
- {
- glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
- }
- }
- glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
- glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
- glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
- }
- void update()
- {
- glUseProgram(prog);
- model = glm::mat4(1.0f);
- model = glm::translate(model, position);
- glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
- glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
- glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
- }
- void draw()
- {
- // binding stuff before drawing (in case more objects are drawn)
- glUseProgram(prog);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- if (useTexture)
- glBindTexture(GL_TEXTURE_2D, texture);
- glEnableVertexAttribArray(posAttrib);
- glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), nullptr);
- glEnableVertexAttribArray(colAttrib);
- glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (3 * sizeof(float)));
- if (useTexture)
- {
- glEnableVertexAttribArray(texAttrib);
- glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
- }
- if (useNormals)
- {
- glEnableVertexAttribArray(normalAttrib);
- if (useTexture)
- {
- glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (8 * sizeof(float)));
- }
- else
- {
- glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
- }
- }
- glDrawArrays(GL_TRIANGLES, 0, count);
- }
- void drawWithOutline(GLuint outlineProg)
- {
- glClear(GL_STENCIL_BUFFER_BIT);
- glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
- glStencilFunc(GL_ALWAYS, 1, 0xFF);
- glStencilMask(0xFF);
- glUseProgram(prog);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- if (useTexture)
- glBindTexture(GL_TEXTURE_2D, texture);
- glEnableVertexAttribArray(posAttrib);
- glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), nullptr);
- glEnableVertexAttribArray(colAttrib);
- glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (3 * sizeof(float)));
- if (useTexture)
- {
- glEnableVertexAttribArray(texAttrib);
- glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
- }
- if (useNormals)
- {
- glEnableVertexAttribArray(normalAttrib);
- if (useTexture)
- {
- glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (8 * sizeof(float)));
- }
- else
- {
- glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
- }
- }
- glDrawArrays(GL_TRIANGLES, 0, count);
- glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
- glStencilMask(0x00);
- glDisable(GL_DEPTH_TEST);
- glUseProgram(outlineProg);
- GLint pos2 = glGetAttribLocation(outlineProg, "pos");
- glEnableVertexAttribArray(pos2);
- glVertexAttribPointer(pos2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), nullptr);
- glm::mat4 m = glm::mat4(1.0f);
- m = glm::scale(m, glm::vec3(1.1f, 1.1f, 1.1f));
- GLint uniModel2 = glGetUniformLocation(outlineProg, "model");
- glUniformMatrix4fv(uniModel2, 1, GL_FALSE, glm::value_ptr(m));
- GLint uniView2 = glGetUniformLocation(outlineProg, "view");
- glUniformMatrix4fv(uniView2, 1, GL_FALSE, glm::value_ptr(view));
- GLint uniProj2 = glGetUniformLocation(outlineProg, "proj");
- glUniformMatrix4fv(uniProj2, 1, GL_FALSE, glm::value_ptr(proj));
- glDrawArrays(GL_TRIANGLES, 0, count);
- glStencilMask(0xFF);
- glEnable(GL_DEPTH_TEST);
- }
- ~object()
- {
- glDeleteBuffers(1, &vbo);
- if (useTexture)
- {
- glDeleteTextures(1, &texture);
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement