chore: extract shader compilation functions to file
This commit is contained in:
parent
e9cdefe28a
commit
4400839ae1
|
@ -24,7 +24,12 @@ elseif(APPLE)
|
|||
message(FATAL_ERROR "macOS is not supported yet.")
|
||||
endif()
|
||||
|
||||
add_executable(${PROJECT_NAME} src/hello.cpp src/io.cpp src/triangle.cpp)
|
||||
add_executable(${PROJECT_NAME}
|
||||
src/hello.cpp
|
||||
src/io.cpp
|
||||
src/triangle.cpp
|
||||
src/gfx.cpp
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
#include "gfx.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "io.hpp"
|
||||
|
||||
GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
|
||||
{
|
||||
GLuint shader;
|
||||
GLint success;
|
||||
|
||||
std::string shaderSource = readFile(shaderPath);
|
||||
const char* source = shaderSource.c_str();
|
||||
|
||||
shader = glCreateShader(shaderType);
|
||||
glShaderSource(shader, 1, &source, NULL);
|
||||
glCompileShader(shader);
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
GLchar infoLog[512];
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
std::cerr << "shader compilation failed" << std::endl
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
GLuint compileShaderProgram()
|
||||
{
|
||||
GLuint vertShader = compileShader("./vert.glsl", GL_VERTEX_SHADER);
|
||||
GLuint fragShader = compileShader("./frag.glsl", GL_FRAGMENT_SHADER);
|
||||
|
||||
GLuint shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertShader);
|
||||
glAttachShader(shaderProgram, fragShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||
std::cerr << "shader linking failed" << std::endl
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
// We no longer need the individual shaders
|
||||
glDeleteShader(vertShader);
|
||||
glDeleteShader(fragShader);
|
||||
|
||||
return shaderProgram;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "GL/glew.h"
|
||||
|
||||
GLuint compileShaderProgram();
|
||||
GLuint compileShader(const std::string& shaderPath, GLenum shaderType);
|
|
@ -44,33 +44,9 @@
|
|||
|
||||
#include <chrono>
|
||||
|
||||
#include "io.hpp"
|
||||
#include "gfx.hpp"
|
||||
#include "triangle.hpp"
|
||||
|
||||
GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
|
||||
{
|
||||
GLuint shader;
|
||||
GLint success;
|
||||
|
||||
std::string shaderSource = readFile(shaderPath);
|
||||
const char* source = shaderSource.c_str();
|
||||
|
||||
shader = glCreateShader(shaderType);
|
||||
glShaderSource(shader, 1, &source, NULL);
|
||||
glCompileShader(shader);
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
GLchar infoLog[512];
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
std::cerr << "shader compilation failed" << std::endl
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();
|
||||
float getTime()
|
||||
{
|
||||
|
@ -114,33 +90,6 @@ int initGraphics(GLFWwindow** window)
|
|||
return 0;
|
||||
}
|
||||
|
||||
GLuint getShaderProgram()
|
||||
{
|
||||
GLuint vertShader = compileShader("./vert.glsl", GL_VERTEX_SHADER);
|
||||
GLuint fragShader = compileShader("./frag.glsl", GL_FRAGMENT_SHADER);
|
||||
|
||||
GLuint shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertShader);
|
||||
glAttachShader(shaderProgram, fragShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||
std::cerr << "shader linking failed" << std::endl
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
// We no longer need the individual shaders
|
||||
glDeleteShader(vertShader);
|
||||
glDeleteShader(fragShader);
|
||||
|
||||
return shaderProgram;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Calculate period of ISS orbit around the Earth
|
||||
|
@ -157,7 +106,7 @@ int main()
|
|||
if (initGraphics(&window) != 0)
|
||||
return -1;
|
||||
|
||||
Triangle triangle(getShaderProgram());
|
||||
Triangle triangle(compileShaderProgram());
|
||||
|
||||
// Main loop
|
||||
while (!glfwWindowShouldClose(window))
|
||||
|
|
Loading…
Reference in New Issue