opengl_x11

Playing with OpenGL
git clone git://bsandro.tech/opengl_x11
Log | Files | Refs | README | LICENSE

commit 6c799e60a3675f4fd9f90311e2bdfb2d57f972c0
parent 3dfc6cfdb943fa00282d18931040e3b6dc016395
Author: bsandro <email@bsandro.tech>
Date:   Thu, 20 Nov 2025 22:45:32 +0200

rotating weird cube

Diffstat:
Mcube.c | 66+++++++++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 47 insertions(+), 19 deletions(-)

diff --git a/cube.c b/cube.c @@ -37,14 +37,14 @@ static double t_started; //@todo maybe move shaders to separate files static const GLchar *vertexSrc = R"( #version 420 -layout(location=0) in vec2 position; +layout(location=0) in vec3 position; layout(location=1) in vec2 aTexCoord; out vec2 texCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { - gl_Position = projection * view * model * vec4(position, 0.0, 1.0); + gl_Position = projection * view * model * vec4(position, 1.0); texCoord = aTexCoord; } )"; @@ -181,14 +181,47 @@ int main(int argc, char *argv[]) { // x,y,tx,ty static const GLfloat vertices[] = { - -0.5, -0.5, 0+c, 1-c, - 0.5, -0.5, 1-c, 1-c, - 0.5, 0.5, 1-c, 0+c, - -0.5, 0.5, 0+c, 0+c - }; - static const GLuint elements[] = { - 0, 1, 2, - 2, 3, 0 + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f }; GLuint vbo; @@ -196,11 +229,6 @@ 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); @@ -212,10 +240,10 @@ int main(int argc, char *argv[]) { // texture GLuint tex = mkTextureAstc(tex_fname); GLint position = glGetAttribLocation(shaderPrg, "position"); - glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void *)0); + glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void *)0); glEnableVertexAttribArray(position); GLint aTexCoord = glGetAttribLocation(shaderPrg, "aTexCoord"); - glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void *)(2*sizeof(GLfloat))); + glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void *)(3*sizeof(GLfloat))); glEnableVertexAttribArray(aTexCoord); mat4 model; @@ -260,7 +288,7 @@ int main(int argc, char *argv[]) { glUniform1d(iTime, current_ts()-t_started); //printf("%f\n", fabsf(sinf(current_ts()-t_started))); - //glm_rotate(view, glm_rad(rotation), (vec3){0.0, 0.0, 1.0}); + glm_rotate(model, glm_rad(1.f), (vec3){0.5f, 1.f, 0.f}); GLuint loc_model = glGetUniformLocation(shaderPrg, "model"); glUniformMatrix4fv(loc_model, 1, GL_FALSE, model[0]); GLuint loc_view = glGetUniformLocation(shaderPrg, "view"); @@ -272,7 +300,7 @@ int main(int argc, char *argv[]) { glUniform1i(tex, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex); - glDrawElements(GL_TRIANGLES, sizeof(elements)/sizeof(elements[0]), GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(vertices[0])); glXSwapBuffers(dpy, win); usleep(1000*7); // 144hz