commit 568c46f8ba2de13efcc180f068506dc3f035cfef
parent 450932a7f5e0e0761dc08925946be44abf47f0fd
Author: bsandro <email@bsandro.tech>
Date: Wed, 15 Oct 2025 02:09:18 +0300
elements
Diffstat:
| M | main1.c | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
1 file changed, 46 insertions(+), 28 deletions(-)
diff --git a/main1.c b/main1.c
@@ -28,24 +28,30 @@ static const double current_ts() {
static double t_started;
-static const GLchar *vertexSrc = " \n\
+//@todo move shaders to separate files
+static const GLchar *vertexSrc = " \n\
+#version 420 \n\
+in vec2 position; \n\
+in vec3 inColor; \n\
+out vec3 color; \n\
+void main() { \n\
+ gl_Position = vec4(position, 0.0, 1.0); \n\
+ color = inColor; \n\
+} \n\
+";
+
+static const GLchar *fragmentSrc = " \n\
#version 420 \n\
-in vec3 position; \n\
+uniform double iTime; \n\
+in vec3 color; \n\
+out vec4 outColor; \n\
void main() { \n\
- gl_Position = vec4(position, 1.0); \n\
+ float c = abs(sin(float(iTime))); \n\
+// outColor = vec4(c, 0.5, 0.3, 1.0); \n\
+ outColor = vec4(color, 1.0); \n\
} \n\
";
-static const GLchar *fragmentSrc = " \n\
-#version 420 \n\
-uniform double iTime; \n\
-out vec4 outColor; \n\
-void main() { \n\
- float c = abs(sin(float(iTime))); \n\
- outColor = vec4(c, 0.5, 0.3, 1.0); \n\
-} \n\
-";
-
static GLuint mkShader(GLenum type, const GLchar *src) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
@@ -64,16 +70,12 @@ static GLuint linkProgram(GLuint vertex, GLuint fragment) {
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertex);
glAttachShader(shaderProgram, fragment);
- glBindAttribLocation(shaderProgram, 0, "position");
- glBindFragDataLocation(shaderProgram, 0, "outColor");
+ //glBindAttribLocation(shaderProgram, 0, "position");
+ //glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
return shaderProgram;
}
-void DrawThing(GLuint shaderPrg) {
-
-}
-
void __attribute__((constructor)) start() {
printf("start()\n");
t_started = current_ts();
@@ -114,14 +116,20 @@ int main(int argc, char *argv[]) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
+ // [x, y, r, g, b]
static const GLfloat vertices[] = {
- 0.01f, 0.01f, 0.0f,
- 0.4f, 0.0f, 0.0f,
- 0.0f, 0.4f, 0.0f,
+ 0.01f, 0.01f, 1.0f, 0.0f, 0.0f,
+ 0.4f, 0.0f, 0.0f, 1.0f, 0.0f,
+ 0.0f, 0.4f, 0.0f, 0.0f, 1.0f,
+
+ -0.01f, -0.01f, 0.0f, 0.0f, 1.0f,
+ -0.4f, 0.0f, 0.0f, 1.0f, 0.0f,
+ 0.0f, -0.4f, 1.0f, 0.0f, 0.0f
+ };
- -0.01f, -0.01f, 0.0f,
- -0.4f, 0.0f, 0.0f,
- 0.0f, -0.4f, 0.0f
+ static const GLuint elements[] = {
+ 0, 1, 5,
+ 3, 2, 4
};
GLuint vbo;
@@ -129,6 +137,11 @@ int main(int argc, char *argv[]) {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+ GLuint ebo;
+ glGenBuffers(1, &ebo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
+
GLuint vertex = mkShader(GL_VERTEX_SHADER, vertexSrc);
GLuint fragment = mkShader(GL_FRAGMENT_SHADER, fragmentSrc);
GLuint shaderPrg = linkProgram(vertex, fragment);
@@ -164,9 +177,14 @@ int main(int argc, char *argv[]) {
glUseProgram(shaderPrg);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)0);
- glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(GLfloat)/3);
+ GLint position = glGetAttribLocation(shaderPrg, "position");
+ glEnableVertexAttribArray(position);
+ glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0);
+ GLint color = glGetAttribLocation(shaderPrg, "color");
+ glEnableVertexAttribArray(color);
+ glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0);
+ //glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(GLfloat)/5);
+ glDrawElements(GL_TRIANGLES, sizeof(elements), GL_UNSIGNED_INT, 0);
//glDisableVertexAttribArray(0);
//glUseProgram(0);