Opengl program sample crash - c++

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.

Related

OpenGL window not opening process terminating on eclipse cdt

When i run this project nothing happens. Nothing is sent to the console either. I'm on windows
In the libraries settings in eclipse, I have opengl32, glu32, and freeglut.
/*
* GL01Hello.cpp: Test OpenGL/GLUT C/C++ Setup
* Tested under Eclipse CDT with MinGW/Cygwin and CodeBlocks with MinGW
* To compile with -lfreeglut -lglu32 -lopengl32
*/
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include <GLFW/glfw3.h>
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Draw a Red 1x1 Square centered at origin
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}

OpenGL rendering full screen image [duplicate]

This question already has answers here:
OpenGL texture mapping stubbornly refuses to work
(4 answers)
Closed 6 years ago.
I am trying to create an application that all it does is display an image full screen and then flash through a sequence of images quickly (144hz) repeatedly. I have just started looking at OpenGL, have done a few tutorials and cannot figure out what I'm doing wrong here. The part that I'm getting stuck on is actually rendering the image to to the display as it only shows up as a white square. I have gone through other stack overflow questions for this but none of the suggestions have worked for me.
I am doing this in Visual Studio 2015, using a win32 application and have installed the NupenGL package. For testing purposes, I am using a 256x256 bitmap image and am loading it through the SOIL library which I have built and statically linked.
I was originally thinking that I did not building/linking the SOIL library properly so something funky was going on trying to load the image. I created a custom BMP loader which didn't work and I also tried other peoples BMP loaders on stack overflow to no avail. I now believe that it is not the loading of the texture but that I am messing up something when actually trying to render it. Also in my code below I output if the texture is invalid but it always comes back good.
Output (FULLSCREEN):
Output (WINDOWED):
My Code:
#include <gl/freeglut.h>
#include <stdio.h>
#include <iostream>
#include "SOIL.h"
void init();
void display(void);
void keyPressed(unsigned char key, int x, int y);
void resize(int heightY, int widthX);
// define the window position on screen
int window_x;
int window_y;
// variables representing the window size
int window_width = 480;
int window_height = 480;
// variable representing the window title
char *window_title = "Resolution Enhancement via Display Vibrations";
bool fullscreen = false;
//-------------------------------------------------------------------------
// Program Main method.
//-------------------------------------------------------------------------
void main(int argc, char **argv)
{
// Connect to the windowing system + create a window
// with the specified dimensions and position
// + set the display mode + specify the window title.
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(window_x, window_y);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow(window_title);
glutFullScreen();
// Setup keyPressed
glutKeyboardFunc(keyPressed);
// Handler for when the screen resizes
glutReshapeFunc(resize);
// Set OpenGL program initial state.
init();
// Set the callback functions
glutDisplayFunc(display);
// Start GLUT event processing loop
glutMainLoop();
}
//-------------------------------------------------------------------------
// Set OpenGL program initial state.
//-------------------------------------------------------------------------
void init()
{
// Set the frame buffer clear color to black.
glClearColor(0.0, 0.0, 0.0, 0.0);
}
//-------------------------------------------------------------------------
// This function is passed to glutDisplayFunc in order to display
// OpenGL contents on the window.
//-------------------------------------------------------------------------
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
GLuint texture = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
"C:/Users/joeja/Desktop/Grass.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if (texture == 0) {
std::cout << "Texture not found!\n" << std::endl;
}
else
{
std::cout << "Texture is good\n" << std::endl;
}
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS); // front face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
glutSwapBuffers();
}
void resize(int heightY,int widthX) {
const float ar = (float)widthX / (float)heightY;
glViewport(0, 20, widthX, heightY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar + 1, ar - 1, -1.0, 1.0, 2.0, 90.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyPressed(unsigned char key, int x, int y) {
switch (key) {
case 27:
case 70:
case 102: /* Fullscreen mode (Additional) : f/F */
fullscreen = !fullscreen;
if (fullscreen)
{
glutFullScreen(); /* Go to full screen */
}
else
{
glutReshapeWindow(800, 600); /* Restore us */
glutPositionWindow(0, 0);
}
break;
}
}
Do not load the image from file every frame.
In your init you should:
load the image with SOIL like you already are, storing the ID as texture
In display you should,
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D)
draw stuff
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
You may notice that you could skip some enables/disables by just enabling texture 2D and leaving it on. I gave pseudocode that tries to always work, skipping redundant state changes is an optimization not relevant to the problem.

OpenGL/Freeglut in Eclipse Problems - Includes Not Being Found?

I am trying to configure OpenGL/Freeglut in Eclipse on Windows. I have installed MinGW as my toolchain, and the Environment maps MINGW_HOME to the correct top level directory on my C:/ Drive (C:/MingGW). My project lists the various includes in the Project Explorer, including the relevant one to my purpose, C:/MinGW/include.
C:/MinGW/include/GL has all the Freeglut header files which detail the functions I want to use as expected. In particular, C:/MinGW/include/GL/glut.h is definitely where it is expected. My simple test code is below:
#include <windows.h> // For MS Windows
#include <GL/glut.h>
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// Draw a Red 1x1 Square centered at origin
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the infinitely event-processing loop
return 0;
}
None of the functions (except glutInit and glutCreateWindow) are recognized by the program, despite the include lines. What might be causing this? Is there a step I have missed in my configuration?
Please ask if any further details required.
UPDATE: Situation improved. Not sure how to link to the libraries freeglut_std.h requires, as three functions in it (listed in comment below) are throwing errors, but do not appear to be in explicit use. Any advice?
I solved the problem by including the #define GLUT_DISABLE_ATEXIT_HACK define before the #include header!

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