Hello World OpenGL program: "glEnableVertexAttribArray was not declared it this scope" - opengl

It's getting frustrating. I've been trying to get a simple hello world type program to run, but it just isn't happening.
I'm using Windows 7 and my graphics card does support the newer OpenGL stuff.
Writing in C with freeglut, and I am compiling with MinGW and Code::Blocks.
In the linker I have freeglut, opengl32 and glu32.
I keep the freeglut stuff in the freeglut folder that is located in my MinGW folder, so under search directories in my build settings I have "C:\MinGW\freeglut\include" for the compiler and "C:\MinGW\freeglut\lib" for the linker.
Here's my code:
#include <stdlib.h>
#include <GL/glut.h>
void NIAppIdle(void);
void NIAppDisplay(void);
int main(int argc, char *argv[])
{
// Setup windwing
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(800, 600);
glutCreateWindow("HELLO WORLD");
// Define GLUT callbacks
glutIdleFunc(NIAppIdle);
glutDisplayFunc(NIAppDisplay);
// Enter render loop
glutMainLoop();
return 0;
}
void NIAppIdle(void)
{
glutPostRedisplay();
}
void NIAppDisplay(void)
{
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
float geometry[] =
{
-0.5f, 0.5f, 0.0f, 1.0f,
0.5f, -5.0f, 0.0f, 1.0f,
0.0f, 0.5f, 0.0f, 1.0f
};
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, 0, 0, geometry);
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
The problem is with the "glEnableVertexAttribArray" and "glVertexAttribPointer" functions.
The compiler says that they are "not declared it this scope".
Program can run without those lines, so I guess OpenGL is linked properly, but I simply can't use those functions for some reason, and I know they are a part of OpenGL.
Is it something to do with the version of OpenGL I have or something?
Anyway, it is my first time learning OpenGL, I've probably done something horribly wrong, so I'm asking for somebody to help me. I'm sorry if this post seems a bit crappy, this is also my first time posting in this site. Also I'm sorry for my grammar, English isn't my first language.

I think you can only get to glEnableVertexAttribArray by manually loading the function pointer for it. If all you want to do is create a simple hello world program, I'd recommend you use GLEW to handle that for you.

Related

Unhandled exception error in HelloWorld OpenGL program on Visual Studio 2008

