move shaders to files
This commit is contained in:
parent
16c0ce8fcc
commit
b0a3bc3f3d
4 changed files with 34 additions and 23 deletions
4
README.md
Normal file
4
README.md
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# Opengl-Starter
|
||||||
|
|
||||||
|
This is a template for developing an OpenGL based application in Rust.
|
||||||
|
It targets OpenGL Core 4.6 on Linux.
|
6
data/main.frag
Normal file
6
data/main.frag
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#version 460 core
|
||||||
|
in vec3 ourColor;
|
||||||
|
out vec4 FragColor;
|
||||||
|
void main() {
|
||||||
|
FragColor = vec4(ourColor, 1.0);
|
||||||
|
}
|
8
data/main.vert
Normal file
8
data/main.vert
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#version 460 core
|
||||||
|
layout(location = 0) in vec3 aPos;
|
||||||
|
layout(location = 1) in vec3 aColor;
|
||||||
|
out vec3 ourColor;
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
}
|
39
src/main.rs
39
src/main.rs
|
@ -7,14 +7,14 @@
|
||||||
use glow::{Context, HasContext, NativeProgram, NativeVertexArray};
|
use glow::{Context, HasContext, NativeProgram, NativeVertexArray};
|
||||||
use glutin::{
|
use glutin::{
|
||||||
config::{ConfigSurfaceTypes, ConfigTemplateBuilder},
|
config::{ConfigSurfaceTypes, ConfigTemplateBuilder},
|
||||||
context::{ContextAttributesBuilder, GlProfile, PossiblyCurrentContext},
|
context::{ContextApi, ContextAttributesBuilder, GlProfile, PossiblyCurrentContext, Version},
|
||||||
display::GetGlDisplay,
|
display::GetGlDisplay,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
surface::{Surface, SurfaceAttributesBuilder, WindowSurface},
|
surface::{Surface, SurfaceAttributesBuilder, WindowSurface},
|
||||||
};
|
};
|
||||||
use glutin_winit::DisplayBuilder;
|
use glutin_winit::DisplayBuilder;
|
||||||
use raw_window_handle::HasRawWindowHandle;
|
use raw_window_handle::HasRawWindowHandle;
|
||||||
use std::{error::Error, ffi::CString};
|
use std::{error::Error, ffi::CString, fs::File, io::Read, path::Path};
|
||||||
use winit::{
|
use winit::{
|
||||||
application::ApplicationHandler,
|
application::ApplicationHandler,
|
||||||
event::WindowEvent,
|
event::WindowEvent,
|
||||||
|
@ -97,6 +97,16 @@ impl ApplicationHandler for App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let mut vert_shader_file =
|
||||||
|
File::open(Path::new("./data/main.vert")).expect("Didn't find vertex shader");
|
||||||
|
let mut vert_shader_src = "".to_string();
|
||||||
|
vert_shader_file.read_to_string(&mut vert_shader_src)?;
|
||||||
|
|
||||||
|
let mut frag_shader_file =
|
||||||
|
File::open(Path::new("./data/main.frag")).expect("Didn't find fragment shader");
|
||||||
|
let mut frag_shader_src = "".to_string();
|
||||||
|
frag_shader_file.read_to_string(&mut frag_shader_src)?;
|
||||||
|
|
||||||
// Create an event loop
|
// Create an event loop
|
||||||
let event_loop = EventLoop::new().unwrap();
|
let event_loop = EventLoop::new().unwrap();
|
||||||
event_loop.set_control_flow(ControlFlow::Poll);
|
event_loop.set_control_flow(ControlFlow::Poll);
|
||||||
|
@ -133,8 +143,9 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
// Create OpenGL context attributes
|
// Create OpenGL context attributes
|
||||||
let context_attributes = ContextAttributesBuilder::new()
|
let context_attributes = ContextAttributesBuilder::new()
|
||||||
|
.with_context_api(ContextApi::OpenGl(Some(Version::new(4, 6))))
|
||||||
.with_profile(GlProfile::Core)
|
.with_profile(GlProfile::Core)
|
||||||
.build(None);
|
.build(window.raw_window_handle().ok());
|
||||||
|
|
||||||
// Create the OpenGL surface attributes
|
// Create the OpenGL surface attributes
|
||||||
let surface_attributes = SurfaceAttributesBuilder::<WindowSurface>::new().build(
|
let surface_attributes = SurfaceAttributesBuilder::<WindowSurface>::new().build(
|
||||||
|
@ -177,17 +188,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let vertex_shader = gl
|
let vertex_shader = gl
|
||||||
.create_shader(glow::VERTEX_SHADER)
|
.create_shader(glow::VERTEX_SHADER)
|
||||||
.expect("Cannot create shader");
|
.expect("Cannot create shader");
|
||||||
let vertex_shader_src = r#"
|
gl.shader_source(vertex_shader, &vert_shader_src);
|
||||||
#version 330 core
|
|
||||||
layout (location = 0) in vec3 aPos;
|
|
||||||
layout (location = 1) in vec3 aColor;
|
|
||||||
out vec3 ourColor;
|
|
||||||
void main() {
|
|
||||||
gl_Position = vec4(aPos, 1.0);
|
|
||||||
ourColor = aColor;
|
|
||||||
}
|
|
||||||
"#;
|
|
||||||
gl.shader_source(vertex_shader, vertex_shader_src);
|
|
||||||
gl.compile_shader(vertex_shader);
|
gl.compile_shader(vertex_shader);
|
||||||
|
|
||||||
// Check for shader compilation errors
|
// Check for shader compilation errors
|
||||||
|
@ -200,15 +201,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let fragment_shader = gl
|
let fragment_shader = gl
|
||||||
.create_shader(glow::FRAGMENT_SHADER)
|
.create_shader(glow::FRAGMENT_SHADER)
|
||||||
.expect("Cannot create shader");
|
.expect("Cannot create shader");
|
||||||
let fragment_shader_src = r#"
|
gl.shader_source(fragment_shader, &frag_shader_src);
|
||||||
#version 330 core
|
|
||||||
in vec3 ourColor;
|
|
||||||
out vec4 FragColor;
|
|
||||||
void main() {
|
|
||||||
FragColor = vec4(ourColor, 1.0);
|
|
||||||
}
|
|
||||||
"#;
|
|
||||||
gl.shader_source(fragment_shader, fragment_shader_src);
|
|
||||||
gl.compile_shader(fragment_shader);
|
gl.compile_shader(fragment_shader);
|
||||||
|
|
||||||
// Check for shader compilation errors
|
// Check for shader compilation errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue