Results 1 to 3 of 3

Thread: OpenGL code not working

  1. #1
    Newbie Newbie
    Join Date
    Oct 2014
    Posts
    2

    OpenGL code not working

    Please explain to me why this code is not working:

    main.cpp:

    #include <GL/glew.h>
    #include <GL/freeglut.h>
    #include <stdio.h>
    #include "3dmath.h"

    static GLuint render_vbo;
    static void render_callback(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, render_vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_POINTS, 0, 1);
    glDisableVertexAttribArray(0);

    glutSwapBuffers();
    }

    static void render_prepare(void)
    {
    vec3 verts[1];
    verts[0] = { 0, 0, 0 };

    glGenBuffers(1, &render_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, render_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    }

    int main(int argc, char **argv)
    {
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("OpenGL");

    glutDisplayFunc(&render_callback);

    GLenum test = glewInit();
    if (test != GLEW_OK)
    {
    fprintf(stderr, "Failed to initialize glew: %s\n", glewGetErrorString(test));
    return 1;
    }

    render_prepare();
    glClearColor(0, 0, 0, 0);

    glutMainLoop();
    }

    3dmath.h:

    #ifndef MATH3D_H
    #define MATH3D_H

    struct vec3
    {
    float x, y, z;
    vec3() { };
    vec3(float sx, float sy, float sz) :
    x(sx), y(sy), z(sz)
    { };

    };

    #endif

    I got this off a youtube video and it worked for him, the code compiles with no errors and after a window creates it says: OpenGL.exe has stopped working.

    Thanks!

  2. #2
    Member Newbie
    Join Date
    Nov 2013
    Posts
    48
    I believe adding the following to the beginning of your render_prepare function would make it work:

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    where vao is GLuint

  3. #3
    Member Contributor
    Join Date
    Jul 2014
    Location
    Italy, Lissone
    Posts
    80
    you cannot bind a buffer if you hasn't generated it, it is very simple, is like fill a container that does not exists.
    Another thing, please use "[CODE] [?/CODE]" tags because it's frustating scroll down the page because of people do not use tags. good job

Similar Threads

  1. How this code is working?
    By mahmad in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 01-30-2015, 07:37 AM
  2. gluLootAt not working in this code..
    By kawaikx15 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-26-2014, 07:19 AM
  3. Replies: 1
    Last Post: 11-18-2011, 08:13 AM
  4. code not working
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-26-2001, 06:22 AM
  5. code not working!!! :-(
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-14-2001, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean