-1

I am having issues getting this to run:

main file:

//
#include <iostream>


#include <GL/glew.h>
#include <OpenGL/OpenGL.h>
#include <GLFW/glfw3.h>
#include <fstream>
#include <sstream>
#include <time.h>
#include <chrono>
#include <_time.h>
#include <iostream>
#include "methantantis.h"
static unsigned int CompileShader( unsigned int type, const std::string source){
    
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);
    
    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE){
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        std::cout <<"failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader" <<"\n";
        std::cout << message << "\n";
        glDeleteShader(id);
        return 0;
    }
    return id;
}

static int CreateShader(const std::string& VertexShader, const std::string& FragmentShader){
    
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER, VertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, FragmentShader);
    
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);
    
    glDeleteShader(vs);
    glDeleteShader(fs);
    
    return program;
}

int main(void){
    
    /* Initialize the library */
    GLFWwindow* window;
    
    if (!glfwInit()){return -1;}
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
    
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
    std::cout<<glfwGetVersionString();
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Gaston", NULL, NULL);
    if (!window){
        glfwTerminate();
        return -1;
    }
    glfwSwapInterval(1);
    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    
    if (glewInit() != GLEW_OK){ std::cout <<"glewunit(); failed\n"; }
    
    float positions[] = {
        -0.5f, -0.5f,//0
        .5f, -.5f,//1
        0.5f, 0.5f,//2
        -0.5f, 0.5f,//3
    };
    unsigned int indeces[]={
        0,1,2,
        2,3,0
    };
    
        GLuint spookyvao;
        glGenVertexArrays(1, &spookyvao);//still no idea what this does DO NOT TOUCH IT
        glBindVertexArray(spookyvao);
        
        varray va;
        vbuff Vbuffer(positions,4*2*sizeof(float));
        
        vblayout layout;
        layout.push<float>(2,false);
        va.addbuffer(Vbuffer, layout);
        
        ibuff ibuffer(indeces,sizeof(float) * sizeof(indeces));
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        std::string vertexshader =
        "#version 330 core\n"
        "   \n"
        "layout(location = 0) in vec4 position;\n"
        "uniform \n"
        "   \n"
        "void main()\n"
        "{\n"
        "   gl_Position = position;\n"
        "}\n";
        std::string fragmentshader =
        "#version 330 core\n"
        "    \n"
        "layout(location = 0) out vec4 colour;\n"
        "    uniform vec4 u_Colour;\n"
        "void main()\n"
        "{\n"
        "   colour = u_Colour;\n"
        "}\n";
        std::string ofragmentshader =
        "#version 330 core\n"
        "    \n"
        "layout(location = 0) out vec4 colour;\n"
        "    uniform vec4 u_Colour;\n"
        "    uniform vec4 u_Line;\n"
        "void main()\n"
        "{\n"
        "\n"
        "   colour = u_Colour + (gl_FragCoord - (0.9 * floor(gl_FragCoord/0.9)));\n"
        "\n"
        "\n"
        "\n"
        "}\n";//vec4(1.0, 0.0, 0.0, 1.0)
        char* c = new char;
        std::cin>>*c;
        
        std::string slimV = "#version 330 core\n  \n"
        "                                                    \n"
        "    layout(location = 0) in vec3 in_vertex;         \n"
        "    layout(location = 1) in vec2 in_texcoord;       \n"
        "    layout(location = 2) in vec4 in_color;          \n"
        "    out vec2 tex_coord;                             \n"
        "    out vec4 color;                                 \n"
        "                                                    \n"
        "    uniform mat4 projection = mat4(1.0);            \n"
        "    uniform mat4 view = mat4(1.0);                  \n"
        "    uniform mat4 model = mat4(1.0);                 \n"
        "                                                    \n"
        "    void main() {                                   \n"
        "        gl_Position = projection * view * model * vec4(in_vertex, 1.0);   \n"
        "        tex_coord = in_texcoord;                    \n"
        "        color = in_color;                           \n"
        "    }\n                                             \n"
        ;//dont use https://devcry.heiho.net/html/2017/20170122-shader.html
        unsigned int shader;
        if (*c =='o'){
            shader = CreateShader(vertexshader, ofragmentshader);
        }else {
            shader = CreateShader(vertexshader, fragmentshader);
        }
        glUseProgram(shader);
        u_int location = glGetUniformLocation(shader,"u_Colour");
        u_int lineloc = glGetUniformLocation(shader,"u_Line");
        glUniform4f(location,1.0f, 0.0f, 0.0f, 1.0f);
        float jimbo[6] = {-0.1f, -0.0f,0.0f, -.0f,0.0f, 0.0f};//also unsure
        /* Loop until the user closes the window */
        float redness = 1.0f;
        float linepos = 0.0f;
        float increment = 0.01f;
        while (!glfwWindowShouldClose(window)){
           
            /* Render here */
            positions[1] = positions[1] + jimbo[1];
            
            glClear(GL_COLOR_BUFFER_BIT);
            glUseProgram(shader);
            glUniform4f(location,redness,0.0f,0.0f,0.0f);
            glUniform4f(lineloc,linepos,0.0f,0.0f,0.0f);
            glBindVertexArray(va.rendererid);
            ibuffer.bind();
            
            glDrawElements(GL_TRIANGLES, sizeof(indeces),GL_UNSIGNED_INT,NULL);
            /* Swap front and back buffers */
            glfwSwapBuffers(window);
            
            /* Poll for and process events */
            glfwPollEvents();
            linepos = linepos + increment;
            redness = redness + increment;
            if (redness > 1)
                increment = -0.01f;
            if (redness < -1)
                increment = 0.01f;
            glBufferData(GL_ARRAY_BUFFER, sizeof(positions) * sizeof(float), positions, GL_DYNAMIC_DRAW);
        }
        glDeleteProgram(shader);
    
    glfwTerminate();
    exit(0);
}

