karo/karo.cpp
2024-02-15 21:20:41 +01:00

200 lines
5.0 KiB
C++

// karo.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
struct Vertex {
glm::vec3 pos;
glm::vec3 color;
};
static const char* s_vs_src = R"GLSL(
#version 330 core
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_color;
out vec3 u_color;
uniform mat4 u_mvp;
void main() {
gl_Position = u_mvp * vec4(a_pos, 1.0);
u_color = a_color;
}
)GLSL";
static const char* s_fs_src = R"GLSL(
#version 330 core
in vec3 u_color;
out vec4 o_color;
void main() {
o_color = vec4(u_color, 1.0);
}
)GLSL";
void CheckShaderCompileErrors(GLuint shader) {
GLint success;
GLchar infoLog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, infoLog);
std::cerr << "Shader compilation failed: " << infoLog << std::endl;
exit(1);
}
}
void CheckProgramLinkErrors(GLuint program) {
GLint success;
GLchar infoLog[512];
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 512, NULL, infoLog);
std::cerr << "Program link failed: " << infoLog << std::endl;
exit(1);
}
}
int main() {
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW\n";
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
glfwWindowHint(GLFW_DEPTH_BITS, 0);
//glfwWindowHint(GLFW_DEPTH_BITS, 0);
glfwWindowHint(GLFW_STENCIL_BITS, 0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Karo", NULL, NULL);
if (!window) {
std::cerr << "Failed to create window\n";
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW\n";
glfwTerminate();
return 1;
}
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
GLuint shader;
shader = glCreateProgram();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &s_vs_src, NULL);
glCompileShader(vs);
CheckShaderCompileErrors(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &s_fs_src, NULL);
glCompileShader(fs);
CheckShaderCompileErrors(fs);
glAttachShader(shader, vs);
glAttachShader(shader, fs);
glLinkProgram(shader);
CheckProgramLinkErrors(shader);
glDeleteShader(vs);
glDeleteShader(fs);
glUseProgram(shader);
GLuint u_mvp = glGetUniformLocation(shader, "u_mvp");
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
//setup for stream draw
glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, pos));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glBindVertexArray(0);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glfwShowWindow(window);
//list of lines
std::vector<Vertex> vertices;
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 mvp = proj * view;
glUniformMatrix4fv(u_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// test data
vertices.push_back({ glm::vec3(-1, 0, 0), glm::vec3(1.0f, 0.0f, 0.0f) });
vertices.push_back({ glm::vec3(1, 0, 0), glm::vec3(0.0f, 1.0f, 0.0f) });
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STREAM_DRAW);
glDrawArrays(GL_LINES, 0, vertices.size());
vertices.clear();
glfwSwapBuffers(window);
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file