Drew a triangle

This commit is contained in:
Dane Johnson 2021-01-13 10:42:57 -06:00
parent 584a8654a8
commit 41b99aa442
20 changed files with 325 additions and 4 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
core/couch

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
$(MAKE) -C core

3
common.mk Normal file
View File

@ -0,0 +1,3 @@
CXX = g++
CXXFLAGS = `pkg-config --cflags gl glew glfw3 guile-2.2` -g
LIBS = `pkg-config --libs gl glew glfw3 guile-2.2`

10
core/Ball.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "Ball.h"
Ball::Ball() {
vertices.push_back(Vertex(0.0f, 0.0f, 0.0f));
vertices.push_back(Vertex(1.0f, 0.0f, 0.0f));
vertices.push_back(Vertex(0.5f, 1.0f, 0.0f));
indices.push_back(Index(0, 1, 2));
}

11
core/Ball.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef BALL_H
#define BALL_H
#include "Mesh.h"
class Ball : public Mesh {
public:
Ball();
};
#endif /* BALL_H */

7
core/Index.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "Index.h"
Index::Index(unsigned int v0, unsigned int v1, unsigned int v2) {
this->v0 = v0;
this->v1 = v1;
this->v2 = v2;
}

13
core/Index.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef INDEX_H
#define INDEX_H
#include <vector>
struct Index {
unsigned int v0, v1, v2;
Index(unsigned int v0, unsigned int v1, unsigned int v2);
};
typedef std::vector<Index> IndexList;
#endif /* INDEX_H */

8
core/Makefile Normal file
View File

@ -0,0 +1,8 @@
include ../common.mk
SOURCES = $(wildcard *.cpp)
OBJS = $(SOURCES:.cpp=.o)
all: couch
couch: $(OBJS)
$(CXX) $(LIBS) -o $@ $^

36
core/Mesh.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "Mesh.h"
Mesh::Mesh() {}
Mesh::Mesh(VertexList vertices, IndexList indices) {
this->vertices = vertices;
this->indices = indices;
}
void Mesh::SetupMesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex),
&vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index),
&indices[0], GL_STATIC_DRAW);
// Vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
// TODO normals, uv
glBindVertexArray(0);
}
void Mesh::Draw() {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

22
core/Mesh.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef MESH_H
#define MESH_H
#include "types.h"
#include "Vertex.h"
#include "Index.h"
#include "Transform.h"
class Mesh {
public:
VertexList vertices;
IndexList indices;
Transform transform;
Mesh();
Mesh(VertexList vertices, IndexList indices);
void Draw();
virtual void SetupMesh();
private:
Id VAO, VBO, EBO;
};
#endif /* MESH_H */

73
core/Shader.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "Shader.h"
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
} catch (std::ifstream::failure e) {
std::cerr << "Error reading shader file." << std::endl;
}
const char * vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
Id vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cerr << "Vertex shader failed compilation\n" << infoLog << std::endl;
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cerr << "Fragment shader failed compilation\n" << infoLog << std::endl;
}
id = glCreateProgram();
glAttachShader(id, vertex);
glAttachShader(id, fragment);
glLinkProgram(id);
glGetProgramiv(id, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(id, 512, NULL, infoLog);
std::cerr << "Shader program linking failed\n" << infoLog << std::endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Shader::Use() {
glUseProgram(id);
}

19
core/Shader.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef SHADER_H
#define SHADER_H
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include "types.h"
class Shader {
public:
Id id;
Shader(const char *vertexPath, const char *fragmentPath);
void Use();
};
#endif /* SHADER_H */

9
core/Transform.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "Transform.h"
Transform::Transform() {
position = Vector3(0.0f);
}
Transform::Transform(Vector3 position) {
this->position = position;
}

View File

@ -4,7 +4,9 @@
#include "types.h"
struct Transform {
Position position;
}
Transform();
Transform(Vector3 position);
Vector3 position;
};
#endif /* TRANSFORM_H */

13
core/Vertex.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "Vertex.h"
Vertex::Vertex() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
Vertex::Vertex(cfloat x, cfloat y, cfloat z) {
this->x = x;
this->y = y;
this->z = z;
}

View File

@ -1,10 +1,16 @@
#ifndef VERTEX_H
#define VERTEX_H
#include <vector>
#include "types.h"
struct Vertex {
Vertex();
Vertex(cfloat x, cfloat y, cfloat z);
cfloat x, y, z;
}
};
typedef std::vector<Vertex> VertexList;
#endif /* VERTEX_H */

58
core/couch.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "types.h"
#include "Shader.h"
#include "Ball.h"
Window *window;
const int width = 800;
const int height = 600;
int main() {
int err;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, "Couch", NULL, NULL);
if (!window) {
std::cerr << "Error creating window" << std::endl;
return 1;
}
glfwMakeContextCurrent(window);
err = glewInit();
if (err != GLEW_OK) {
std::cerr << "Error initializing glew" << std::endl;
}
glViewport(0, 0, width, height);
Shader shader("shaders/flat.vert", "shaders/flat.frag");
shader.Use();
Ball ball;
ball.SetupMesh();
while(!glfwWindowShouldClose(window)) {
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ball.Draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

View File

@ -1,10 +1,15 @@
#ifndef TYPES_H
#define TYPES_H
#include <vector>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
typedef glm::vec3 Position;
typedef GLFWwindow Window;
typedef glm::vec3 Vector3;
typedef GLfloat cfloat;
typedef GLuint Id;
#endif /* TYPES_H */

10
shaders/flat.frag Normal file
View File

@ -0,0 +1,10 @@
#version 330 core
// uniform vec3 color;
out vec4 FragColor;
void main() {
//FragColor = vec4(color, 1.0);
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

12
shaders/flat.vert Normal file
View File

@ -0,0 +1,12 @@
#version 330 core
layout (location = 0) in vec3 pos;
// uniform mat4 MODEL;
// uniform mat4 VIEW;
// uniform mat4 PROJECTION;
void main() {
//gl_Position = MODEL * VIEW * PROJECTION * vec4(pos, 1.0);
gl_Position = vec4(pos, 1.0);
}