Lazarus - OpenGL 3.3 Tutorial - Texturen - Multitexturing: Unterschied zwischen den Versionen
Aus DGL Wiki
(Die Seite wurde neu angelegt: „200px<br><br> =Texturen - Multitexturing = == Einleitung == Multitexturing, tönt Anfan…“) |
|||
Zeile 1: | Zeile 1: | ||
− | [[Image:Lazarus - OpenGL 3.3 Tutorial - Texturen - | + | [[Image:Lazarus - OpenGL 3.3 Tutorial - Texturen - Texturen Perspektiven-Korrektur.png|200px]]<br><br> |
− | =Texturen - | + | =Texturen - Texturen Perspektiven-Korrektur = |
== Einleitung == | == Einleitung == | ||
− | + | Für sehr einfache Texturen, ist das xpm-Format geeignet. Mit diesem kann man sehr schnell eine einfache Textur mit einem Text-Editor erstellen.<br> | |
− | + | <br><br> | |
− | + | Da etwas anderes als '''BMP''' gleaden wird, muss anstelle von '''TBitmap TPicture''' verwendet werden.<br> | |
<br> | <br> | ||
− | + | Momentan kann TPicture folgende Datei-Formate laden: '''BMP, GIF, JPG, PCX, PNG, P?M, PDS, TGA, TIF, XPM, ICO, CUR, ICNS'''.<br> | |
− | + | <syntaxhighlight lang="pascal">procedure TForm1.InitScene; | |
− | + | var | |
− | <syntaxhighlight lang="pascal">var | + | pic: TPicture; |
− | + | i: integer; | |
− | |||
− | |||
− | |||
begin | begin | ||
− | + | glClearColor(0.6, 0.6, 0.4, 1.0); | |
− | Textur[ | + | |
+ | glBindVertexArray(VBO_Trapeze.VAO); | ||
+ | |||
+ | // Vektoren Trapez | ||
+ | glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Vertex); | ||
+ | glBufferData(GL_ARRAY_BUFFER, sizeof(TrapezeVertex), @TrapezeVertex, GL_STATIC_DRAW); | ||
+ | glEnableVertexAttribArray(0); | ||
+ | glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, nil); | ||
+ | |||
+ | // Unkorrigierte Textur-Koordinaten | ||
+ | glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Textur[0]); | ||
+ | glBufferData(GL_ARRAY_BUFFER, sizeof(TextureNormalVertex), @TextureNormalVertex, GL_STATIC_DRAW); | ||
+ | glEnableVertexAttribArray(10); | ||
+ | glVertexAttribPointer(10, 2, GL_FLOAT, False, 0, nil); | ||
− | + | // Perspektivenkorrigiert Variante 1 | |
− | + | glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Textur[1]); | |
− | + | glBufferData(GL_ARRAY_BUFFER, sizeof(TexturePerspVertex1), @TexturePerspVertex1, GL_STATIC_DRAW); | |
− | + | glEnableVertexAttribArray(11); | |
− | + | glVertexAttribPointer(11, 3, GL_FLOAT, False, 0, nil); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // Perspektivenkorrigiert Variante 2 | |
− | Textur[ | + | glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Textur[2]); |
− | + | glBufferData(GL_ARRAY_BUFFER, sizeof(TexturePerspVertex2), @TexturePerspVertex2, GL_STATIC_DRAW); | |
− | + | glEnableVertexAttribArray(12); | |
− | + | glVertexAttribPointer(12, 4, GL_FLOAT, False, 0, nil);</syntaxhighlight> | |
− | |||
− | |||
<br><br> | <br><br> | ||
==Vertex-Shader:== | ==Vertex-Shader:== | ||
<syntaxhighlight lang="glsl">#version 330 | <syntaxhighlight lang="glsl">#version 330 | ||
− | layout (location = 0) in vec3 inPos; | + | layout (location = 0) in vec3 inPos; // Vertex-Koordinaten |
layout (location = 10) in vec2 inUV0; // Textur-Koordinaten | layout (location = 10) in vec2 inUV0; // Textur-Koordinaten | ||
− | layout (location = 11) in | + | layout (location = 11) in vec3 inUV1; // Textur-Koordinaten |
+ | layout (location = 12) in vec4 inUV2; // Textur-Koordinaten | ||
uniform mat4 mat; | uniform mat4 mat; | ||
− | out vec2 UV0; | + | out Data { |
− | out | + | vec2 UV0; |
+ | out vec3 UV1; | ||
+ | out vec4 UV2; | ||
+ | } DataOut; | ||
void main(void) | void main(void) | ||
{ | { | ||
gl_Position = mat * vec4(inPos, 1.0); | gl_Position = mat * vec4(inPos, 1.0); | ||
− | UV0 = inUV0; // Textur-Koordinaten weiterleiten. | + | DataOut.UV0 = inUV0; // Textur-Koordinaten weiterleiten. |
− | UV1 = inUV1; // Textur-Koordinaten weiterleiten. | + | DataOut.UV1 = inUV1, // Textur-Koordinaten weiterleiten. |
+ | DataOut.UV2 = inUV2; // Textur-Koordinaten weiterleiten. | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
==Fragment-Shader:== | ==Fragment-Shader:== | ||
− | |||
− | |||
<syntaxhighlight lang="glsl">#version 330 | <syntaxhighlight lang="glsl">#version 330 | ||
− | in vec2 UV0; | + | in Data { |
− | in | + | vec2 UV0; |
+ | in vec3 UV1; | ||
+ | in vec4 UV2; | ||
+ | } DataIn; | ||
− | uniform sampler2D Sampler | + | uniform sampler2D Sampler; // Textursampler |
+ | uniform int variante; // Variante der Texturberechnung | ||
out vec4 FragColor; | out vec4 FragColor; | ||
Zeile 87: | Zeile 81: | ||
void main() | void main() | ||
{ | { | ||
− | FragColor = | + | switch (variante) { |
− | + | ||
+ | // Unkorrigiert | ||
+ | case 0: FragColor = texture( Sampler, DataIn.UV0 ); | ||
+ | break; | ||
+ | |||
+ | // Korrigiert Variante 1 | ||
+ | case 1: FragColor = texture( Sampler, DataIn.UV1.xy / DataIn.UV1.z ); | ||
+ | break; | ||
+ | |||
+ | // Korrigiert Variante 2 | ||
+ | case 2: FragColor = texture2DProj( Sampler, DataIn.UV2 ); | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
==mauer.xpm:== | ==mauer.xpm:== | ||
− | <syntaxhighlight | + | <syntaxhighlight lang="cpp">/* XPM */ |
− | |||
− | " | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
static char *XPM_mauer[] = { | static char *XPM_mauer[] = { | ||
− | "8 8 2 1", | + | "8 8 2 1", |
− | " c # | + | " c #882222", |
− | "* c # | + | "* c #442222", |
− | "********", | + | "********", |
− | "* * ", | + | "* * ", |
− | "* * ", | + | "* * ", |
− | "* * ", | + | "* * ", |
− | "********", | + | "********", |
− | " * * ", | + | " * * ", |
− | " * * ", | + | " * * ", |
− | " * * " | + | " * * " |
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 21. Januar 2018, 19:05 Uhr
Inhaltsverzeichnis
Texturen - Texturen Perspektiven-Korrektur
Einleitung
Für sehr einfache Texturen, ist das xpm-Format geeignet. Mit diesem kann man sehr schnell eine einfache Textur mit einem Text-Editor erstellen.
Da etwas anderes als BMP gleaden wird, muss anstelle von TBitmap TPicture verwendet werden.
Momentan kann TPicture folgende Datei-Formate laden: BMP, GIF, JPG, PCX, PNG, P?M, PDS, TGA, TIF, XPM, ICO, CUR, ICNS.
procedure TForm1.InitScene;
var
pic: TPicture;
i: integer;
begin
glClearColor(0.6, 0.6, 0.4, 1.0);
glBindVertexArray(VBO_Trapeze.VAO);
// Vektoren Trapez
glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Vertex);
glBufferData(GL_ARRAY_BUFFER, sizeof(TrapezeVertex), @TrapezeVertex, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, nil);
// Unkorrigierte Textur-Koordinaten
glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Textur[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(TextureNormalVertex), @TextureNormalVertex, GL_STATIC_DRAW);
glEnableVertexAttribArray(10);
glVertexAttribPointer(10, 2, GL_FLOAT, False, 0, nil);
// Perspektivenkorrigiert Variante 1
glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Textur[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(TexturePerspVertex1), @TexturePerspVertex1, GL_STATIC_DRAW);
glEnableVertexAttribArray(11);
glVertexAttribPointer(11, 3, GL_FLOAT, False, 0, nil);
// Perspektivenkorrigiert Variante 2
glBindBuffer(GL_ARRAY_BUFFER, VBO_Trapeze.VBO.Textur[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(TexturePerspVertex2), @TexturePerspVertex2, GL_STATIC_DRAW);
glEnableVertexAttribArray(12);
glVertexAttribPointer(12, 4, GL_FLOAT, False, 0, nil);
Vertex-Shader:
#version 330
layout (location = 0) in vec3 inPos; // Vertex-Koordinaten
layout (location = 10) in vec2 inUV0; // Textur-Koordinaten
layout (location = 11) in vec3 inUV1; // Textur-Koordinaten
layout (location = 12) in vec4 inUV2; // Textur-Koordinaten
uniform mat4 mat;
out Data {
vec2 UV0;
out vec3 UV1;
out vec4 UV2;
} DataOut;
void main(void)
{
gl_Position = mat * vec4(inPos, 1.0);
DataOut.UV0 = inUV0; // Textur-Koordinaten weiterleiten.
DataOut.UV1 = inUV1, // Textur-Koordinaten weiterleiten.
DataOut.UV2 = inUV2; // Textur-Koordinaten weiterleiten.
}
Fragment-Shader:
#version 330
in Data {
vec2 UV0;
in vec3 UV1;
in vec4 UV2;
} DataIn;
uniform sampler2D Sampler; // Textursampler
uniform int variante; // Variante der Texturberechnung
out vec4 FragColor;
void main()
{
switch (variante) {
// Unkorrigiert
case 0: FragColor = texture( Sampler, DataIn.UV0 );
break;
// Korrigiert Variante 1
case 1: FragColor = texture( Sampler, DataIn.UV1.xy / DataIn.UV1.z );
break;
// Korrigiert Variante 2
case 2: FragColor = texture2DProj( Sampler, DataIn.UV2 );
}
}
mauer.xpm:
/* XPM */
static char *XPM_mauer[] = {
"8 8 2 1",
" c #882222",
"* c #442222",
"********",
"* * ",
"* * ",
"* * ",
"********",
" * * ",
" * * ",
" * * "
};
Autor: Mathias
Siehe auch
- Übersichtseite Lazarus - OpenGL 3.3 Tutorial