basic triangle
This commit is contained in:
parent
a80395b964
commit
b012641dc9
2
build.sh
2
build.sh
|
@ -1,2 +1,2 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
cd build && cmake .. && make && ./TimeCoil
|
cd build && cmake -DCMAKE_C_FLAGS="-g" .. && make && ./TimeCoil
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
|
}
|
114
src/main.cpp
114
src/main.cpp
|
@ -1,6 +1,8 @@
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
|
void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
|
||||||
glViewport(10, 10, width-10, height-10);
|
glViewport(10, 10, width-10, height-10);
|
||||||
|
@ -16,20 +18,91 @@ void renderGraphics(GLFWwindow *window) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void gracefulExit(int code) {
|
||||||
|
glfwTerminate();
|
||||||
|
exit(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string readFile(std::string path) {
|
||||||
|
std::ifstream file(path);
|
||||||
|
if (!file.is_open())
|
||||||
|
std::cerr << "File " << path << " was not found." << std::endl;
|
||||||
|
|
||||||
|
std::string content = "";
|
||||||
|
std::string buffer = "";
|
||||||
|
|
||||||
|
while(getline(file, buffer)) {
|
||||||
|
content += buffer + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
content += '\0';
|
||||||
|
std::cout << content << std::endl;
|
||||||
|
file.close();
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int prepareShaders() {
|
||||||
|
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
std::string vertexShaderSourceString = readFile("../shaders/shader.vert");
|
||||||
|
std::string fragmentShaderSourceString = readFile("../shaders/shader.frag");
|
||||||
|
|
||||||
|
const char *vertexShaderSource = vertexShaderSourceString.c_str();
|
||||||
|
const char *fragmentShaderSource = fragmentShaderSourceString.c_str();
|
||||||
|
|
||||||
|
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
||||||
|
glCompileShader(vertexShader);
|
||||||
|
|
||||||
|
int success;
|
||||||
|
char log[512];
|
||||||
|
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(vertexShader, 512, NULL, log);
|
||||||
|
std::cerr << "Could not compile vertex shader: " << log << std::endl;
|
||||||
|
gracefulExit(-1);
|
||||||
|
}
|
||||||
|
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
|
||||||
|
glCompileShader(fragmentShader);
|
||||||
|
|
||||||
|
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(fragmentShader, 512, NULL, log);
|
||||||
|
std::cerr << "Could not compile fragment shader: " << log << std::endl;
|
||||||
|
gracefulExit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int shaderProgram;
|
||||||
|
shaderProgram = glCreateProgram();
|
||||||
|
glAttachShader(shaderProgram, vertexShader);
|
||||||
|
glAttachShader(shaderProgram, fragmentShader);
|
||||||
|
glLinkProgram(shaderProgram);
|
||||||
|
|
||||||
|
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||||
|
if(!success) {
|
||||||
|
glGetProgramInfoLog(shaderProgram, 512, NULL, log);
|
||||||
|
std::cerr << "Could not link program: " << log << std::endl;
|
||||||
|
gracefulExit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
|
return shaderProgram;
|
||||||
|
}
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GLFWwindow *window = glfwCreateWindow(800, 600, "TimeCoil", NULL, NULL);
|
GLFWwindow *window = glfwCreateWindow(800, 600, "TimeCoil", NULL, NULL);
|
||||||
if (window == NULL) {
|
if (window == NULL) {
|
||||||
std::cerr << "Failed to create GLFW window." << std::endl;
|
std::cerr << "Failed to create GLFW window." << std::endl;
|
||||||
glfwTerminate();
|
gracefulExit(-1);
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
@ -40,16 +113,43 @@ int main () {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int shaderProgram = prepareShaders();
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
-0.5, 0.0, 0.0,
|
||||||
|
0.5, 0.5, 0.0,
|
||||||
|
0.5, -0.5, 0.0
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int VBO, VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
processInput(window);
|
processInput(window);
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
glUseProgram(shaderProgram);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwTerminate();
|
glDeleteVertexArrays(1, &VAO);
|
||||||
return 0;
|
glDeleteBuffers(1, &VBO);
|
||||||
|
glDeleteProgram(shaderProgram);
|
||||||
|
gracefulExit(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue