Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from SDL2 to GLFW, add WebGL compatibility #1

Merged
merged 7 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"cfenv": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"__nullptr": "cpp"
"__nullptr": "cpp",
"filesystem": "cpp"
}
}
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
CXX= g++
CXXFLAGS=-std=c++17 -fopenmp -MMD
OBJECTS=main.o graph.o node.o vec3d.o universe.o rgb.o mycamera.o
OBJECTS=main.o graph.o node.o utils/vec3d.o utils/rgb.o \
universe.o mycamera.o \
draw_utils/solid_sphere.o draw_utils/line.o


DEPENDS=${OBJECTS:.o=.d}
EXEC=fdg


${EXEC}: ${OBJECTS}
${CXX} ${OBJECTS} -o ${EXEC} -I/usr/X11R6/include -L/usr/X11R6/lib \
-lGL -lGLEW -lSDL2 -lGLU -fopenmp
-lGL -lGLEW -lGLU -fopenmp -lglfw

rm ${OBJECTS} ${DEPENDS}

Expand Down
22 changes: 22 additions & 0 deletions Makefile_emscripten
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CXX= emcc
CXXFLAGS=-std=c++17 -MMD
OBJECTS=main.o graph.o node.o utils/vec3d.o utils/rgb.o \
universe.o mycamera.o \
draw_utils/solid_sphere.o draw_utils/line.o

DEPENDS=${OBJECTS:.o=.d}
EXEC=fdg.html


${EXEC}: ${OBJECTS}
${CXX} ${OBJECTS} -o ${EXEC} -I/usr/X11R6/include -L/usr/X11R6/lib \
-lGL -lGLEW -lGLU -lglfw -s FULL_ES3=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -s USE_GLFW=3 -s WASM=1 -sLINKABLE=1 -sEXPORT_ALL=1

rm ${OBJECTS} ${DEPENDS}

-include ${DEPENDS}

.PHONY: clean

clean:
rm ${OBJECTS} ${DEPENDS} ${EXEC}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Fudge: A 3D Force Directed Graph Visualizer
# fudge - 3D force directed graph explorer

## Build
```make && ./fdg```
143 changes: 143 additions & 0 deletions draw_utils/line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <GL/glew.h>
#include <GL/glu.h>
#include "../glm/glm/ext.hpp"
#include <iostream>
#include "line.h"
using namespace std;

// GL_LINES is deprecated, use this instead
// https://stackoverflow.com/questions/14486291/how-to-draw-line-in-opengl
Line::Line(glm::vec3 start, glm::vec3 end)
{

startPoint = start;
endPoint = end;
lineColor = glm::vec3(1, 1, 1);

}

void Line::init() {
const char *vertexShaderSource = "#version 300 es \n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 MVP;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
"}\0";
const char *fragmentShaderSource = "#version 300 es \n"
"precision mediump float;\n"
"out vec4 FragColor;\n"
"uniform vec3 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(color, 1.0f);\n"
"}\n\0";

int success;
char infoLog[512];
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

// check for shader compile errors
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
<< infoLog << std::endl;
};
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);

// check for shader compile errors
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"
<< infoLog << std::endl;
};

// link shaders
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

// vertices = {
// start.x,
// start.y,
// start.z,
// end.x,
// end.y,
// end.z,

// };

glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * vertices.size(), vertices.data(), GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Line::setVertices(glm::vec3 start, glm::vec3 end)
{
vertices = {
start.x,
start.y,
start.z,
end.x,
end.y,
end.z,

};
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
}

int Line::setMVP(glm::mat4 mvp)
{
MVP = mvp;
return 1;
}

int Line::setColor(glm::vec3 color)
{
lineColor = color;
return 1;
}

int Line::draw()
{
glUseProgram(shaderProgram);

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "MVP"), 1, GL_FALSE, &MVP[0][0]);
glUniform3fv(glGetUniformLocation(shaderProgram, "color"), 1, &lineColor[0]);

glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, 2);
return 1;
}

Line::~Line()
{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
}
25 changes: 25 additions & 0 deletions draw_utils/line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <GL/glew.h>
#include <GL/glu.h>
#include "../glm/glm/ext.hpp"
#include <iostream>
#include <vector>
#ifndef FUDGE_LINE_H
#define FUDGE_LINE
class Line {
int shaderProgram;
unsigned int VBO, VAO;
std::vector<float> vertices;
glm::vec3 startPoint;
glm::vec3 endPoint;
glm::mat4 MVP = glm::mat4(1.0);
glm::vec3 lineColor;
public:
int setMVP(glm::mat4 mvp);
int setColor(glm::vec3 color);
void setVertices(glm::vec3 start, glm::vec3 end);
int draw();
void init();
Line(glm::vec3 start, glm::vec3 end);
~Line();
};
#endif
Loading