0
\$\begingroup\$

I am trying to learn OpenGL and this is my attempt to create a hello triangle program but no matter what the vertices are not normalized I when I have the set to 1,0,0 or something the vertex is not at the very edge of not even close in face.

here is my code

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>


//Vertex shader source code
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";
//Fragment Shader source code
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(0.4f, 0.4f, 0.8f, 1.0f);\n"
"}\n\0";

int main()
{
    //init glfw
    glfwInit();


    //specifing window hints
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //defining vertices of the triangle we want to draw
    GLfloat vertices[] = 
    {
      1.0f , 0.0f , .0f,  
      .0f , -1.0f , .0f, 
      0.0f , 0.0f , .0f  
    };

    //creating the window
    GLFWwindow *window = glfwCreateWindow(800, 800, "Triangle Code", NULL, NULL);
    if (window == 0)
    {
        std::cout << "Could not create a window :(" << std::endl;
        glfwTerminate();
        return -1;
    }

    //adding the window to the current context
    glfwMakeContextCurrent(window);

    //telling glad to load OpenGL for us
    gladLoadGL();

    //setting the viewport
    glViewport(0,0,800,800);

    ///setting the clear color
    glClearColor(.07f, .13f, .17f, 1.0f);

    //clear and assign the new color to the window
    glClear(GL_COLOR_BUFFER_BIT);

    //swap back and front buffers
    glfwSwapBuffers(window);

    //creating a vertex shader object
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    //Attach vertex shader to source code
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    //Complie the vertex shader
    glCompileShader(vertexShader);

    //creating the fragment shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    //Attaching the fragment shader to source code
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    //compiling source code
    glCompileShader(fragmentShader);

    //Creating a shader program and getting its refrence
    GLuint shaderProgram = glCreateProgram();
    //attaching the vertex shader to the shader program
    glAttachShader(shaderProgram,vertexShader);
    //attaching the fragment shader to the shader program
    glAttachShader(shaderProgram,fragmentShader);
    
    //Linking the shaders to shader program
    glLinkProgram(shaderProgram);

    //deleting the vertex shader as its use has been done
    glDeleteShader(vertexShader);
    //deleting the fragment shader as its use has been done
    glDeleteShader(fragmentShader);

    //Creating vertex buffer and vertex array objects
    GLuint VAO,VBO;//buffer object

    //Generating vertex arrays with only one object
    glGenVertexArrays(1, &VAO);
    //Generating fragment arrays with only one object
    glGenBuffers(1, &VBO);

    //Making the VAO the current vertex array object for OpenGL
    glBindVertexArray(VAO);
    //Binding the vertex buffer specinfing it is an GL ARRAY BUFFER
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    //Introduce the verticies to the VBO
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    //configure the vertex attribute so that OpenGl knows how to read the VBO
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    //Enable the vertex array so that OpenGL uses it
    glEnableVertexAttribArray(0);

    //Bind both the VBO & VAO to 0 to prevent us from accidently modifint as we only have one object
    glBindBuffer(GL_ARRAY_BUFFER,0);
    glBindVertexArray(0);

    //Main game loop
    while (!glfwWindowShouldClose(window))
    {
        //Clear color
        glClearColor(.07f, .13f, .17f, 1.0f);
        //Clear the color
        glClear(GL_COLOR_BUFFER_BIT);
        //tell OpenGL which shader program to use
        glUseProgram(shaderProgram);
        //Bint the VAO so OpenGL uses it
        glBindVertexArray(VAO);
        //The the array
        glDrawArrays(GL_TRIANGLES,0,3);
        //Swap front and back buffers
        glfwSwapBuffers(window);
        //poll the events
        glfwPollEvents();
    }

    //Delete VAO and VBO
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    //Delete shader program
    glDeleteProgram(shaderProgram);
    //Destroy window and terminate GLFW
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

the tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-Wall",
                "-g",
                "-I${workspaceFolder}/dependencies/include",
                "-L${workspaceFolder}/dependencies/library",
                "-fansi-escape-codes",
                "${workspaceFolder}/${relativeFile}",//This is for our open gl code
                "${workspaceFolder}/*.c",//This is for glad
                "${workspaceFolder}/dependencies/library/libglfw.3.4.dylib",
                "-o",
                "${workspaceFolder}/app",
                "-framework",
                "OpenGL",
                "-framework",
                "Cocoa",
                "-framework",
                "IOKit",
                "-framework",
                "CoreVideo",
                "-framework",
                "CoreFoundation",
                "-Wno-deprecated"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /usr/bin/clang++"
        },
    ]
}

I am new to OpenGL and this is my first time making a program in OpenGL so I may have made a very silly mistake please let me know enter image description here

one thing that I found out was that the problem is caused by

// glViewport(0, 0, 1200, 1000);
\$\endgroup\$
5
  • \$\begingroup\$ What do you mean by "normalized" in this context? That term gets used to refer to several different operations, so it helps to err on the side of over-explaining. Images/screenshots/diagrams often help too when explaining geometric problems. \$\endgroup\$
    – LudoProf
    Commented Nov 25, 2024 at 15:17
  • \$\begingroup\$ the problem is GLfloat vertices[] ; When I define [-1.0f , 0.5f , 0.0f] the 'x' coord must be at the very left but that is not the case. \$\endgroup\$ Commented Nov 25, 2024 at 15:52
  • \$\begingroup\$ What do you see instead? Show us the rendered output. \$\endgroup\$
    – DMGregory
    Commented Nov 25, 2024 at 16:36
  • \$\begingroup\$ glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); did the job where framebuffer_size_callback is: void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } \$\endgroup\$ Commented Nov 26, 2024 at 14:48
  • \$\begingroup\$ but I do not understand what the initial problem is? \$\endgroup\$ Commented Nov 26, 2024 at 14:49

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.