Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // GLEW
- //#define GLEW_STATIC
- #include <GL/glew.h>
- // GLFW
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <stdio.h>
- #include <math.h>
- #include <algorithm>
- #include "glm/ext.hpp"
- # define M_PI 3.14159265358979323846
- # define OPENGL_MODE 0
- # define CLOSE2GL_MODE 1
- void FramebufferSizeCallback(GLFWwindow* window, int width, int height);
- // Classe utilizada para representar um vetor de quatro posições (coordenadas homogêneas)
- class Vect4 {
- public:
- float x, y, z, w;
- Vect4(float x1, float y1, float z1, float w1) { x = x1; y = y1; z = z1; w = w1; }
- Vect4() { x = 0.0f; y = 0.0f; z = 0.0f; w = 0.0f; }
- float length() { return sqrt(pow(x, 2.0) + pow(y, 2.0) + pow(z, 2.0)); }
- Vect4 operator+(const Vect4& b) const {
- return Vect4(this->x + b.x, this->y + b.y, this->z + b.z, this->w + b.w);
- }
- Vect4 operator-() const {
- return Vect4(-this->x, -this->y, -this->z, -this->w);
- }
- Vect4 operator-(const Vect4& b) const {
- return Vect4(this->x - b.x, this->y - b.y, this->z - b.z, this->w - b.w);
- }
- Vect4 operator*(const float& n) const {
- return Vect4(this->x * n, this->y * n, this->z * n, this->w * n);
- }
- Vect4 operator/(const float& n) const {
- return Vect4(this->x / n, this->y / n, this->z / n, this->w / n);
- }
- void print() const {
- printf("\n%f %f %f %f\n", x, y, z, w);
- }
- };
- inline Vect4 operator* (int n, const Vect4& v) {
- return v*n;
- }
- Vect4 normalize(Vect4 v) {
- return v / (v.length());
- }
- float dot(Vect4 a, Vect4 b) {
- return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
- }
- Vect4 cross(Vect4 a, Vect4 b) {
- return Vect4((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x), 0.0f);
- }
- // Classe utilizada para representar uma matriz 4x4 (coordenadas homogêneas)
- class Matr4 {
- public:
- Vect4 m[4];
- Matr4(float x0, float y0, float z0, float w0,
- float x1, float y1, float z1, float w1,
- float x2, float y2, float z2, float w2,
- float x3, float y3, float z3, float w3) {
- m[0] = Vect4(x0, y0, z0, w0);
- m[1] = Vect4(x1, y1, z1, w1);
- m[2] = Vect4(x2, y2, z2, w2);
- m[3] = Vect4(x3, y3, z3, w3);
- }
- Matr4() {
- m[0] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
- m[1] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
- m[2] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
- m[3] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
- }
- Matr4 transpose() const {
- return Matr4(m[0].x, m[1].x, m[2].x, m[3].x,
- m[0].y, m[1].y, m[2].y, m[3].y,
- m[0].z, m[1].z, m[2].z, m[3].z,
- m[0].w, m[1].w, m[2].w, m[3].w);
- }
- Matr4 inverse() const {
- float inv[16];
- inv[0] = m[1].y * m[2].z * m[3].w -
- m[1].y * m[2].w * m[3].z -
- m[2].y * m[1].z * m[3].w +
- m[2].y * m[1].w * m[3].z +
- m[3].y * m[1].z * m[2].w -
- m[3].y * m[1].w * m[2].z;
- inv[4] = -m[1].x * m[2].z * m[3].w +
- m[1].x * m[2].w * m[3].z +
- m[2].x * m[1].z * m[3].w -
- m[2].x * m[1].w * m[3].z -
- m[3].x * m[1].z * m[2].w +
- m[3].x * m[1].w * m[2].z;
- inv[8] = m[1].x * m[2].y * m[3].w -
- m[1].x * m[2].w * m[3].y -
- m[2].x * m[1].y * m[3].w +
- m[2].x * m[1].w * m[3].y +
- m[3].x * m[1].y * m[2].w -
- m[3].x * m[1].w * m[2].y;
- inv[12] = -m[1].x * m[2].y * m[3].z +
- m[1].x * m[2].z * m[3].y +
- m[2].x * m[1].y * m[3].z -
- m[2].x * m[1].z * m[3].y -
- m[3].x * m[1].y * m[2].z +
- m[3].x * m[1].z * m[2].y;
- inv[1] = -m[0].y * m[2].z * m[3].w +
- m[0].y * m[2].w * m[3].z +
- m[2].y * m[0].z * m[3].w -
- m[2].y * m[0].w * m[3].z -
- m[3].y * m[0].z * m[2].w +
- m[3].y * m[0].w * m[2].z;
- inv[5] = m[0].x * m[2].z * m[3].w -
- m[0].x * m[2].w * m[3].z -
- m[2].x * m[0].z * m[3].w +
- m[2].x * m[0].w * m[3].z +
- m[3].x * m[0].z * m[2].w -
- m[3].x * m[0].w * m[2].z;
- inv[9] = -m[0].x * m[2].y * m[3].w +
- m[0].x * m[2].w * m[3].y +
- m[2].x * m[0].y * m[3].w -
- m[2].x * m[0].w * m[3].y -
- m[3].x * m[0].y * m[2].w +
- m[3].x * m[0].w * m[2].y;
- inv[13] = m[0].x * m[2].y * m[3].z -
- m[0].x * m[2].z * m[3].y -
- m[2].x * m[0].y * m[3].z +
- m[2].x * m[0].z * m[3].y +
- m[3].x * m[0].y * m[2].z -
- m[3].x * m[0].z * m[2].y;
- inv[2] = m[0].y * m[1].z * m[3].w -
- m[0].y * m[1].w * m[3].z -
- m[1].y * m[0].z * m[3].w +
- m[1].y * m[0].w * m[3].z +
- m[3].y * m[0].z * m[1].w -
- m[3].y * m[0].w * m[1].z;
- inv[6] = -m[0].x * m[1].z * m[3].w +
- m[0].x * m[1].w * m[3].z +
- m[1].x * m[0].z * m[3].w -
- m[1].x * m[0].w * m[3].z -
- m[3].x * m[0].z * m[1].w +
- m[3].x * m[0].w * m[1].z;
- inv[10] = m[0].x * m[1].y * m[3].w -
- m[0].x * m[1].w * m[3].y -
- m[1].x * m[0].y * m[3].w +
- m[1].x * m[0].w * m[3].y +
- m[3].x * m[0].y * m[1].w -
- m[3].x * m[0].w * m[1].y;
- inv[14] = -m[0].x * m[1].y * m[3].z +
- m[0].x * m[1].z * m[3].y +
- m[1].x * m[0].y * m[3].z -
- m[1].x * m[0].z * m[3].y -
- m[3].x * m[0].y * m[1].z +
- m[3].x * m[0].z * m[1].y;
- inv[3] = -m[0].y * m[1].z * m[2].w +
- m[0].y * m[1].w * m[2].z +
- m[1].y * m[0].z * m[2].w -
- m[1].y * m[0].w * m[2].z -
- m[2].y * m[0].z * m[1].w +
- m[2].y * m[0].w * m[1].z;
- inv[7] = m[0].x * m[1].z * m[2].w -
- m[0].x * m[1].w * m[2].z -
- m[1].x * m[0].z * m[2].w +
- m[1].x * m[0].w * m[2].z +
- m[2].x * m[0].z * m[1].w -
- m[2].x * m[0].w * m[1].z;
- inv[11] = -m[0].x * m[1].y * m[2].w +
- m[0].x * m[1].w * m[2].y +
- m[1].x * m[0].y * m[2].w -
- m[1].x * m[0].w * m[2].y -
- m[2].x * m[0].y * m[1].w +
- m[2].x * m[0].w * m[1].y;
- inv[15] = m[0].x * m[1].y * m[2].z -
- m[0].x * m[1].z * m[2].y -
- m[1].x * m[0].y * m[2].z +
- m[1].x * m[0].z * m[2].y +
- m[2].x * m[0].y * m[1].z -
- m[2].x * m[0].z * m[1].y;
- float det = m[0].x * inv[0] + m[0].y * inv[4] + m[0].z * inv[8] + m[0].w * inv[12];
- if (det == 0)
- return Matr4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
- det = 1.0 / det;
- return Matr4(inv[0]*det, inv[1]*det, inv[2]*det, inv[3]*det,
- inv[4]*det, inv[5]*det, inv[6]*det, inv[7]*det,
- inv[8]*det, inv[9]*det, inv[10]*det, inv[11]*det,
- inv[12]*det, inv[13]*det, inv[14]*det, inv[15]*det);
- }
- void print() const {
- printf("\n%f %f %f %f\n", m[0].x, m[0].y, m[0].z, m[0].w);
- printf("%f %f %f %f\n", m[1].x, m[1].y, m[1].z, m[1].w);
- printf("%f %f %f %f\n", m[2].x, m[2].y, m[2].z, m[2].w);
- printf("%f %f %f %f\n\n", m[3].x, m[3].y, m[3].z, m[3].w);
- }
- Vect4 operator*(const Vect4& v) const {
- return Vect4(
- (m[0].x * v.x) + (m[0].y * v.y) + (m[0].z * v.z) + (m[0].w * v.w),
- (m[1].x * v.x) + (m[1].y * v.y) + (m[1].z * v.z) + (m[1].w * v.w),
- (m[2].x * v.x) + (m[2].y * v.y) + (m[2].z * v.z) + (m[2].w * v.w),
- (m[3].x * v.x) + (m[3].y * v.y) + (m[3].z * v.z) + (m[3].w * v.w)
- );
- }
- Matr4 operator-() const {
- return Matr4(
- (-m[0].x), (-m[0].y), (-m[0].z), (-m[0].w),
- (-m[1].x), (-m[1].y), (-m[1].z), (-m[1].w),
- (-m[2].x), (-m[2].y), (-m[2].z), (-m[2].w),
- (-m[3].x), (-m[3].y), (-m[3].z), (-m[3].w)
- );
- }
- Matr4 operator*(const Matr4& mat) const {
- Matr4 b = mat.transpose();
- return Matr4(
- dot(this->m[0], b.m[0]), dot(this->m[0], b.m[1]), dot(this->m[0], b.m[2]), dot(this->m[0], b.m[3]),
- dot(this->m[1], b.m[0]), dot(this->m[1], b.m[1]), dot(this->m[1], b.m[2]), dot(this->m[1], b.m[3]),
- dot(this->m[2], b.m[0]), dot(this->m[2], b.m[1]), dot(this->m[2], b.m[2]), dot(this->m[2], b.m[3]),
- dot(this->m[3], b.m[0]), dot(this->m[3], b.m[1]), dot(this->m[3], b.m[2]), dot(this->m[3], b.m[3])
- );
- }
- glm::mat4 convertToGLM() const {
- return glm::mat4(m[0].x, m[1].x, m[2].x, m[3].x,
- m[0].y, m[1].y, m[2].y, m[3].y,
- m[0].z, m[1].z, m[2].z, m[3].z,
- m[0].w, m[1].w, m[2].w, m[3].w
- );
- }
- };
- Matr4 identity() {
- return Matr4(1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- Matr4 lookat(Vect4 eye, Vect4 lookpoint, Vect4 up_vector) {
- Vect4 n = normalize(eye - lookpoint);
- Vect4 u = normalize(cross(up_vector, n));
- Vect4 v = normalize(cross(n, u));
- return Matr4(u.x, u.y, u.z, dot(-eye, u),
- v.x, v.y, v.z, dot(-eye, v),
- n.x, n.y, n.z, dot(-eye, n),
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- }
- Matr4 perspective(float fov, float aspectratio, float n, float f) {
- float t = fabs(n) * tan(fov / 2);
- float b = -t;
- float r = t * aspectratio;
- float l = -r;
- Matr4 P((2.0*n) / (r - l), 0.0f, (r + l) / (r - l), 0.0f,
- 0.0f, (2.0*n) / (t - b), (t + b) / (t - b), 0.0f,
- 0.0f, 0.0f, -(f + n) / (f - n), -(2.0*f*n) / (f - n),
- 0.0f, 0.0f, -1.0f, 0.0f
- );
- return (P);
- }
- Matr4 viewport(float width, float height, float dw, float dh) {
- Matr4 V(width/2.0f, 0.0f, 0.0f, dw + width/2.0f,
- 0.0f,-height/2.0f, 0.0f, dh + height/2.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- return V;
- }
- void processInput(GLFWwindow *window);
- void mouse_callback(GLFWwindow* window, double xpos, double ypos);
- glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 10.0f);
- glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
- glm::vec3 cameraDefaultPos = glm::vec3(0.0f, 0.0f, 10.0f);
- glm::vec3 cameraDefaultFront = glm::vec3(0.0f, 0.0f, -1.0f);
- glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
- glm::vec3 objCenter = glm::vec3(0.0f, 0.0f, 0.0f);
- // timing
- float deltaTime = 0.0f; // time between current frame and last frame
- float lastFrame = 0.0f;
- float rotationx = 0.0f;
- float rotationy = 0.0f;
- float lastX = 320.0f;
- float lastY = 240.0f;
- float pitch = 0.0f;
- float yaw = -90.0f;
- int cw = 0;
- int cull = 1;
- int wireframe = 0;
- int cameraType = 0;
- GLint shadingmethod = 0;
- int renderingmethod = OPENGL_MODE;
- glm::vec4 colorm = glm::vec4(0.5f, 0.5f, 0.5f, 1.0f);
- GLfloat nearplane = 1.0f;
- GLfloat farplane = 10000.0f;
- float calcz = 0.0f;
- float calcdz = 0.0f;
- //float cullSum = 0.0f; //i`m so sorry
- int mouseclicked = 0;
- float aspectRatio = 640.0f / 480.0f;
- int w_width = 640;
- int w_height = 480;
- glm::vec4 light_dir = glm::vec4(2.0f, 2.0f, 2.0f, 1.0f);
- // Função utilizada para calcular em que ponto a câmera precisa ficar para enquadrar o modelo. Recebe os vértices do modelo (points) e número de triângulos (NumTris).
- void calcCameraPos(GLfloat *points, int NumTris) {
- int maxxset = 0;
- int minxset = 0;
- int maxyset = 0;
- int minyset = 0;
- int maxzset = 0;
- int minzset = 0;
- float minx = 0.0f;
- float maxx = 0.0f;
- float miny = 0.0f;
- float maxy = 0.0f;
- float minz = 0.0f;
- float maxz = 0.0f;
- int u = 0;
- while (u < NumTris * 3 * 3) {
- if (u % 3 == 0) {
- if (points[u] < minx || minxset == 0) {
- minxset = 1;
- minx = points[u];
- //printf("minx %d\n", u);
- }
- if (points[u] > maxx || maxxset == 0) {
- maxxset = 1;
- maxx = points[u];
- //printf("maxx %d\n", u);
- }
- }
- if (u % 3 == 1) {
- if (points[u] < miny || minyset == 0) {
- minyset = 1;
- miny = points[u];
- //printf("miny %d\n", u);
- }
- if (points[u] > maxy || maxyset == 0) {
- maxyset = 1;
- maxy = points[u];
- //printf("maxy %d\n", u);
- }
- }
- if (u % 3 == 2) {
- if (points[u] < minz || minzset == 0) {
- minzset = 1;
- minz = points[u];
- //printf("minz %d\n", u);
- }
- if (points[u] > maxz || maxzset == 0) {
- maxzset = 1;
- maxz = points[u];
- // printf("maxz %d\n", u);
- }
- }
- u++;
- }
- objCenter = glm::vec3(((maxx - minx) / 2.0f) + minx, ((maxy - miny) / 2.0f) + miny, ((maxz - minz) / 2.0f) + minz);
- float camtanz;
- if ((maxx - minx / 2.0f) >= (maxy - miny / 2.0f))
- camtanz = glm::tan(glm::radians(30.0f))*(maxx - minx / 2.0f)*1.5f;
- else
- camtanz = glm::tan(glm::radians(30.0f))*(maxy - miny / 2.0f)*1.5f;
- cameraPos = glm::vec3(((maxx - minx) / 2.0f) + minx, (maxy - miny) / 2.0f + miny, camtanz + 0.5f);
- cameraDefaultPos = cameraPos;
- calcz = camtanz + 0.5f;
- calcdz = calcz;
- }
- // Função que inicializa o Vertex Shader da parte OpenGL.
- GLuint initVSShaderOGL() {
- const char* vertex_shader =
- "#version 460\n"
- "in vec3 position;"
- "in vec3 pnormal;"
- "in int colorindex;"
- "uniform mat4 ModelView;"
- "uniform mat4 ModelViewIT;"
- "uniform mat4 Proj;"
- "uniform vec4 colorm;"
- "uniform vec4 light_pos;"
- "uniform vec3 camera_position;"
- "uniform int shading_method;"
- "out vec4 color0;"
- "out vec4 mod_position;"
- "out vec4 mod_normal;"
- "void main() {"
- " vec4 view_position = ModelView * vec4(position, 1.0f);"
- " vec4 l = normalize(light_pos - view_position);"
- " color0 = colorm;"
- " mod_position = vec4(position, 1.0f);"
- " mod_normal = normalize(ModelViewIT * vec4(pnormal, 0.0f));"
- " vec4 v = normalize(- view_position);"
- " vec4 h = normalize(v + l);"
- " if (shading_method == 0)"
- " {color0 = vec4(0.1f, 0.1f, 0.1f, 1.0f) + 0.3f * colorm * max(0.0f, dot(mod_normal, l));}"
- " if (shading_method == 1)"
- " {color0 = vec4(0.1f, 0.1f, 0.1f, 1.0f) + 0.3f * colorm * (max(0.0f, dot(mod_normal, l)) + vec4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6 * max(0.0f, pow((dot(h, mod_normal)), 10)));}"
- " mod_normal = vec4(pnormal, 0.0f);"
- " gl_Position = Proj * view_position;"
- "}";
- GLuint vs = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vs, 1, &vertex_shader, NULL);
- glCompileShader(vs);
- GLint isCompiled = 0;
- glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
- if (isCompiled == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
- // The maxLength includes the NULL character
- char *errorLog = (char *) malloc(sizeof(char) * maxLength);
- glGetShaderInfoLog(vs, maxLength, &maxLength, errorLog);
- printf("%s", errorLog);
- // Provide the infolog in whatever manor you deem best.
- // Exit with failure.
- glDeleteShader(vs); // Don't leak the shader.
- exit(1);
- }
- return vs;
- }
- // Função que inicializa o Fragment Shader da parte OpenGL.
- GLuint initFSShaderOGL() {
- const char* fragment_shader =
- "#version 460\n"
- "in vec4 color0;"
- "in vec4 mod_position;"
- "in vec4 mod_normal;"
- "uniform vec4 colorm;"
- "uniform mat4 ModelView;"
- "uniform mat4 Proj;"
- "uniform int shading_method;"
- "uniform vec4 light_pos;"
- "uniform vec3 camera_position;"
- "out vec4 frag_color;"
- "void main() {"
- // " frag_color = vec4(0.5f, 0.0f, 0.5f, 1.0f);"
- " frag_color = color0;"
- " vec4 v = mod_position - vec4(camera_position, 1.0f);"
- " vec4 l = light_pos - (ModelView * mod_position);"
- " vec4 h = normalize (v + l) ;"
- " frag_color = color0;"
- " if (shading_method == 2)"
- // " frag_color = vec4(0.1f, 0.1f, 0.1f, 1.0f) + colorm * ( 0.3 *max(0.0f, dot(normalize(mod_normal), normalize(l))) + 0.6 * max(0.0f, pow((dot(h, normalize(mod_normal))), 10)));"
- " frag_color = vec4(1.0f, 0.25f, 0.4f, 1.0f);"
- "}";
- GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fs, 1, &fragment_shader, NULL);
- glCompileShader(fs);
- GLint isCompiled = 0;
- glGetShaderiv(fs, GL_COMPILE_STATUS, &isCompiled);
- if (isCompiled == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
- // The maxLength includes the NULL character
- char *errorLog = (char *)malloc(sizeof(char) * maxLength);
- glGetShaderInfoLog(fs, maxLength, &maxLength, errorLog);
- printf("%s", errorLog);
- // Provide the infolog in whatever manor you deem best.
- // Exit with failure.
- glDeleteShader(fs); // Don't leak the shader.
- exit(1);
- }
- return fs;
- }
- // Função que inicializa o Vertex Shader da parte Close2GL.
- GLuint initVSShaderC2GL() {
- const char* vertex_shader =
- "#version 460\n"
- "in vec3 position;"
- "in vec2 texcoord;"
- "uniform vec4 colorm;"
- "uniform sampler2D tex;"
- "out vec4 color0;"
- "out vec2 tex_coord;"
- "void main() {"
- " gl_Position = vec4(position, 1.0f);"
- " color0 = colorm;"
- " tex_coord = texcoord;"
- "}";
- GLuint vs = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vs, 1, &vertex_shader, NULL);
- glCompileShader(vs);
- GLint isCompiled = 0;
- glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
- if (isCompiled == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
- // The maxLength includes the NULL character
- char *errorLog = (char *)malloc(sizeof(char) * maxLength);
- glGetShaderInfoLog(vs, maxLength, &maxLength, errorLog);
- printf("%s", errorLog);
- // Provide the infolog in whatever manor you deem best.
- // Exit with failure.
- glDeleteShader(vs); // Don't leak the shader.
- exit(1);
- }
- return vs;
- }
- // Função que inicializa o Fragment Shader da parte Close2GL.
- GLuint initFSShaderC2GL() {
- const char* fragment_shader =
- "#version 460\n"
- "in vec2 tex_coord;"
- "uniform vec4 colorm;"
- "uniform sampler2D tex;"
- "out vec4 frag_color;"
- "void main() {"
- //" frag_color = colorm;"
- " frag_color = texture(tex, tex_coord);"
- "}";
- GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fs, 1, &fragment_shader, NULL);
- glCompileShader(fs);
- GLint isCompiled = 0;
- glGetShaderiv(fs, GL_COMPILE_STATUS, &isCompiled);
- if (isCompiled == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
- // The maxLength includes the NULL character
- char *errorLog = (char *)malloc(sizeof(char) * maxLength);
- glGetShaderInfoLog(fs, maxLength, &maxLength, errorLog);
- printf("%s", errorLog);
- // Provide the infolog in whatever manor you deem best.
- // Exit with failure.
- glDeleteShader(fs); // Don't leak the shader.
- exit(1);
- }
- return fs;
- }
- int main(int argc, char *argv[]) {
- // start GL context and O/S window using the GLFW helper library
- //if (argc <= 1)
- // exit(1)
- FILE *fp3;
- fp3 = fopen("show.txt", "w+");
- fwrite("oi", 1, 2, fp3);
- fclose(fp3);
- if (!glfwInit()) {
- fprintf(stderr, "ERROR: could not start GLFW3\n");
- return 1;
- }
- GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
- if (!window) {
- fprintf(stderr, "ERROR: could not open window with GLFW3\n");
- glfwTerminate();
- return 1;
- }
- glfwMakeContextCurrent(window);
- // start GLEW extension handler
- glewExperimental = GL_TRUE;
- glewInit();
- // get version info
- const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
- const GLubyte* version = glGetString(GL_VERSION); // version as a string
- printf("Renderer: %s\n", renderer);
- printf("OpenGL version supported %s\n", version);
- // tell GL to only draw onto a pixel if the shape is closer to the viewer
- glEnable(GL_DEPTH_TEST); // enable depth-testing
- glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
- glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
- glfwSetCursorPosCallback(window, mouse_callback);
- // inicializa os shaders
- GLuint vs = initVSShaderOGL();
- GLuint fs = initFSShaderOGL();
- GLuint c2vs = initVSShaderC2GL();
- GLuint c2fs = initFSShaderC2GL();
- GLuint shader_program = glCreateProgram();
- glAttachShader(shader_program, fs);
- glAttachShader(shader_program, vs);
- glBindAttribLocation(shader_program, 0, "position");
- glBindAttribLocation(shader_program, 1, "pnormal");
- glBindAttribLocation(shader_program, 2, "colorindex");
- glLinkProgram(shader_program);
- glValidateProgram(shader_program);
- GLuint c2gl_shader_program = glCreateProgram();
- glAttachShader(c2gl_shader_program, c2fs);
- glAttachShader(c2gl_shader_program, c2vs);
- glBindAttribLocation(c2gl_shader_program, 0, "position");
- glBindAttribLocation(c2gl_shader_program, 1, "texcoord");
- glLinkProgram(c2gl_shader_program);
- glValidateProgram(c2gl_shader_program);
- // parsing do arquivo
- char ch;
- int NumTris;
- int material_count;
- FILE *fp;
- if (argc <= 1)
- fp = fopen("cow_up.in", "r");
- else
- {
- fp = fopen(argv[1], "r"); if (argc >= 3 && argv[2][0] == 't') cw = 1;
- }
- if (fp == NULL) { printf("ERROR: unable to open TriObj !\n"); exit(1); }
- fscanf(fp, "%c", &ch);
- while (ch != '\n') // skip the first line – object’s name
- fscanf(fp, "%c", &ch);
- fscanf(fp, "# triangles = %d\n", &NumTris); // read # of triangles
- fscanf(fp, "Material count = %d\n", &material_count); // read material count
- glm::vec3 *ambient = (glm::vec3 *) malloc (sizeof(glm::vec3) * material_count);
- glm::vec3 *diffuse = (glm::vec3 *) malloc(sizeof(glm::vec3) * material_count);
- glm::vec3 *specular = (glm::vec3 *) malloc(sizeof(glm::vec3) * material_count);
- glm::vec3 *shine = (glm::vec3 *) malloc(sizeof(glm::vec3) * material_count);
- for (int i = 0; i<material_count; i++) {
- fscanf(fp, "ambient color %f %f %f\n", &(ambient[i].x), &(ambient[i].y), &(ambient[i].z));
- fscanf(fp, "diffuse color %f %f %f\n", &(diffuse[i].x), &(diffuse[i].y), &(diffuse[i].z));
- fscanf(fp, "specular color %f %f %f\n", &(specular[i].x), &(specular[i].y), &(specular[i].z));
- fscanf(fp, "material shine %f\n", &(shine[i]));
- }
- fscanf(fp, "%c", &ch);
- while (ch != '\n') // skip documentation line
- fscanf(fp, "%c", &ch);
- GLfloat *points = (GLfloat *) malloc(sizeof(GLfloat) * NumTris * 3 * 3);
- GLfloat *pnormals = (GLfloat *) malloc(sizeof(GLfloat) * NumTris * 4 * 3);
- GLfloat *fnormals = (GLfloat *) malloc(sizeof(GLfloat) * NumTris * 3);
- GLuint *indices = (GLuint *) malloc(sizeof(GLuint) * NumTris * 3);
- GLuint *color_index = (GLuint *) malloc(sizeof(GLuint) * NumTris * 3);
- for (int i = 0; i<NumTris; i++) // read triangles
- {
- fscanf(fp, "v0 %f %f %f %f %f %f %d\n",
- &(points[9 * i]), &(points[9 * i + 1]), &(points[9 * i + 2]),
- &(pnormals[9 * i]), &(pnormals[9 * i + 1]), &(pnormals[9 * i + 2]),
- &(color_index[3 * i]));
- fscanf(fp, "v1 %f %f %f %f %f %f %d\n",
- &(points[9 * i + 3]), &(points[9 * i + 4]), &(points[9 * i + 5]),
- &(pnormals[9 * i + 3]), &(pnormals[9 * i + 4]), &(pnormals[9 * i + 5]),
- &(color_index[3 * i + 1]));
- fscanf(fp, "v2 %f %f %f %f %f %f %d\n",
- &(points[9 * i + 6]), &(points[9 * i + 7]), &(points[9 * i + 8]),
- &(pnormals[9 * i + 6]), &(pnormals[9 * i + 7]), &(pnormals[9 * i + 8]),
- &(color_index[3 * i + 2]));
- fscanf(fp, "face normal %f %f %f\n", &(fnormals[3 * i]), &(fnormals[3 * i + 1]), &(fnormals[3 * i + 2]));
- // /*
- if (cw == 0) {
- indices[3 * i] = 3 * i;
- indices[3 * i + 1] = 3 * i + 1;
- indices[3 * i + 2] = 3 * i + 2;
- }
- else {
- indices[3 * i] = 3 * i + 2;
- indices[3 * i + 1] = 3 * i + 1;
- indices[3 * i + 2] = 3 * i;
- }
- // */
- }
- fclose(fp);
- // passa os vértices para a GPU (OpenGL)
- calcCameraPos(points, NumTris);
- GLuint vbo[3];
- glGenBuffers(3, vbo);
- GLuint EBO;
- glGenBuffers(1, &EBO);
- GLuint c2vbo;
- glGenBuffers(1, &c2vbo);
- GLuint c2EBO;
- glGenBuffers(1, &c2EBO);
- GLuint c2TBO;
- glGenBuffers(1, &c2TBO);
- glActiveTexture(GL_TEXTURE1);
- GLuint c2Tex;
- glGenTextures(1, &c2Tex);
- GLuint vao = 0;
- GLuint c2vao = 0;
- glGenVertexArrays(1, &vao);
- glGenVertexArrays(1, &c2vao);
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * NumTris * 3 * 3, points, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * NumTris * 3, indices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * NumTris * 4 * 3, pnormals, GL_STATIC_DRAW);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint) * NumTris * 3, color_index, GL_STATIC_DRAW);
- glVertexAttribPointer(2, 1, GL_UNSIGNED_INT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(2);
- glUseProgram(shader_program);
- Matr4 model = identity(); // Sempre será identidade, o que muda é a câmera
- char windowTitle[32];
- float *new_points = (float *) malloc (sizeof(float) * NumTris * 3 * 3);
- GLuint *new_indexes = (GLuint *) malloc (sizeof(GLuint) * NumTris * 3 * 3);
- while (!glfwWindowShouldClose(window)) {
- // wipe the drawing surface clear
- // Calcula o FPS para mostrar no title
- float currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- // liga/desliga o modo wireframe
- if (wireframe == 1)
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- if (wireframe == 0)
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- // liga/desliga o culling (OpenGL)
- if (cull == 1 && renderingmethod == OPENGL_MODE)
- glEnable(GL_CULL_FACE);
- else
- glDisable(GL_CULL_FACE);
- // escolhe o modo de renderização (OpenGl ou Close2Gl)
- if (renderingmethod == OPENGL_MODE)
- glUseProgram(shader_program);
- else
- glUseProgram(c2gl_shader_program);
- // Atualiza o title da janela (FPS)
- snprintf(windowTitle, sizeof(windowTitle), "FPS: %f\n", 1.0f / deltaTime);
- glfwSetWindowTitle(window, windowTitle);
- // Processa os inputs
- processInput(window);
- // Limpa o buffer de cor e o z-buffer
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Calcula a projection matrix
- Matr4 proj = perspective(M_PI / 3.0f, aspectRatio, nearplane, farplane);
- // Calcula a matriz view dependendo do tipo de câmera (look-at ou free camera)
- Matr4 view;
- if (cameraType == 0) {
- view = lookat(Vect4(sin(rotationx) * (calcz + 2.0f), 0.0f, cos(rotationx)*(calcz + 2.0f), 0.0f) + Vect4(objCenter.x, objCenter.y, objCenter.z, 1.0f), Vect4(objCenter.x, objCenter.y, objCenter.z, 1.0f), Vect4(cameraUp.x, cameraUp.y, cameraUp.z, 0.0f));
- }
- else {
- view = lookat(Vect4(cameraPos.x, cameraPos.y, cameraPos.z, 1.0f), Vect4(cameraPos.x + cameraFront.x, cameraPos.y + cameraFront.y, cameraPos.z + cameraFront.z, 1.0f), Vect4(cameraUp.x, cameraUp.y, cameraUp.z, 0.0f));
- }
- // Model View Proj calculada
- Matr4 ModelView = (view * model);
- Matr4 ModelViewIT = (ModelView.inverse()).transpose();
- Matr4 ModelViewProj = proj * ModelView;
- glm::vec3 trueCameraPosition;
- if (cameraType == 0) {
- trueCameraPosition = glm::vec3(glm::sin(rotationx) * (calcz + 2.0f), 0.0f, glm::cos(rotationx)*(calcz + 2.0f)) + objCenter;
- }
- else {
- trueCameraPosition = cameraPos;
- }
- int trueTriCount = 0;
- // Passa os vértices para a etapa de rasterização (Close2Gl)
- if (renderingmethod == CLOSE2GL_MODE) {
- int i = 0;
- float* vertex_colors = (float *) malloc (sizeof(float) * NumTris * 3 * 5);
- while (i < NumTris * 3 * 3) {
- Vect4 a(points[i], points[i + 1], points[i + 2], 1.0f);
- Vect4 b(points[i + 3], points[i + 4], points[i + 5], 1.0f);
- Vect4 c(points[i + 6], points[i + 7], points[i + 8], 1.0f);
- Vect4 na(pnormals[i], pnormals[i + 1], pnormals[i + 2], 1.0f);
- Vect4 nb(pnormals[i + 3], pnormals[i + 4], pnormals[i + 5], 1.0f);
- Vect4 nc(pnormals[i + 6], pnormals[i + 7], pnormals[i + 8], 1.0f);
- Vect4 newCA = ModelView * a;
- Vect4 newCB = ModelView * b;
- Vect4 newCC = ModelView * c;
- Vect4 newNA = ModelViewIT * na;
- Vect4 newNB = ModelViewIT * nb;
- Vect4 newNC = ModelViewIT * nc;
- Vect4 la(newCA.x - light_dir.x, newCA.y - light_dir.y, newCA.z - light_dir.z, 0.0f);
- Vect4 lb(newCB.x - light_dir.x, newCB.y - light_dir.y, newCB.z - light_dir.z, 0.0f);
- Vect4 lc(newCC.x - light_dir.x, newCC.y - light_dir.y, newCC.z - light_dir.z, 0.0f);
- Vect4 camera_position(trueCameraPosition.x, trueCameraPosition.y, trueCameraPosition.z, 1.0f);
- Vect4 va = newCA - camera_position;
- Vect4 vb = newCB - camera_position;
- Vect4 vc = newCC - camera_position;
- Vect4 Ha = normalize(va + la);
- Vect4 Hb = normalize(vb + lb);
- Vect4 Hc = normalize(vc + lc);
- Vect4 newA = proj * newCA;
- Vect4 newB = proj * newCB;
- Vect4 newC = proj * newCC;
- // Checa o w para não projetar negativos ou dividir por 0
- if (newA.w > 0) {
- if (newB.w > 0) {
- if (newC.w > 0) {
- float AW = newA.w;
- float BW = newB.w;
- float CW = newC.w;
- newA = newA / newA.w;
- newB = newB / newB.w;
- newC = newC / newC.w;
- // Testa o culling de acordo com o OpenGL Red Book
- float cullSum = 0.0f;
- cullSum += (newA.x + 1) * (newB.y + 1) - (newB.x + 1) * (newA.y + 1);
- cullSum += (newB.x + 1) * (newC.y + 1) - (newC.x + 1) * (newB.y + 1);
- cullSum += (newC.x + 1) * (newA.y + 1) - (newA.x + 1) * (newC.y + 1);
- cullSum = cullSum / 2.0f;
- if ((cull == 0 || cullSum < 0)
- && ((newA.x < 1.0f) && (newA.x > -1.0f) && (newA.y < 1.0f) && (newA.y > -1.0f) && (newA.z < 1.0f) && (newA.z > -1.0f))
- && ((newB.x < 1.0f) && (newB.x > -1.0f) && (newB.y < 1.0f) && (newB.y > -1.0f) && (newB.z < 1.0f) && (newB.z > -1.0f))
- && ((newC.x < 1.0f) && (newC.x > -1.0f) && (newC.y < 1.0f) && (newC.y > -1.0f) && (newC.z < 1.0f) && (newC.z > -1.0f))) {
- new_points[trueTriCount * 9] = newA.x;
- new_points[trueTriCount * 9 + 1] = newA.y;
- new_points[trueTriCount * 9 + 2] = newA.z;
- new_points[trueTriCount * 9 + 3] = newB.x;
- new_points[trueTriCount * 9 + 4] = newB.y;
- new_points[trueTriCount * 9 + 5] = newB.z;
- new_points[trueTriCount * 9 + 6] = newC.x;
- new_points[trueTriCount * 9 + 7] = newC.y;
- new_points[trueTriCount * 9 + 8] = newC.z;
- new_indexes[trueTriCount * 9] = indices[i];
- new_indexes[trueTriCount * 9 + 1] = indices[i + 1];
- new_indexes[trueTriCount * 9 + 2] = indices[i + 2];
- new_indexes[trueTriCount * 9 + 3] = indices[i + 3];
- new_indexes[trueTriCount * 9 + 4] = indices[i + 4];
- new_indexes[trueTriCount * 9 + 5] = indices[i + 5];
- new_indexes[trueTriCount * 9 + 6] = indices[i + 6];
- new_indexes[trueTriCount * 9 + 7] = indices[i + 7];
- new_indexes[trueTriCount * 9 + 8] = indices[i + 8];
- Vect4 colorA = Vect4(0.1f, 0.1f, 0.1f, 1.0f)
- + Vect4(colorm.x, colorm.y, colorm.z, colorm.w) * (0.3f * std::max(0.0f, dot(normalize(newNA), normalize(la))))
- + Vect4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6f * std::max(0.0f, pow((dot(Ha, normalize(newNA))), 10));
- Vect4 colorB = Vect4(0.1f, 0.1f, 0.1f, 1.0f)
- + Vect4(colorm.x, colorm.y, colorm.z, colorm.w) * (0.3f * std::max(0.0f, dot(normalize(newNB), normalize(lb))))
- + Vect4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6f * std::max(0.0f, pow((dot(Hb, normalize(newNB))), 10));
- Vect4 colorC = Vect4(0.1f, 0.1f, 0.1f, 1.0f)
- + Vect4(colorm.x, colorm.y, colorm.z, colorm.w) * (0.3f * std::max(0.0f, dot(normalize(newNC), normalize(lc))))
- + Vect4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6f * std::max(0.0f, pow((dot(Hc, normalize(newNC))), 10));
- vertex_colors[trueTriCount * 15] = colorA.x / AW;
- vertex_colors[trueTriCount * 15 + 1] = colorA.y / AW;
- vertex_colors[trueTriCount * 15 + 2] = colorA.z / AW;
- vertex_colors[trueTriCount * 15 + 3] = colorA.w / AW;
- vertex_colors[trueTriCount * 15 + 4] = 1.0f / AW;
- vertex_colors[trueTriCount * 15 + 5] = colorB.x / BW;
- vertex_colors[trueTriCount * 15 + 6] = colorB.y / BW;
- vertex_colors[trueTriCount * 15 + 7] = colorB.z / BW;
- vertex_colors[trueTriCount * 15 + 8] = colorB.w / BW;
- vertex_colors[trueTriCount * 15 + 9] = 1.0f / BW;
- vertex_colors[trueTriCount * 15 + 10] = colorC.x / CW;
- vertex_colors[trueTriCount * 15 + 11] = colorC.y / CW;
- vertex_colors[trueTriCount * 15 + 12] = colorC.z / CW;
- vertex_colors[trueTriCount * 15 + 13] = colorC.w / CW;
- vertex_colors[trueTriCount * 15 + 14] = 1.0f / CW;
- trueTriCount++;
- }
- }
- }
- }
- i += 9;
- }
- float vertex_data[] = { -1.0f, -1.0f, 0.5f, 1.0f, -1.0f, 0.5f, 1.0f, 1.0f, 0.5f,
- 1.0f, 1.0f, 0.5f, -1.0f, 1.0f, -0.5f, -1.0f, -1.0f, 0.5f };
- int vertex_index[] = { 0, 1, 2, 3, 4, 5};
- float texture_coords[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f };
- glBindVertexArray(c2vao);
- /*
- glBindBuffer(GL_ARRAY_BUFFER, c2vbo);
- glBufferData(GL_ARRAY_BUFFER, trueTriCount * 3 * 3 * sizeof(float), new_points, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, c2EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, trueTriCount * 3 * sizeof(int), indices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(0);
- */
- // glDisable(GL_CULL_FACE);
- glBindBuffer(GL_ARRAY_BUFFER, c2vbo);
- glBufferData(GL_ARRAY_BUFFER, 3*3*2*sizeof(float), vertex_data, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, c2EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * 3 * sizeof(int), vertex_index, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, c2TBO);
- glBufferData(GL_ARRAY_BUFFER, 2 * 3 * 2 * sizeof(float), texture_coords, GL_STATIC_DRAW);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(1);
- float *color_buffer = (float *) calloc (w_width * w_height * 4, sizeof(float));
- float *depth_buffer = (float *) calloc (w_width * w_height, sizeof(float));
- Matr4 v = viewport(w_width, w_height, 0.0f, 0.0f);
- for (int t = 0; t < trueTriCount; t++) {
- Vect4 d (new_points[t * 9], new_points[t * 9 + 1], new_points[t * 9 + 2], 1.0f);
- Vect4 e (new_points[t * 9 + 3], new_points[t * 9 + 4], new_points[t * 9 + 5], 1.0f);
- Vect4 f (new_points[t * 9 + 6], new_points[t * 9 + 7], new_points[t * 9 + 8], 1.0f);
- int a_ix = 0;
- int b_ix = 1;
- int c_ix = 2;
- Vect4 a = v * d;
- Vect4 b = v * e;
- Vect4 c = v * f;
- if (a.y > b.y) {
- Vect4 aux = a;
- a = b;
- b = aux;
- a_ix += b_ix;
- b_ix = a_ix - b_ix;
- a_ix -= b_ix;
- }
- if (a.y > c.y) {
- Vect4 aux = a;
- a = c;
- c = aux;
- a_ix += c_ix;
- c_ix = a_ix - c_ix;
- a_ix -= c_ix;
- }
- if (b.y > c.y) {
- Vect4 aux = b;
- b = c;
- c = aux;
- b_ix += c_ix;
- c_ix = b_ix - c_ix;
- b_ix -= c_ix;
- }
- if ((a.y == b.y) || (b.y == c.y) || (a.y == c.y)) {
- if (a.y == b.y && b.y != c.y) {
- int ii1 = round(a.x);
- int ij = (int)(a.y);
- int ii2 = round(b.x);
- if (ii1 > ii2) {
- ii1 = ii2 + ii1;
- ii2 = ii1 - ii2;
- ii1 = ii1 - ii2;
- }
- for (int i = ii1; i <= ii2; i++) {
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij)] = 1.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij) + 1] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij) + 2] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij) + 3] = 1.0f;
- }
- float dx = c.x - a.x;
- float dy = c.y - a.y;
- float dz = c.z - a.z;
- float ix = dx / dy;
- float iz = dz / dy;
- float dx2 = c.x - b.x;
- float dy2 = c.y - b.y;
- float dz2 = c.z - b.z;
- float ix2 = dx2 / dy2;
- float iz2 = dz2 / dy2;
- for (int cy = 1; cy < dy2; cy++) {
- int i1 = round(a.x + ix * cy);
- int j = (int)(a.y + cy);
- int i2 = round(a.x + ix2 * cy);
- if (i1 > i2) {
- i1 = i2 + i1;
- i2 = i1 - i2;
- i1 = i1 - i2;
- }
- for (int i = i1; i <= i2; i++) {
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = 1.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = 1.0f;
- }
- }
- }
- else if (b.y == c.y && a.y != c.y) {
- int i = round(a.x);
- int j = (int) a.y;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = 1.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = 1.0f;
- float dx = b.x - a.x;
- float dy = b.y - a.y;
- float dz = b.z - a.z;
- float ix = dx / dy;
- float iz = dz / dy;
- float dx2 = c.x - a.x;
- float dy2 = c.y - a.y;
- float dz2 = c.z - a.z;
- float ix2 = dx2 / dy2;
- float iz2 = dz2 / dy2;
- for (int cy = 1; cy < dy2; cy++) {
- int i1 = round(a.x + ix * cy);
- int j = (int)(a.y + cy);
- int i2 = round(a.x + ix2 * cy);
- if (i1 > i2) {
- i1 = i2 + i1;
- i2 = i1 - i2;
- i1 = i1 - i2;
- }
- for (int i = i1; i <= i2; i++) {
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = 1.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = 0.0f;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = 1.0f;
- }
- }
- }
- }
- else {
- float dx = b.x - a.x;
- float dy = b.y - a.y;
- float dz = b.z - a.z;
- float sq_d = pow(dx, 2) + pow(dy, 2);
- float ix = dx / dy;
- float iz = dz / dy;
- float dx2 = c.x - a.x;
- float dy2 = c.y - a.y;
- float dz2 = c.z - a.z;
- float sq_d2 = pow(dx2, 2) + pow(dy2, 2);
- float ix2 = dx2 / dy2;
- float iz2 = dz2 / dy2;
- float x0 = a.x;
- int offset = 0;
- int line_change = 0;
- Vect4 colora(vertex_colors[t * 15], vertex_colors[t * 15 + 1], vertex_colors[t * 15 + 2], vertex_colors[t * 15 + 3]);
- float aw = vertex_colors[t*15 + 4];
- Vect4 colorb(vertex_colors[t * 15 + 5], vertex_colors[t * 15 + 6], vertex_colors[t * 15 + 7], vertex_colors[t * 15 + 8]);
- float bw = vertex_colors[t * 15 + 9];
- Vect4 colorc(vertex_colors[t * 15 + 10], vertex_colors[t * 15 + 11], vertex_colors[t * 15 + 12], vertex_colors[t * 15 + 13]);
- float cw = vertex_colors[t * 15 + 14];
- for (int cy = 0; cy < dy2; cy++) {
- if ((line_change == 0) && (cy >= dy)) {
- offset = cy;
- dx = c.x - b.x;
- dy = c.y - b.y;
- dz = c.z - b.z;
- sq_d = pow(dx, 2) + pow(dy, 2);
- ix = dx / dy;
- iz = dz / dy;
- x0 = b.x;
- line_change = 1;
- }
- float f1 = x0 + ix * (cy - offset);
- float f2 = a.x + ix2 * cy;
- int i1 = round(f1);
- int j = (int)(a.y + cy);
- int i2 = round(f2);
- float alpha1 = sqrt((pow(f1 - x0, 2) + pow(cy - offset, 2)) / sq_d);
- float alpha2 = sqrt((pow(f2 - a.x, 2) + pow(cy, 2)) / sq_d2);
- Vect4 colorleft = (1 - line_change) * (colora * alpha1 + colorb * (1-alpha1)) + (line_change) * (colorb * alpha1 + colorc * (1-alpha1));
- Vect4 colorright = colora * alpha2 + colorc * (1-alpha2);
- float wleft = (1 - line_change) * (aw * alpha1 + bw * (1 - alpha1)) + (line_change)* (bw * alpha1 + cw * (1 - alpha1));
- float wright = aw * alpha2 + cw * (1 - alpha2);
- if (i1 > i2) {
- i1 = i2 + i1;
- i2 = i1 - i2;
- i1 = i1 - i2;
- colorleft = colorleft + colorright;
- colorright = colorleft - colorright;
- colorleft = colorleft - colorright;
- wleft = wright + wleft;
- wright = wleft - wright;
- wleft = wleft - wright;
- }
- for (int i = i1; i <= i2; i++) {
- float thirdalpha;
- if (i2 == i1 && i == i1)
- thirdalpha = 0.0f;
- else
- thirdalpha = (i - i1) / (i2 - i1);
- float finalw = wleft * thirdalpha + wright * (1 - thirdalpha);
- Vect4 finalcolor = colorleft * thirdalpha + colorright * (1 - thirdalpha);
- finalcolor = finalcolor / finalw;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = finalcolor.x;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = finalcolor.y;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = finalcolor.z;
- color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = finalcolor.w;
- }
- //printf("1 %d\n", t);
- }
- }
- }
- glBindTexture(GL_TEXTURE_2D, c2Tex);
- glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA4, w_width, w_height);
- glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w_width, w_height, GL_RGBA, GL_FLOAT, color_buffer);
- free(color_buffer);
- free(depth_buffer);
- free(vertex_colors);
- int texLoc = glGetUniformLocation(c2gl_shader_program, "tex");
- glUniform1i(texLoc, 1);
- }
- // Passa as uniformes para o método Open GL
- if (renderingmethod == OPENGL_MODE) {
- int matLoc = glGetUniformLocation(shader_program, "ModelView");
- glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr((ModelView).convertToGLM()));
- matLoc = glGetUniformLocation(shader_program, "Proj");
- glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr((proj).convertToGLM()));
- matLoc = glGetUniformLocation(shader_program, "ModelViewIT");
- glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr((ModelViewIT).convertToGLM()));
- int colLoc = glGetUniformLocation(shader_program, "colorm");
- glUniform4fv(colLoc, 1, glm::value_ptr(colorm));
- int smeLoc = glGetUniformLocation(shader_program, "shading_method");
- glUniform1i(smeLoc, shadingmethod);
- int camLoc = glGetUniformLocation(shader_program, "camera_position");
- glUniform3fv(camLoc, 1, glm::value_ptr(trueCameraPosition));
- int ligLoc = glGetUniformLocation(shader_program, "light_pos");
- glUniform4fv(ligLoc, 1, glm::value_ptr(light_dir));
- }
- else {
- int colLoc = glGetUniformLocation(c2gl_shader_program, "colorm");
- glUniform4fv(colLoc, 1, glm::value_ptr(colorm));
- }
- if (renderingmethod == OPENGL_MODE) {
- glBindVertexArray(vao);
- glDrawElements(GL_TRIANGLES, NumTris * 3, GL_UNSIGNED_INT, 0);
- }
- else {
- glBindVertexArray(c2vao);
- //glDrawElements(GL_TRIANGLES, trueTriCount * 4, GL_UNSIGNED_INT, 0);
- glDrawElements(GL_TRIANGLES, 2*3, GL_UNSIGNED_INT, 0);
- }
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- // close GL context and any other GLFW resources
- glfwTerminate();
- return 0;
- }
- void processInput(GLFWwindow *window)
- {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
- glfwSetWindowShouldClose(window, true);
- }
- float cameraSpeed = 50 * deltaTime;
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
- if (cameraType == 0)
- calcz -= cameraSpeed;
- else
- cameraPos += cameraSpeed * cameraFront;
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
- if (cameraType == 0)
- calcz += cameraSpeed;
- else
- cameraPos -= cameraSpeed * cameraFront;
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
- if (cameraType == 0)
- rotationx -= cameraSpeed;
- else
- cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
- if (cameraType == 0)
- rotationx += cameraSpeed;
- else
- cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
- if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
- if (cameraType == 1)
- cameraPos += cameraUp * cameraSpeed;
- if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
- if (cameraType == 1)
- cameraPos -= cameraUp * cameraSpeed;
- if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
- {
- nearplane -= 400.0f * deltaTime; printf("Nearplane: %f\n", nearplane);
- }
- if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
- {
- nearplane += 400.0f * deltaTime; printf("Nearplane: %f\n", nearplane);
- }
- if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
- {
- farplane -= 400.0f * deltaTime; printf("Farplane: %f\n", farplane);
- }
- if (glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS)
- {
- farplane += 400.0f * deltaTime; printf("Farplane: %f\n", farplane);
- }
- if (glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS)
- {
- colorm.x -= 0.01f; if (colorm.x < 0.0f) colorm.x = 0.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_5) == GLFW_PRESS)
- {
- colorm.y -= 0.01f; if (colorm.y < 0.0f) colorm.y = 0.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS)
- {
- colorm.z -= 0.01f; if (colorm.z < 0.0f) colorm.z = 0.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_7) == GLFW_PRESS)
- {
- colorm.x += 0.01f; if (colorm.x > 1.0f) colorm.x = 1.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS)
- {
- colorm.y += 0.01f; if (colorm.y > 1.0f) colorm.y = 1.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_9) == GLFW_PRESS)
- {
- colorm.z += 0.01f; if (colorm.z > 1.0f) colorm.z = 1.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_1) == GLFW_PRESS)
- printf("Color: %f %f %f\n", colorm.x, colorm.y, colorm.z);
- if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
- cameraType = 1 - cameraType;
- if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
- shadingmethod = 0;
- if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
- shadingmethod = 1;
- if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
- shadingmethod = 2;
- if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS)
- renderingmethod = OPENGL_MODE;
- if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS)
- renderingmethod = CLOSE2GL_MODE;
- if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS)
- cull = 0;
- if (glfwGetKey(window, GLFW_KEY_7) == GLFW_PRESS)
- cull = 1;
- if (glfwGetKey(window, GLFW_KEY_8) == GLFW_PRESS)
- wireframe = 0;
- if (glfwGetKey(window, GLFW_KEY_9) == GLFW_PRESS)
- wireframe = 1;
- if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
- {
- cameraPos = cameraDefaultPos;
- cameraFront = cameraDefaultFront;
- calcz = calcdz;
- yaw = -90.0f;
- pitch = 0.0f;
- rotationx = 0.0f;
- lastX = 320.0f;
- lastY = 240.0f;
- }
- }
- void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
- if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != GLFW_PRESS)
- return;
- float xoffset = xpos - lastX;
- float yoffset = lastY - ypos; // reversed since y-coordinates range from bottom to top
- lastX = xpos;
- lastY = ypos;
- float sensitivity = 0.5f;
- xoffset *= sensitivity;
- yoffset *= sensitivity;
- yaw += xoffset;
- pitch += yoffset;
- if (pitch > 89.0f)
- pitch = 89.0f;
- else if (pitch < -89.0f)
- pitch = -89.0f;
- glm::vec3 front;
- front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
- front.y = sin(glm::radians(pitch));
- front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
- cameraFront = glm::normalize(front);
- }
- void FramebufferSizeCallback(GLFWwindow* window, int width, int height) {
- glViewport(0, 0, width, height);
- aspectRatio = (float)width / height;
- w_width = width;
- w_height = height;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement