Writing a Shader in OpenGL

preview_player
Показать описание


Thank you to the following Patreon supporters:
- Samuel Egger
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig

Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

A 1000$ value course for free by this guy. Respect.
Massive help for my students.

StarContract
Автор

Nice my dumb ass hitting F5 to recompile while focus is on the youtube window

Netrole
Автор

C++11 introduced raw string literals. Really nice for multiline string definitions.

const std::string vs = R"glsl(
#version 330 core

layout(location = 0) in vec4 position;

void main(){
gl_Position = position;
}
)glsl";

Phidelux
Автор

Modern OpenGL requires a VAO be defined and bound if you are using the core profile. Add `unsigned int VAO; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO);` to your code, otherwise you will draw nothing. Otherwise, watch up to his Vertex Arrays video.

tomashaddad
Автор

Hey guys just a tip if you dont want to type \n or the "" C++ supports multiline strings using an opening and closing delimeter like so R "delimiter( raw_characters )delimiter" (6) (since C++11)

it would look something like this in code ill use myDelim just to make it clear you can type whatever you want as a delimiter

std::string vertexShader = R"myDelim(
#version 330 core
layout(location = 0)in vec4 position;
void main(){
gl_Position = position;
})myDelim";

I actually dont use anything as a delimiter because its pointless but i thought id show you the proper way
so its like
const *char = R"(
my
multiline
string
)"

which means then you can just get crazy and do this if you wanted to move that all to a .shader file, instead of reading it in

const char *fragmentShader =
#include "VertexShader.shader";

and the file would look like

R"(
#version 330 core
layout(location = 0)in vec4 position;
void main(){
gl_Position = position;
}
)";

man i hope this helps someone at least..

mycollegeshirt
Автор

Make sure to define a VAO in your code, I'm on windows and I couldn't make the triangle appears, apparently it's required if you use openGL core. Hopefully it will be useful to someone even if the video is 5 years old ( best explanation of openGL I've found so far )

andreadws
Автор

Cherno: "We're good though, we're kinda above this level of comedy..." - this episode made me laugh too many times :D

adamodimattia
Автор

His explanation about these functions in OpenGL are so good :)

viniciusmachado
Автор

I mean, this is the most complex stuff I've ever seen, just to show a triangle on the screen. Jees.

omri
Автор

Don't be like me. I used GL_VERTEX_SHADER instead of GL_FRAGMENT_SHADER for the fragment shader, fs, when I did:

unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

All I saw was a white triangle and it gave no error messages. It took me like 30 minutes to find out what went wrong.

stevenismart
Автор

For anyone watching using VS 2019 and using those libs, change your fragment core to 410

kinochdotcom
Автор

Apparently, for this code to work on Mac, you also need to create a VAO and bind it, otherwise you will get a black screen with no triangle.

Like this:
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

Levendo
Автор

"We're above this level of comedy"

necrondow_
Автор

The way I'd setup the message buffer is to use a std::string, set up as follows:
std::string message(length, ' ');
Then to pass this use either:
glGetShaderInfoLog(id, length, &length, message.data()); // In C++17 onwards
OR
glGetShaderInfoLog(id, length, &length, &message.front()); // In C++14 or prior

Great series though!

marinedalek
Автор

Hope this helps out others in my situation. Spend good 3 hours bashing my head against screen trying to find a solution.

If even after trying everything you triangle is still white then your graphics card is unable to support the latest versions(or in his case 3.3) just like me.

So here's what I did :
- Changed version from 330 to 150.
- Removed any mention of layout, location, in, out.


As it turns out neither location and layout nor in and out is supported in 330 earlier versions.
Here's my modified code if you're just too lazy.

std::string vertexShader =
"#version 150 core\n"
"\n"
"attribute vec4 position;\n"
"void main(){\n"
" gl_Position = position;\n"
"}\n";



// Color values in R, G, B, A
std::string fragmentShader =
"#version 150 core\n"
"\n"
"out vec4 color;\n"
"void main(){\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

w.
Автор

NB : to draw the Triforce of Wisdom version, there are a few important adjustments to be made.


The positions are different

float positions[18] = {
-0.5f, -0.5f,
-0.25f, 0.0f,
0.0f, -0.5f,
-0.25f, 0.0f,
0.0f, 0.5f,
0.25f, 0.0f,
0.0f, -0.5f,
0.25f, 0.0f,
0.5f, -0.5f };


More memory is allocated to the buffer.
glBufferData(GL_ARRAY_BUFFER, *18* *sizeof(float), positions, GL_STATIC_DRAW);


The array draws 9 points instead of 3.
glDrawArrays(GL_TRIANGLES, 0, 9);


Thank You Intel.

CoolDude
Автор

Really great video man, I bought the opengl "red book" but they skip over a lot of things and don't properly explain them and the sample code just makes use of a bunch of their own library functions that are never explained. Your videos are really helping me to piece everything together.

shorthouse
Автор

Amazing Course !
As a beginner, it was effortless to understand. Helped me a lot

hackatech
Автор

How to use VBO without VAO ?
In this video you use only VBO, but i can't . The screen does not render.

atnguyenquoc
Автор

Just in case anyone had the same error as me, "assignment to varying in color". Turned out I had forgotten to change 'in' to 'out' in the fragment shader. Pretty easy mistake to make when copy/pasting. But wasn't easy to figure out exactly what I'd done wrong. It produced some other cryptic errors as well, that went away when I changed this.

mikeyparsons