Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Code not working when inside class constructor?

  1. #1
    Newbie Newbie
    Join Date
    Jan 2017
    Posts
    2

    Question Code not working when inside class constructor?

    This is my program:

    Code cpp:
    #include <GL\glew.h>
    #include <GLFW\glfw3.h>
     
    #include <iostream>
     
    int main()
    {
        glfwWindowHint(GLFW_VERSION_MAJOR, 4);
        glfwWindowHint(GLFW_VERSION_MINOR, 4);
     
        if (!glfwInit())
            std::cerr << "Failed to initialize GLFW." << std::endl;
     
        GLFWwindow* window;
        window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
        if (!window) 
            std::cerr << "Failed to create Window." << std::endl;
     
        glfwMakeContextCurrent(window);
     
        GLenum err;
        if ((err = glewInit()) != GLEW_OK)
        {
            std::cerr << "Failed to initialize GLEW" << std::endl;
            std::cerr << err << std::endl;
        }
     
        float data[] = { -0.5, -0.5, 0.1,
                        0.0, 0.5, 0.1,
                        0.5, -0.5, 0.1 };
     
        GLuint vaoID;
        GLuint vboID;
     
        glGenVertexArrays(1, &vaoID);
     
        glBindVertexArray(vaoID);
        glGenBuffers(1, &vboID);
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);
        glBindVertexArray(0);
     
        while (!glfwWindowShouldClose(window))
        {
     
            glClear(GL_COLOR_BUFFER_BIT);
     
            glBindVertexArray(vaoID);
            glEnableVertexAttribArray(0);
            glBindBuffer(GL_ARRAY_BUFFER, vboID);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
     
            glDrawArrays(GL_TRIANGLES, 0, 3);
            glDisableVertexAttribArray(0);
     
            glfwSwapBuffers(window);
     
            glfwPollEvents();
        }
     
        glfwTerminate();
    }

    It produces a white triangle like you might expect. BUT. when i place some of the vao and vbo code in a class like this:

    Code cpp:
    #pragma once
    #include <GL\glew.h>
     
    class Model
    {
    public:
        Model(float* data) {
            glGenVertexArrays(1, &vaoID);
     
            glBindVertexArray(vaoID);
            glGenBuffers(1, &vboID);
            glBindBuffer(GL_ARRAY_BUFFER, vboID);
            glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);
            glBindVertexArray(0);
        }
        ~Model();
     
        GLuint vaoID;
        GLuint vboID;
    };

    And the main code looks like this:

    Code cpp:
    #include <GL\glew.h>
    #include <GLFW\glfw3.h>
     
    #include <iostream>
     
    #include "Model.h"
     
    int main()
    {
        glfwWindowHint(GLFW_VERSION_MAJOR, 4);
        glfwWindowHint(GLFW_VERSION_MINOR, 4);
     
        if (!glfwInit())
            std::cerr << "Failed to initialize GLFW." << std::endl;
     
        GLFWwindow* window;
        window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
        if (!window) 
            std::cerr << "Failed to create Window." << std::endl;
     
        glfwMakeContextCurrent(window);
     
        GLenum err;
        if ((err = glewInit()) != GLEW_OK)
        {
            std::cerr << "Failed to initialize GLEW" << std::endl;
            std::cerr << err << std::endl;
        }
     
        float data[] = { -0.5, -0.5, 0.1,
                        0.0, 0.5, 0.1,
                        0.5, -0.5, 0.1 };
     
        Model model(data);
     
        while (!glfwWindowShouldClose(window))
        {
     
            glClear(GL_COLOR_BUFFER_BIT);
     
            glBindVertexArray(model.vaoID);
            glEnableVertexAttribArray(0);
            glBindBuffer(GL_ARRAY_BUFFER, model.vboID);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
     
            glDrawArrays(GL_TRIANGLES, 0, 3);
            glDisableVertexAttribArray(0);
     
            glfwSwapBuffers(window);
     
            glfwPollEvents();
        }
     
        glfwTerminate();
    }

    It does not.

    Why is this it has been foiling my projects for ages and i'm really fed up with it.
    Last edited by Dark Photon; 01-24-2017 at 06:22 AM.

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    452
    wrong use of pointers .. try printing "sizeof(data)" in your constructor, it wont give you "sizeof(float) * 9" as you expect

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2017
    Posts
    27
    use model(const float* data) otherwise do us john_connor said. for me personaly i hate using flat out pointers it's 2017 and std::vector has been here for long time and almost few who use it
    . it's simpler less messy and controllable

  4. #4
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    456
    Quote Originally Posted by Hellice View Post
    it's 2017 and std::vector has been here for long time and almost few who use it. it's simpler less messy and controllable
    This will work as long as you compile with at least a C++ 03 compliant compiler.
    This is thus not guarantee to work as you transparently expect it to work on C++ 98 compilers.
    Last edited by Silence; 01-24-2017 at 05:53 AM. Reason: Fixed real C++ version for vector memory layout

  5. #5
    Newbie Newbie
    Join Date
    Jan 2017
    Posts
    2

    It works

    Thank you i've changed to using vectors as suggested and also watch my sizeof usage and it all works great now thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •