-1

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
precise
  • 439
  • 1
  • 4
  • 13

2 Answers2

2

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.

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

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • Well, it seems like I forgot to mention that I am new to OpenGL world in my question text. :) After your answer, it made sense why I have been getting this error from the code. So, in this case, should I assume that all the libraries added at the top of the code are taking care of the old deprecated functions, so that developers will not have to deal with the details of implementing "Immediate Mode" rendering things? – precise Nov 03 '13 at 21:18
  • @ErdemKAYA You can always simply target and older version of OpenGL, like version 1.5 or something like that. I'm not sure I 100% understand what you mean with the rest though, you should only get those errors when you actually USE the deprecated functions, not simply by loading the libraries. The thing about *Immediate Mode* well as I wrote at the bottom of my answer, today we don't use it, we use VAOs with VBOs, which is buffers that can loading vertex data into them and store them on the GPU. – vallentin Nov 04 '13 at 07:29
1

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>.

keltar
  • 17,711
  • 2
  • 37
  • 42