methantantis.h:

//
//  e..h
//  A box of toys
//
//  Created by Preston Heffner on 11/11/25.
//
#ifndef methantantis
#define methantantis
#include <Gl/glew.h>
#include <vector>
class vbuff{
public:
    unsigned int RendererID;
    
    vbuff(const void* data,unsigned int size);
    ~vbuff();
    void bind();
    void unbind();
};//now wes go the ibuff

class ibuff{
public:
    unsigned int RendererID;
    unsigned int Count;
    ibuff(const void* data,unsigned int count);
    ~ibuff();
    void bind();
    void unbind();
};//now varray
typedef struct vament{
    unsigned int count;
    unsigned int type;
    unsigned int normalised:1;
    static unsigned int GetTypeSize(unsigned int type){
        switch (type){
            case GL_FLOAT: return 4;
            case GL_UNSIGNED_INT: return 4;
            case GL_UNSIGNED_BYTE: return 1;
        }
        return 0;
    }
} vament;
class vblayout{
public:
    std::vector<vament> Elements;
    unsigned int stride;
    template <typename t> void push(unsigned int count,bool normalised){
        
    }
    template <> void push<float>(unsigned int count,bool normalised){
        Elements.push_back({GL_FLOAT,count,normalised});
        stride += vament::GetTypeSize(GL_FLOAT) * count;
    }
    template <> void push<unsigned int>(unsigned int count,bool normalised){
        Elements.push_back({GL_UNSIGNED_INT,count,normalised});
        stride += vament::GetTypeSize(GL_UNSIGNED_INT) * count;
    }
    template <> void push<unsigned char>(unsigned int count,bool normalised){
        Elements.push_back({GL_UNSIGNED_BYTE,count,normalised});
        stride += vament::GetTypeSize(GL_UNSIGNED_BYTE) * count;
    }
};
class varray{

public:
    unsigned int rendererid;
    vblayout vblay;
    varray();
    ~varray();
    void addbuffer(vbuff& vb, const vblayout& lalay);
    void Bind();
    void unBind();
};
#undef Element
#endif // !methantantis
//beginning methantantis

//final end

methantantis.c++

//
//  e.cpp
//  A box of toys
//
//  Created by Preston Heffner on 11/29/24.
//

#include <Gl/glew.h>
#include "methantantis.h"
//beginning of vertex buffer class
vbuff::vbuff(const void* data,unsigned int size){
    glGenBuffers(1, &RendererID);
    glBindBuffer(GL_ARRAY_BUFFER, RendererID);
    glBufferData(GL_ARRAY_BUFFER, size, data, GL_DYNAMIC_DRAW);
}
vbuff::~vbuff(){
    glDeleteBuffers(1,&RendererID);
}
void vbuff::bind(){
    glBindBuffer(GL_ARRAY_BUFFER, RendererID);//might need a &
}
void vbuff::unbind(){
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//end
//beginning of index buffer
ibuff::ibuff(const void* data,unsigned int count){
    glGenBuffers(1, &RendererID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RendererID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(data) * sizeof(unsigned int), data, GL_DYNAMIC_DRAW);
}
ibuff::~ibuff(){
    glDeleteBuffers(1,&RendererID);
}
void ibuff::bind(){
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RendererID);//might need a &
}
void ibuff::unbind(){
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

//end
//beginning methantantis
varray::varray(){
    
    glGenVertexArrays(1, &rendererid);//still no idea what this does DO NOT TOUCH IT
    glBindVertexArray(rendererid);
}
void varray::Bind(){
    glBindVertexArray(rendererid);
    
}
void varray::unBind(){
    glBindVertexArray(0);
    
}
varray::~varray(){
    glDeleteVertexArrays(1,&rendererid);
}
void varray::addbuffer(vbuff& vb,const vblayout& lalay){
    Bind();
    vb.bind();
    unsigned int co = 0;
    const auto& elements = lalay.Elements;
    for (u_int i = 0; i < elements.size();i++){
        const auto& element = elements[i];
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(i, element.count, element.type, element.normalised,vblay.stride, (const void*) co);
        co+=vament::GetTypeSize(element.type)*element.count;
    }
}
//final end
6
  • 2
    What problem are you having? Have you tried using a debugger? Commented Nov 15 at 6:31
  • 1
    That's quite a lot of code. Please reduce it to a minimal reproducible example. Commented Nov 15 at 6:44
  • Apart from anything else, in the code shown your vertex shader contains an unterminated uniform statement -- I don't see how that could possibly compile. Do you not see any error messages? Commented Nov 15 at 13:28
  • I noticed and fixed that and now the window appears but nothing draws. Commented Nov 15 at 16:49
  • 1
    Second argument of glDrawElements is wrong. You pass the size in bytes of the indices array instead the elements number of this array , it is 6. Commented Nov 15 at 17:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.