PDA

View Full Version : how to



technologist
06-14-2017, 11:40 AM
Ive got a decent amt of c++ knowledge but lately I am being humbled by trying to get n example out of a Redbook to work. Its 8.13 and I've typed it in exactly -- yet the compiler complains like there is no tomorrow. Even inserting the #version 330 core causes an error. The settings for the project are the same had they been a typical C++ glut or freeglut problem prior to the Redbook examples. I've searched the net for 8.13 and I am not seeing it. I am get kneecapped at the outset here: can anyone help?

Dark Photon
06-14-2017, 05:20 PM
...example out of a Redbook ... Its 8.13 ... I've searched the net for 8.13 and I am not seeing it.

Which version of the OpenGL Programming Guide (https://www.amazon.com/OpenGL-Programming-Guide-Official-Learning/dp/0134495497/ref=sr_1_1/130-6428016-9170334?ie=UTF8&qid=1497485820&sr=8-1&keywords=opengl+programming+guide) are you referring to? 9th? 8th?

And what's the name of the example? "Source Code for an Antialiased Checkerboard Fragment Shader"? If so, there's quite a few hits for that on the net. Seach for the name of the example rather than the number.

technologist
06-15-2017, 11:36 AM
Which version of the OpenGL Programming Guide (https://www.amazon.com/OpenGL-Programming-Guide-Official-Learning/dp/0134495497/ref=sr_1_1/130-6428016-9170334?ie=UTF8&qid=1497485820&sr=8-1&keywords=opengl+programming+guide) are you referring to? 9th? 8th?

And what's the name of the example? "Source Code for an Antialiased Checkerboard Fragment Shader"? If so, there's quite a few hits for that on the net. Search for the name of the example rather than the number.

Thx for the reply. I have found much information but none that will help me compile the bottom code. I've tried .c thinking it was a .cpp issue/.c issue but no success at all. Why can't I compile this? 4.3 8th edition, paperback.


#version 330 core

uniform vec3 Color0; uniform vec3Color1; uniform vec3AvgColor; uniform floatFrequency;
in vec2 TexCoord;
outvec4 FragColor;
void main()
{
vec3 color;

//Determine the width of the projection of one pixel into
//s-tspace
vec2 fw = fwidth(TexCoord);

// Determine the amount of fuzziness vec2 fuzz = fw* Frequency* 2.0;
float fuzzMax = max(fuzz.s, fuzz.t);

// Determine the position in the checkerboard pattern vec2 checkPos = fract(TexCoord* Frequency);
if (fuzzMax < 0.5)
{
//If the filter width is small enough,
//compute the pattern color
vec2 p = smoothstep(vec2(0.5), fuzz +vec2(0.5), checkPos) + (1.0 - smoothstep(vec2(0.0), fuzz, checkPos));
color = mix(Color0, Color1,
p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
// Fade in the average color when we get close to the limit color=mix(color,AvgColor,smoothstep(0.125,0.5,fuzz Max));
}
else
{
// Otherwise, use only the average color color = AvgColor;
}
FragColor = vec4(color, 1.0);
}

YardenJ2R
06-15-2017, 02:03 PM
What is the compiler complaining exactly?



outvec4 FragColor;


You need to put a space between "out" and "vec4". Also, there are some comments that you put that are mixed with some code, including variables declarations. Is the file in that way too or only here? It shouldn't be like that. For example,



// Determine the amount of fuzziness vec2 fuzz = fw* Frequency* 2.0;


shoud be



// Determine the amount of fuzziness
vec2 fuzz = fw* Frequency* 2.0;

Dark Photon
06-16-2017, 04:59 AM
technologist, YardenJ2R's put you on the right track. For some reason, your shader is missing some space and EOL characters. With the changes he's suggested plus a few more, this compiles:


#version 330 core

uniform vec3 Color0;
uniform vec3 Color1;
uniform vec3 AvgColor;
uniform float Frequency;
in vec2 TexCoord;
out vec4 FragColor;

void main()
{
vec3 color;

//Determine the width of the projection of one pixel into
//s-tspace
vec2 fw = fwidth(TexCoord);

// Determine the amount of fuzziness
vec2 fuzz = fw* Frequency* 2.0;
float fuzzMax = max(fuzz.s, fuzz.t);

// Determine the position in the checkerboard pattern
vec2 checkPos = fract(TexCoord* Frequency);
if (fuzzMax < 0.5)
{
//If the filter width is small enough,
//compute the pattern color
vec2 p = smoothstep(vec2(0.5), fuzz +vec2(0.5), checkPos) + (1.0 - smoothstep(vec2(0.0), fuzz, checkPos));
color = mix(Color0, Color1,
p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
// Fade in the average color when we get close to the limit color=mix(color,AvgColor,smoothstep(0.125,0.5,fuzz Max));
}
else
{
// Otherwise, use only the average color color = AvgColor;
}
FragColor = vec4(color, 1.0);
}


Did a quick compile test here with cgc.

technologist
06-16-2017, 07:27 AM
Ok, so I think we have established that the code is good. Mine still isn't compiling (posted below). Mybe a build setting, I don't know.

..\src\main.cpp:7:2: error: invalid preprocessing directive #version
#version 330 core
^
In file included from <command-line>:0:0:
C:\soft_glut_binary\glut-3.7.6-bin\glut.h:50:24: error: redeclaration of C++ built-in type 'wchar_t' [-fpermissive]
typedef unsigned short wchar_t;
^
..\src\main.cpp:9:1: error: 'uniform' does not name a type
uniform vec3 Color0;
^
..\src\main.cpp:10:1: error: 'uniform' does not name a type
uniform vec3 Color1;
^
..\src\main.cpp:11:1: error: 'uniform' does not name a type
uniform vec3 AvgColor;
^
..\src\main.cpp:12:1: error: 'uniform' does not name a type
uniform float Frequency;
^
..\src\main.cpp:13:1: error: 'in' does not name a type
in vec2 TexCoord;
^
..\src\main.cpp:14:1: error: 'out' does not name a type
out vec4 FragColor;
^
..\src\main.cpp:16:11: error: '::main' must return 'int'
void main()
^
..\src\main.cpp: In function 'int main()':
..\src\main.cpp:18:5: error: 'vec3' was not declared in this scope
vec3 color;
^
..\src\main.cpp:22:5: error: 'vec2' was not declared in this scope
vec2 fw = fwidth(TexCoord);
^
..\src\main.cpp:25:10: error: expected ';' before 'fuzz'
vec2 fuzz = fw* Frequency* 2.0;
^
..\src\main.cpp:26:25: error: 'fuzz' was not declared in this scope
float fuzzMax = max(fuzz.s, fuzz.t);
^
..\src\main.cpp:26:39: error: 'max' was not declared in this scope
float fuzzMax = max(fuzz.s, fuzz.t);
^
..\src\main.cpp:29:10: error: expected ';' before 'checkPos'
vec2 checkPos = fract(TexCoord* Frequency);
^
..\src\main.cpp:34:14: error: expected ';' before 'p'
vec2 p = smoothstep(vec2(0.5), fuzz +vec2(0.5), checkPos) + (1.0 - smoothstep(vec2(0.0), fuzz, checkPos));
^
..\src\main.cpp:35:9: error: 'color' was not declared in this scope
color = mix(Color0, Color1,
^
..\src\main.cpp:35:21: error: 'Color0' was not declared in this scope
color = mix(Color0, Color1,
^
..\src\main.cpp:35:29: error: 'Color1' was not declared in this scope
color = mix(Color0, Color1,
^
..\src\main.cpp:36:21: error: 'p' was not declared in this scope
p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
^
..\src\main.cpp:36:57: error: 'mix' was not declared in this scope
p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
^
..\src\main.cpp:43:5: error: 'FragColor' was not declared in this scope
FragColor = vec4(color, 1.0);
^
..\src\main.cpp:43:22: error: 'color' was not declared in this scope
FragColor = vec4(color, 1.0);
^
..\src\main.cpp:43:32: error: 'vec4' was not declared in this scope
FragColor = vec4(color, 1.0);
^
In file included from <command-line>:0:0:
C:\soft_glut_binary\glut-3.7.6-bin\glut.h: At global scope:
C:\soft_glut_binary\glut-3.7.6-bin\glut.h:486:22: warning: 'void glutInit_ATEXIT_HACK(int*, char**)' defined but not used [-Wunused-function]
static void APIENTRY glutInit_ATEXIT_HACK(int *argcp, char **argv) { __glutInitWithExit(argcp, argv, exit); }
^
C:\soft_glut_binary\glut-3.7.6-bin\glut.h:503:21: warning: 'int glutCreateWindow_ATEXIT_HACK(const char*)' defined but not used [-Wunused-function]
static int APIENTRY glutCreateWindow_ATEXIT_HACK(const char *title) { return __glutCreateWindowWithExit(title, exit); }
^
C:\soft_glut_binary\glut-3.7.6-bin\glut.h:549:21: warning: 'int glutCreateMenu_ATEXIT_HACK(void (__attribute__((__cdecl__)) *)(int))' defined but not used [-Wunused-function]
static int APIENTRY glutCreateMenu_ATEXIT_HACK(void (GLUTCALLBACK *func)(int)) { return __glutCreateMenuWithExit(func, exit); }
^

10:22:19 Build Finished (took 137ms)

larryl
06-16-2017, 09:48 AM
Technologist, I'm a newbie like you. I'm sure someone who knows what they're talking about will reply to this thread, but I'll post this so you can get started a little sooner.

Somebody will let us both know if I'm full of crap.

OpenGL's shader programs are written in GLSL, not C.

It's similar to C. I'm assuming that's to make it familiar for C and C++ programmers.

This (https://learnopengl.com/#!Getting-started/Hello-Triangle) tutorial introduces the concept, and should get you going. The next lesson introduces a nice utility that handles a shader's code in its own individual file.

Spoiler: The vertex and fragment shaders aren't compiled by the IDE you're using to build your C++ application and they don't run on your CPU. They're built, error checked and linked by some functions your video card vendor installed on your computer along with their driver. The shaders live and run on your GPU. I believe they're the programs running in parallel on those thousands of processors you have on your GPU.

It sounds to me like you would benefit from going through at least the early parts of the tutorial series.

WhiteSword
06-16-2017, 10:40 AM
Ok, so I think we have established that the code is good. Mine still isn't compiling (posted below). Mybe a build setting, I don't know.10:22:19 Build Finished (took 137ms)

If I'm not wrong then the C++ compiler is considering your Shader code as C++ code, and hence reporting errors. So, I suspect some error in handling the Shader source file.
Could you post your c/c++ code you are using to compile this shader, and also are you reading this Shader code from some file or some string inside your program?

technologist
06-16-2017, 11:28 AM
If I'm not wrong then the C++ compiler is considering your Shader code as C++ code, and hence reporting errors. So, I suspect some error in handling the Shader source file.
Could you post your c/c++ code you are using to compile this shader, and also are you reading this Shader code from some file or some string inside your program?

Could be. I will post whatever code (link below) I had at the top before Larry's response. Larry was right, as well as some other folks: this code is processed by GLSL:

I also literally bumped on this by accident: https://learnopengl.com/#!Getting-started/Shaders

It appears we have to learn a new total language. I am experiencing a lot of confusion abt my next move. Is this the right and only place to commit learning openGl: I don't know.

Right now I am also following GLFW tutorials and both are requiring I learn GLSL...could be cool

But have now come to a crossroads. I think for sure I need to back up and look at the viability of what's needed toprogram.

http://www.studfiles.ru/preview/5125756/page:93/

Which leaves me to the basic question is which resource most appropriate to learn OpenGl. Is there a language whose spoiler attribute isn't huge. I have performance demands.

Where do I "place my bets" so to speak to get the correct openGL education out of this.

technologist
06-16-2017, 11:38 AM
Update:


This section, OpenGL Setup for GLSL, assumes you’ve got a pair of shaders, a vertex shader and a fragment shader, and you want to use them in an OpenGL application. If you’re not ready yet to write your own shaders there are plenty of places to get shaders from the internet. Try the site from the Orange Book. The tools for shader development, namely Shader Designer or Render Monkey, all have a lot of shader examples.

So I am not sure I am ready to tackle vertex and fragment shaders, YET, or should I just swallow the bitter pill and get to it?


Each shader is like a C module, and it must be compiled separately, as in C. The set of compiled shaders, is then linked into a program, exactly as in C

So where do I go from here->

where does basic OpenGl taper off, and shaders and fragments begin? For example would I necessarily need shaders and fragments with simple geometrical shapes being translated and rotated in space?

technologist
06-16-2017, 11:44 AM
Thanks Dark

Your compile test was with ?

larryl
06-16-2017, 03:35 PM
If I'm not wrong then the C++ compiler is considering your Shader code as C++ code, and hence reporting errors. So, I suspect some error in handling the Shader source file.

Your C++ compiler builds your C++ into machine code for your CPU.

Your GLSL compiler, which has nothing to do with any part of your C++ development environment, turns your GLSL into code for your GPU. The people who made your GPU, Nvidia, AMD or Intel, wrote your GLSL compiler and packaged it with their drivers, which you had to install to make your card work with your operating system.

You create a shader with these C++ statements, in your application:


unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);

The fragment shader is created like this:


unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

You associate your shader source code, the stuff your C++ compiler doesn't like, with your new shader like this:


glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

And compile it like this:


glCompileShader(vertexShader);

You repeat those steps for the fragment shader.

You should then check to see if the shader compiler found errors.

Moderately complex models, vehicles and environment, can easily have tens of thousands of vertices. And a scene can have many models to render in less than 1/60 of a second.

You link the shaders into a shader program, which is run in parallel against all those vertices on the thousands of cores you paid for when you bought that fancy GPU.

The link (https://learnopengl.com/#!Getting-started/Hello-Triangle) I posted in my previous response explains it better than I can.

The point is that Nvidia wrote these functions for their GPU, AMD wrote them for theirs and Intel wrote them for their on chip GPU.

Because GLSL isn't C++ your C++ compiler will always find errors in perfectly legal GLSL code. It's a nice familiar C like language. But it's not C or C++.

Every GLSL legal shader program, vertex or fragment, and there's something called a geometry shader if your not confused enough already, requires a pre-processor looking statement like:


#version 330 core

No C++ compiler I've ever heard of is going to like that statement.

larryl
06-16-2017, 04:13 PM
I also literally bumped on this by accident: https://learnopengl.com/#!Getting-started/Shaders

Yea, that's the lesson after the one I linked to. The lesson I linked is a first exposure to the concept of shaders.


It appears we have to learn a new total language. I am experiencing a lot of confusion abt my next move. Is this the right and only place to commit learning openGl: I don't know.

First, RELAX. GLSL is almost C, you almost already know it.


Right now I am also following GLFW tutorials and both are requiring I learn GLSL...could be cool

But have now come to a crossroads. I think for sure I need to back up and look at the viability of what's needed toprogram.

http://www.studfiles.ru/preview/5125756/page:93/

Which leaves me to the basic question is which resource most appropriate to learn OpenGl. Is there a language whose spoiler attribute isn't huge. I have performance demands.

Where do I "place my bets" so to speak to get the correct openGL education out of this.

The link you have is to a set of tutorials that are well regarded in this forum. Start there, at the first lesson.


So I am not sure I am ready to tackle vertex and fragment shaders, YET, or should I just swallow the bitter pill and get to it?

So where do I go from here->

where does basic OpenGl taper off, and shaders and fragments begin? For example would I necessarily need shaders and fragments with simple geometrical shapes being translated and rotated in space?

In modern OpenGL your very first rendering will be with your very first C++ OpenGL code on your very first shaders, written in GLSL.

In other words, you can't start rendering simple objects in OpenGL, then ease your way into GLSL shader code after you become familiar with the environment.

In my simplified newbie view of OpenGL, the vertex shader figures out where on your 2D screen a point in 3D space is drawn. The fragment shader figures out what color to draw it.

If you don't provide those shaders nothing is drawn, period.

Fortunately these can be simple shader programs written in a language that will feel very familiar.

Relax, Dark Photon, John Connor, mhagain, Silence, GClements and many others are here to help. I'd be nowhere without their guidance.

GClements
06-16-2017, 05:11 PM
where does basic OpenGl taper off, and shaders and fragments begin? For example would I necessarily need shaders and fragments with simple geometrical shapes being translated and rotated in space?
"Modern" OpenGL requires shaders to do anything. "Legacy" OpenGL has the fixed-function pipeline, which is effectively a default shader program (a fairly large and complex one, with many variables).

It is possible to mix the two to an extent, but that adds even more complexity; you not only have to learn both types, but also how they interact. At this time, there's not much point in learning legacy OpenGL unless you specifically need to be able to understand existing code which uses it.

Dark Photon
06-16-2017, 06:55 PM
Thanks Dark. Your compile test was with ?

As a newbie, this isn't likely to be useful to you. But just in case you really want to know...

NVidia had another GPU shading language for a long while called Cg. It was like GLSL syntax-wise, but unlike GLSL in that GLSL is natively supported by OpenGL drivers and Cg was not.

Their Cg Toolkit (https://developer.nvidia.com/cg-toolkit-download) has a command-line compiler (called cgc) that'll compile Cg shaders down into assembly shaders. They also added support to cgc to compile GLSL shaders too. So for years and years, cgc provided a simple way to do a quick compile-test on GLSL shaders to make sure they were syntactically valid, without having to write a short C++ OpenGL program to feed the GLSL shader to the GL driver to compile it to find out if the shader was valid.

That said, NVidia discontinued support for and releases of the Cg Toolkit back in 2012, so the latest cgc doesn't support the newest GLSL syntax added in the latest OpenGL versions, and so GLSL shaders using the newest syntax won't compile with cgc.

In other words, this is probably of no real use for you.

technologist
06-17-2017, 11:20 AM
ok, I've "calmed down" : )
I need a consistent way/program to write shader language. It would help if it were cut paste friendly initially for red book. One of the examples I was using used char pointer to string language. I was able to run an example off of it this way. But I couldn't get it to work for "uniform", etc.

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";
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\0";

I tried to expand this strategy to other SHADER programs but without luck.

john_connor
06-17-2017, 12:37 PM
So where do I go from here->

where does basic OpenGl taper off, and shaders and fragments begin? For example would I necessarily need shaders and fragments with simple geometrical shapes being translated and rotated in space?

regarding learning openGL:
there's a site with that name, https://www.learnopengl.com

it tells you everything you need to know (theory and practice) to get started

there are a bunch of other sites with useful information on "how to" get started ... i've made a list of some on my site: https://sites.google.com/site/john87connor

on my site you can also find simple / advanced examples which work well with the "copy & paste" approach as long as your system supports openGL 4.5, but almost no theory is found there, theory you can read in a book of your choice

the most important thing about openGL / general graphics programming is to understand the "graphics pipeline" and how the data flows: what happens to the "numbers" you've just put into a buffer, how are primitives (faces / lines / points etc) generated out of "numbers", and what does the "rasterizer", what does the fragment shader, what / where is the "framebuffer", double buffering, a bit of vector / matrix math and so on ...

=> much stuff to learn before you understand how the simplest example on any red book works

just my opinion:
start with this:
https://www.youtube.com/user/thebennybox
https://www.youtube.com/watch?v=ftiKrP3gW3k&list=PUnlpv-hhcsAtEHKR2y2fW4Q
everything you dont understand there, read about it in the wiki:
https://www.khronos.org/opengl/wiki

when you're done with them, start over with these:
https://learnopengl.com
and read a book while you're doing them
if you encounter some problems along the way, ask questions here


by the way:
shaders are usually written into separate text files, and the application reads those files and provides the string to openGL, but you can of course place the shader source directly in your application code


and regarding c++:
https://www.khronos.org/opengl/wiki/Common_Mistakes#The_Object_Oriented_Language_Probl em

GClements
06-17-2017, 01:11 PM
It's usually better to read shaders from text files. It makes it easier to edit shaders, avoids the need to recompile the program whenever you make changes to a shader, and allows you to modify and reload shaders without restarting the program.

Dark Photon
06-17-2017, 02:00 PM
I need a consistent way/program to write shader language. It would help if it were cut paste friendly initially for red book. One of the examples I was using used char pointer to string language. I was able to run an example off of it this way. But I couldn't get it to work for "uniform", etc.


const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
...



Yeah, that syntax for encoding GLSL shaders in C++ code is definitely possible, but it makes your shaders an unreadable mess.

I'd also suggest just putting your shaders in a separate file and loading them in as it's easy. However, for small GLSL test shaders, if you want to stick them in the C++ code inline, use some C++11 raw string magic like this:


static const char VERT_SHADER[] = R"MAGIC(
#version 330

in vec3 vertex;
out vec3 pos;

void main(void)
{
pos = vertex;
};

)MAGIC";

larryl
06-17-2017, 06:32 PM
Technologist and WhiteSword, I hope, as you follow this thread, you're working your way through the set of tutorials that keep getting linked. Basic shader code is covered in the early lessons.

This (https://www.learnopengl.com/#!Getting-started/Shaders) particular tutorial, the second and more detailed one that deals with shaders, includes a header with all the code for a shader class that facilitates reading, compiling, linking and using shader programs. "Linking" refers to combining vertex and fragment shaders into a single shader program, nothing to do with linking object files.

The time you're spending trying to figure out what to do might be better spent on those tutorials.

I've had lots of questions while following those tutorials and have received a lot of assistance from this forum.

You're particularly interested in code sample 8.13 of the Red Book. Following the tutorials through the "Coordinate Systems" section will probably make you better prepared to tackle it than you are now.

technologist
06-21-2017, 02:27 PM
Thank you to ALL for your input. I was the lucky recipient of advice that made me take a few shortcuts but without compromising the learning aspect and make some smart choices in my opinion regarding software choices. This is really important because lack of research can burn you way under way down the road.