Below is my opengl code, it might not do something meaningful. I used freeglut library in the code. Below is the all of the code:
#include <glload/gll.h>
#include <glload/gl_3_0.h>
#include <GL/glut.h>
#include "glfw3.h"
void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
glLineWidth(1);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE);
glVertex3f(0.0f, 0.0f, 0.5f);
glVertex3f(0.0f, 1.0f, 0.5f);
glVertex3f(1.0f, 1.0f, 0.5f);
glEnd();
glMatrixMode(GL_PROJECTION);
glOrtho(-2.0,2.0,-2.0,2.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main (int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
// glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
// glShadeModel(GL_SMOOTH);
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutMainLoop();
return 0;
}
Here is the error message:
Unhandled exception at 0x00000000 in HelloGL5.exe: 0xC0000005: Access violation.
Please notice that I commented out two lines of code and this I am not getting this error. Why am I getting this error when I add one of those two lines to the code?
As you said in the comments.
I am targeting OpenGL 4.4.0
The reason you you're getting an "Access violation" error is because you're using a lot of deprecated functions in OpenGL.
The following functions are some of the functions deprecated in OpenGL version 3.1
glMatrixMode()
glLoadIdentity()
glBegin()
glEnd()
glVertex3*()
glTexCoord*()
glNormal*()
glColor*()
Here is a great spreadsheet about "all" the OpenGL functions and whether they are deprecated, removed, core, etc. Click here to see it. The reason I say "all" like that, is because the spreadsheet is made by people using OpenGL and not by the The Khronos Group.
Now you're maybe asking, well if the Matrix Stack is deprecated then how are we suppose to do, well... everything. Bottom line now you're suppose to build/calculate and control your own MatrixStack.
Here is a few links where you can read about Matrix calculations and Matrix Transformations.
Matrix Mathematics
Transformation Matrix
OpenGL Programming/3D/Matrices
The reason behind why glBegin(), glEnd(), etc. is deprecated. Is because the whole Immediate Mode rendering was deprecated. Now you're suppose to use VAOs with VBOs (and IBOs).
VAO
A Vertex Array Object (VAO) is an OpenGL Object that encapsulates all of the state needed to specify vertex data (with one minor exception noted below). They define the format of the vertex data as well as the sources for the vertex arrays. Note that VAOs do not contain the arrays themselves; the arrays are stored in Buffer Objects (see below). The VAOs simply reference already existing buffer objects.
Source: http://www.opengl.org/wiki/Vertex_Array_Objects#Vertex_Array_Object
VBO
A Vertex Buffer Object (VBO) is a Buffer Object which is used as the source for vertex array data. It is no different from any other buffer object, and a buffer object used for Transform Feedback or asynchronous pixel transfers can be used as source values for vertex arrays.
Source: http://www.opengl.org/wiki/Vertex_Specification#Vertex_Buffer_Object
You called function with NULL address. Debugger will tell you which exactly.
Without knowing your libraries, context and whatever, my guess is that your gl* functions all declared as pointers in either "glload/gll.h" or "glfw3.h". Remove these includes and replace them with #include <GL/gl.h>.

Opengl program sample crash

I compiled the following code:
// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class
#ifdef __APPLE__
#include <glut/glut.h> // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h> // Windows FreeGlut equivalent
#endif
GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
// Blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
shaderManager.InitializeStockShaders();
// Load up a triangle
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f };
triangleBatch.Begin(GL_TRIANGLES, 3);
triangleBatch.CopyVertexData3f(vVerts);
triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
triangleBatch.Draw();
// Perform the buffer swap to display back buffer
glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
But when i try to execute it the program crash, then the debugger gives me the following error:
Excepción no controlada en 0x00000000 en Triangle.exe: 0xC0000005: Infracción de acceso al leer la ubicación 0x00000000.
To use glCreateShader(GL_VERTEX_SHADER) you must be running OpenGL 2.0 or higher. One way to tell is to check the value of GLEW_VERSION_2_0 (after your call to glewInit()). If the value is true then OpenGL 2.0 is supported. Otherwise, you may need to update your graphics driver or use a newer graphics card.
The only way that this:
hVertexShader = glCreateShader(GL_VERTEX_SHADER)
Can get a NULL pointer exception (are you sure it's this line and not the one before it?) is if glCreateShader is NULL. GLTools is part of the OpenGL Superbible volume 5's distribution; it's not a "standard" OpenGL tool, so I can't say much about it.
But you seem to be initializing GLEW. And since you don't directly include the GLEW header, I can only guess that GLTools is including it for you. So GLEW's initialization ought to be carrying over to GLTools.
Check the value of "glCreateShader". Follow GLEW's #define for this function all the way back to the actual variable that GLEW defines, and then check this variable's value. If it is NULL, then you've got problems. Perhaps GLEW's initialization failed.

Opengl superbible 5th edition code problem

I've started reading superbible 5th edition and I'm stuck at the first program. As I'm using xp & vs2008, I've followed all the instruction provided by the book to setup the vs2008 to run the triangle program, but after compilation process it shows an error and I've no idea why is this happenning. The program is following:
// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class
#ifdef __APPLE__
#include <glut/glut.h> // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h> // Windows FreeGlut equivalent
#endif
GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
// Blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
shaderManager.InitializeStockShaders();
// Load up a triangle
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f };
triangleBatch.Begin(GL_TRIANGLES, 3);
triangleBatch.CopyVertexData3f(vVerts);
triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
triangleBatch.Draw();
// Perform the buffer swap to display the back buffer
glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
And the error displaying is this
1>d:\opengl\SB5\freeglut-2.6.0\VisualStudio2008Static\Release\freeglut_static.lib : fatal error LNK1107: invalid or corrupt file: cannot read at 0x3
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\dfdf\Debug\BuildLog.htm"
Could somebody please help me with this problem? Thank you.
I think your problem lays somewhere else than code. I've traced back what is
#define FREEGLUT_STATIC
doing and this popped out:
# ifdef FREEGLUT_STATIC
# define FGAPI
# define FGAPIENTRY
/* Link with Win32 static freeglut lib */
# if FREEGLUT_LIB_PRAGMAS
# pragma comment (lib, "freeglut_static.lib")
# endif
However if you downloaded prepared package from freeglut Windows Development Libraries, you get only freeglut.lib. So either you have to build "freeglut_static.lib" for yourself from freeglut, or don't use static macro and you should be fine. Linker is just saying what it's experiencing - can't find your file so not able to read...
and BTW: What book instructs is one thing but maybe you skipped something or they just didn't included it, but when you download clean freeglut, VisualStudio2008Static folder doesn't contain any library just another folders and visual studio project. You need to open that project, select release version in MSVC (build-> configuration manager) and build project. You get you freeglut_static.lib in release folder.
The error message you're getting indicates that the freeglut_static.lib file is corrupt. You would be best off to reinstall the library to ensure the file was copied correctly, or you could try rebuilding the library from source.
It might be worth trying a prebuilt version from another website, maybe you'll have more luck with the Visual Studio version available here.

