Lazarus - OpenGL 3.3 Tutorial - Shader - Punkte verschieden darstellen: Unterschied zwischen den Versionen
Aus DGL Wiki
(Die Seite wurde neu angelegt: „200px<br><br> =Shader - Punkte verschieden darstellen = == Einleitung == B…“) |
|||
Zeile 2: | Zeile 2: | ||
=Shader - Punkte verschieden darstellen = | =Shader - Punkte verschieden darstellen = | ||
== Einleitung == | == Einleitung == | ||
− | + | Man kann auch Punkte mit dem Shader darstellen, dies kann man auf verschiedene Weise.<br> | |
− | + | Im Fragment-Shader kann man das Zeichen der Punkte manipulieren.<br> | |
− | Man kann | ||
− | |||
− | |||
− | |||
<br><br> | <br><br> | ||
− | Die Deklaration der | + | Die Deklaration der Koordianten und Punktgrösse.<br> |
<syntaxhighlight lang="pascal">var | <syntaxhighlight lang="pascal">var | ||
− | + | Point: array of TVertex2f; | |
− | + | PointSize: array of GLfloat;</syntaxhighlight> | |
− | + | Daten für die Punkte in die Grafikkarte übertragen<br> | |
− | |||
− | |||
<syntaxhighlight lang="pascal">procedure TForm1.InitScene; | <syntaxhighlight lang="pascal">procedure TForm1.InitScene; | ||
begin | begin | ||
glClearColor(0.6, 0.6, 0.4, 1.0); // Hintergrundfarbe | glClearColor(0.6, 0.6, 0.4, 1.0); // Hintergrundfarbe | ||
− | // Daten für | + | // Daten für Punkt Position |
− | glBindVertexArray( | + | glBindVertexArray(VBPoint.VAO); |
− | glBindBuffer(GL_ARRAY_BUFFER, | + | glBindBuffer(GL_ARRAY_BUFFER, VBPoint.VBO); |
− | glBufferData(GL_ARRAY_BUFFER, sizeof(TVertex2f) * Length( | + | glBufferData(GL_ARRAY_BUFFER, sizeof(TVertex2f) * Length(Point), Pointer(Point), GL_STATIC_DRAW); |
glEnableVertexAttribArray(10); | glEnableVertexAttribArray(10); | ||
glVertexAttribPointer(10, 2, GL_FLOAT, False, 0, nil); | glVertexAttribPointer(10, 2, GL_FLOAT, False, 0, nil); | ||
− | // Daten für | + | // Daten für Punkt Grösse |
− | glBindVertexArray( | + | glBindVertexArray(VBPoint.VAO); |
− | glBindBuffer(GL_ARRAY_BUFFER, | + | glBindBuffer(GL_ARRAY_BUFFER, VBPoint.VBO_Size); |
− | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * Length( | + | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * Length(PointSize), Pointer(PointSize), GL_STATIC_DRAW); |
glEnableVertexAttribArray(11); | glEnableVertexAttribArray(11); | ||
glVertexAttribPointer(11, 1, GL_FLOAT, False, 0, nil); | glVertexAttribPointer(11, 1, GL_FLOAT, False, 0, nil); | ||
− | |||
end;</syntaxhighlight> | end;</syntaxhighlight> | ||
− | + | Zeichnen der Punkte<br> | |
<syntaxhighlight lang="pascal">procedure TForm1.ogcDrawScene(Sender: TObject); | <syntaxhighlight lang="pascal">procedure TForm1.ogcDrawScene(Sender: TObject); | ||
const | const | ||
Zeile 45: | Zeile 38: | ||
Shader.UseProgram; | Shader.UseProgram; | ||
− | glBindVertexArray( | + | glBindVertexArray(VBPoint.VAO); |
− | // | + | // gelb |
glUniform1i(PointTyp_ID, 0); | glUniform1i(PointTyp_ID, 0); | ||
− | glUniform3f(Color_ID, 1.0, 1.0, 0.0); | + | glUniform3f(Color_ID, 1.0, 1.0, 0.0); |
glUniform1f(X_ID, -ofs); | glUniform1f(X_ID, -ofs); | ||
glUniform1f(Y_ID, -ofs); | glUniform1f(Y_ID, -ofs); | ||
− | glDrawArrays(GL_POINTS, 0, Length( | + | glDrawArrays(GL_POINTS, 0, Length(Point)); |
− | // | + | // rot |
glUniform1i(PointTyp_ID, 1); | glUniform1i(PointTyp_ID, 1); | ||
− | glUniform3f(Color_ID, 1.0, 0.0, 0.0); | + | glUniform3f(Color_ID, 1.0, 0.0, 0.0); |
glUniform1f(X_ID, ofs); | glUniform1f(X_ID, ofs); | ||
glUniform1f(Y_ID, -ofs); | glUniform1f(Y_ID, -ofs); | ||
− | glDrawArrays(GL_POINTS, 0, Length( | + | glDrawArrays(GL_POINTS, 0, Length(Point)); |
− | // | + | // grün |
glUniform1i(PointTyp_ID, 2); | glUniform1i(PointTyp_ID, 2); | ||
− | glUniform3f(Color_ID, 0.0, 1.0, 0.0); | + | glUniform3f(Color_ID, 0.0, 1.0, 0.0); |
glUniform1f(X_ID, ofs); | glUniform1f(X_ID, ofs); | ||
glUniform1f(Y_ID, ofs); | glUniform1f(Y_ID, ofs); | ||
− | glDrawArrays(GL_POINTS, 0, Length( | + | glDrawArrays(GL_POINTS, 0, Length(Point)); |
− | |||
− | |||
+ | // blau | ||
glUniform1i(PointTyp_ID, 3); | glUniform1i(PointTyp_ID, 3); | ||
− | glUniform3f(Color_ID, 0.0, 0.0, 1.0); | + | glUniform3f(Color_ID, 0.0, 0.0, 1.0); |
glUniform1f(X_ID, -ofs); | glUniform1f(X_ID, -ofs); | ||
glUniform1f(Y_ID, ofs); | glUniform1f(Y_ID, ofs); | ||
− | glDrawArrays(GL_POINTS, 0, Length( | + | glDrawArrays(GL_POINTS, 0, Length(Point)); |
+ | |||
+ | ogc.SwapBuffers; | ||
+ | end;</syntaxhighlight> | ||
<br><br> | <br><br> | ||
==Vertex-Shader:== | ==Vertex-Shader:== | ||
− | |||
− | |||
<syntaxhighlight lang="glsl">#version 330 | <syntaxhighlight lang="glsl">#version 330 | ||
Aktuelle Version vom 23. Juli 2018, 21:43 Uhr
Inhaltsverzeichnis
Shader - Punkte verschieden darstellen
Einleitung
Man kann auch Punkte mit dem Shader darstellen, dies kann man auf verschiedene Weise.
Im Fragment-Shader kann man das Zeichen der Punkte manipulieren.
Die Deklaration der Koordianten und Punktgrösse.
var
Point: array of TVertex2f;
PointSize: array of GLfloat;
Daten für die Punkte in die Grafikkarte übertragen
procedure TForm1.InitScene;
begin
glClearColor(0.6, 0.6, 0.4, 1.0); // Hintergrundfarbe
// Daten für Punkt Position
glBindVertexArray(VBPoint.VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBPoint.VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(TVertex2f) * Length(Point), Pointer(Point), GL_STATIC_DRAW);
glEnableVertexAttribArray(10);
glVertexAttribPointer(10, 2, GL_FLOAT, False, 0, nil);
// Daten für Punkt Grösse
glBindVertexArray(VBPoint.VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBPoint.VBO_Size);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * Length(PointSize), Pointer(PointSize), GL_STATIC_DRAW);
glEnableVertexAttribArray(11);
glVertexAttribPointer(11, 1, GL_FLOAT, False, 0, nil);
end;
Zeichnen der Punkte
procedure TForm1.ogcDrawScene(Sender: TObject);
const
ofs = 0.4;
begin
glEnable(GL_PROGRAM_POINT_SIZE);
glClear(GL_COLOR_BUFFER_BIT);
Shader.UseProgram;
glBindVertexArray(VBPoint.VAO);
// gelb
glUniform1i(PointTyp_ID, 0);
glUniform3f(Color_ID, 1.0, 1.0, 0.0);
glUniform1f(X_ID, -ofs);
glUniform1f(Y_ID, -ofs);
glDrawArrays(GL_POINTS, 0, Length(Point));
// rot
glUniform1i(PointTyp_ID, 1);
glUniform3f(Color_ID, 1.0, 0.0, 0.0);
glUniform1f(X_ID, ofs);
glUniform1f(Y_ID, -ofs);
glDrawArrays(GL_POINTS, 0, Length(Point));
// grün
glUniform1i(PointTyp_ID, 2);
glUniform3f(Color_ID, 0.0, 1.0, 0.0);
glUniform1f(X_ID, ofs);
glUniform1f(Y_ID, ofs);
glDrawArrays(GL_POINTS, 0, Length(Point));
// blau
glUniform1i(PointTyp_ID, 3);
glUniform3f(Color_ID, 0.0, 0.0, 1.0);
glUniform1f(X_ID, -ofs);
glUniform1f(Y_ID, ofs);
glDrawArrays(GL_POINTS, 0, Length(Point));
ogc.SwapBuffers;
end;
Vertex-Shader:
#version 330
layout (location = 10) in vec2 inPos; // Vertex-Koordinaten in 2D
layout (location = 11) in float inSize; // Vertex-Koordinaten in 2D
uniform float x; // Richtung von Uniform
uniform float y;
void main(void)
{
vec2 pos = inPos;
pos.x = pos.x + x;
pos.y = pos.y + y;
gl_PointSize = inSize;
gl_Position = vec4(pos, 0.0, 1.0); // Der zweiter Parameter (Z) auf 0.0
}
Fragment-Shader:
#version 330
uniform vec3 Color ; // Farbe von Uniform
out vec4 outColor; // ausgegebene Farbe
uniform int PointTyp;
void main(void)
{
vec2 p = gl_PointCoord * 2.0 - vec2(1.0);
float r = sqrt(dot(p, p));
float theta = atan(p.y, p.x);
switch (PointTyp){
case 0: if(dot(gl_PointCoord - 0.5, gl_PointCoord - 0.5) > 0.25)
discard;
else
outColor = vec4(Color, 1.0);
break;
case 1: if(dot(p, p) > cos(theta * 5))
discard;
else
outColor = vec4(Color, 1.0);
break;
case 2: if(dot(p, p) > r || dot(p, p) < r * 0.75)
discard;
else
outColor = vec4(Color, 1.0);
break;
case 3: if(dot(p, p) > 5.0 / cos(theta - 20 * r))
discard;
else
outColor = vec4(Color, 1.0);
break;
default: discard;
}
}
Autor: Mathias
Siehe auch
- Übersichtseite Lazarus - OpenGL 3.3 Tutorial