PDA

View Full Version : OpenGL code not working



openglnoob123
10-02-2014, 01:51 AM
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. :mad:

Thanks!

sueyllam
10-03-2014, 03:35 AM
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

Ruggero Visitnin
10-03-2014, 07:05 AM
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