OpenGL superbible access violation

Well, I've searched a bit around and the only advice I see people really giving is kind of ambiguous, slightly outdated drivers possibly (mine is the latest Geforce G210m mobile driver). Anyways, I'm just learning OpenGL and having learned some XNA, I feel this will be a good balance but I could do without having such frustration in the beginning.
Anyways, I'm simply using the source straight from Triangle.cpp. I am getting:
Unhandled exception at 0x00000000 in OOGL.exe: 0xC0000005: Access violation.
Right after the call to triangleBatch.Begin().
Can anyone tell me straight forwardly what is up and how to alleviate this?
Here is the source:
// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class
#ifdef __APPLE__
#include <glut/glut.h> // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h> // Windows FreeGlut equivalent
#endif
GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
// Blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
shaderManager.InitializeStockShaders();
// Load up a triangle
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f };
triangleBatch.Begin(GL_TRIANGLES, 3);
triangleBatch.CopyVertexData3f(vVerts);
triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
triangleBatch.Draw();
// Perform the buffer swap to display back buffer
glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
By looking at the code of GLBatch, and looking at the error, it is almost certain that either the GL_ARB_vertex_array_object or GL_ARB_vertex_buffer_object extension is missing on your computer. The code doesn't check, so that is why it falls.
Sollution? If it fails in GLBatch.cpp at the line where glGenVertexArrays() is called, simply #define OPENGL_ES at the beginning of the file, it will disable vertex array objects and everything will be fine (it is a new extension and it might not be supported by your GPU). In case it fails at glGenBuffers(), then there is probably no code remedy. You need to make sure that you have the latest graphics card drivers and that the GL_ARB_vertex_buffer_object extension is supported (you can use OpenGL Extension Viewer).

Linker Error During OpenGL: SuperBible Tutorial

I'm currently trying to decide between DirectX and OpenGL by programming a little DirectX 10 and OpenGL 3.3. I already have the setup for DirectX finished, it was fairly easy to link and compile. OpenGl is... harder.
The OpenGL Superbible has a beginning example called Triangle.cpp in which we link two libraries freeglut_static.lib and GLTools.lib. This isn't a problem; I have also gone to the Project Directories and included the Include/ and lib/ path of all necessary OpenGL extensions (GLEE, Glew, Glut, FreeGlut, GLTools- damn, is that enough?).
First I had several linker errors, because my code generation was set on DLL, not Static. I have fixed this and also added LIBC.lib to the list of ignored libraries in the Linker (not sure if setting the code generation to static fixed this as well).
Now I still have two linker errors remaining which I cannot get rid of:
1>Triangle.obj : error LNK2019: unresolved external symbol ___glutInitWithExit referenced in function _glutInit_ATEXIT_HACK
1>Triangle.obj : error LNK2019: unresolved external symbol ___glutCreateWindowWithExit referenced in function _glutCreateWindow_ATEXIT_HACK
I searched this problem on google, and many people commented on the static nature of the program (which I have fixed) as well as a particular problem with a conflicting version between Glut.h and Glut.lib. However, I even used an older version of Glut (3.6) and the linker error still remains.
Other searches in google don't really come up with anything reasonable to work with. So, I'm asking here: How do I fix this?
Info
Code Generation: Multithreaded
C++ Preprocessor Command: FREEGLUT_STATIC
IDE: Visual Studio 2008 and 2010. (Testing on both- same error on both)
Ignored Libraries: LIBC.lib
Triangle.cpp code (a simple copy/paste from the code in the book):
// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class
//#define FREEGLUT_STATIC
#include <GL/glut.h> // Windows FreeGlut equivalent
GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
// Blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
shaderManager.InitializeStockShaders();
// Load up a triangle
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f };
triangleBatch.Begin(GL_TRIANGLES, 3);
triangleBatch.CopyVertexData3f(vVerts);
triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
triangleBatch.Draw();
// Perform the buffer swap to display the back buffer
glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
Alright, here is the answer:
I had to add the following commands:
#pragma (lib, "freeglut_static.lib")
#pragma (lib, "gltools.lib")
after putting in the library path of the needed files in the Project Directories. After that I also needed to include glut32.dll in my Solution directory (as I had also linked to Glut32), and then it worked.
Hope this helps someone!
For future reference you need to supply the names of the libraries you want to link with at the linker stage.
In visual studio for example you'll find this under
Project Properties->Linker->Input
Just add them ';' delimited in the "Additional Dependencies" field.
Personally I like the #pragma comment solution though as you don't need to remember to add the .libs under additional dependencies you just need those #pragma statements somewhere.