initial commit.
This commit is contained in:
commit
c74c452362
17 changed files with 4983 additions and 0 deletions
146
mybox.cpp
Normal file
146
mybox.cpp
Normal file
|
@ -0,0 +1,146 @@
|
|||
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
#include "src/standard_ints.h"
|
||||
#include "src/lgl_callbacks.h"
|
||||
#include "src/lgl_shader_tools.h"
|
||||
|
||||
int main(void) {
|
||||
|
||||
std::cout << "Hello World." << std::endl;
|
||||
|
||||
// Setup GLFW context
|
||||
glfwInit();
|
||||
|
||||
// Establish 4.2 GL requirement
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
// Build the winodw
|
||||
GLFWwindow *window = glfwCreateWindow(960, 400,
|
||||
"Hello MyBox", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
std::cout << "Failed to create GLFW window." << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
// Set the resize callback;
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
// Setup the glad loader
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initalize GLAD." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
i32 success;
|
||||
char infoLog[512];
|
||||
|
||||
// Create the vertex shader
|
||||
char *shdr_src_vertex = readshader("vertex.glsl");
|
||||
u32 vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &shdr_src_vertex, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
freeshader(shdr_src_vertex);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR SHADER::VERTEX::Compilation Failed\n" <<
|
||||
infoLog << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Create the fragment shader
|
||||
char *shdr_src_fragment = readshader("fragment.glsl");
|
||||
u32 fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &shdr_src_fragment, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
freeshader(shdr_src_fragment);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR SHADER::FRAGMENT::Compilation Failed\n" <<
|
||||
infoLog << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Create the shader program
|
||||
u32 shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||
std::cout << "ERROR SHADER::PROGRAM::Linking Failed\n" <<
|
||||
infoLog << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Free the old shader bits
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
// Define the vertex shader dat.
|
||||
f32 vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // tr
|
||||
0.5f, -0.5f, 0.0f, // br
|
||||
-0.5f, -0.5f, 0.0f, // bl
|
||||
-0.5f, 0.5f, 0.0f // tl
|
||||
};
|
||||
u32 indicies[] {
|
||||
0, 1, 3, // t1
|
||||
1, 2, 3 // t2
|
||||
};
|
||||
|
||||
u32 EBO;
|
||||
u32 VBO;
|
||||
u32 VAO;
|
||||
glGenBuffers(1, &EBO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenVertexArrays(1, &VAO);
|
||||
|
||||
// Bind VAO
|
||||
glBindVertexArray(VAO);
|
||||
// copy in data to the VBO
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
// Setup the indexing into the VBO data
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
|
||||
// Target the Atrib pointer in the VAO to the VBO inside it.
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(f32), (void *)0);
|
||||
// STart at Zero
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
// ??
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
processInput(window);
|
||||
|
||||
// Do some rendering
|
||||
glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// Finish up
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue