YoggieEngine/MyGraphicsEngine/glWindow.c

136 lines
2.8 KiB
C
Raw Normal View History

2021-09-29 18:14:27 +00:00
#include <stdio.h>
#include "glfw3.h"
//#include "spdlog.h"
GLFWwindow * window;
int main (void) {
// spdlog::info("Welcome to spdlog!");
if ( !glfwInit()){
puts("Failed to initialize GLFW!");
return -1;
}
window = glfwCreateWindow( 640 , 480, "BarinkEngine", NULL, NULL);
if( !window){
puts("Failed to create the window");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if(glfwVulkanSupported())
{
puts("Vulkan is supported!");
}
int width,height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0,0, width, height);
// SOURCE: https://learnopengl.com/Getting-started/Hello-Triangle
float vertices[] = {
-0.5, -0.5f, 0.0f,
0.5, -0.5f, 0.0f,
0.5, 0.5f, 0.0f,
};
unsigned int VBO;
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Load the vertex shader program
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main ()\n {\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); \n"
"}\n";
unsigned int fragmentShader ;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader (shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// then set the vertex attributes pointers
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
unsigned int VAO;
glGenVertexArrays(1,&VAO);
glBindVertexArray(VAO);
// use our hader program when we want to render an object
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
while(!glfwWindowShouldClose(window)){
/* Start rendering here */
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glfwTerminate();
return 0;
}