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!
Related
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;
}
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.
I'm currently working on a basic GUI that create and draw a robot in a 3d space, I'm using OpenGL and freeglut to deal with the 3d part.
Until last week, I was ignoring all the perspective stuff like 'gluLookAt' or 'gluPerspective' ...
Now, I would like to add those things in order to get basic camera movement (rotation, zoom, translation) with user input.
But i'm stuck cause whenever I try to add the perspective part to my code, I'm not able to get my beautiful robot anymore.
here's my current code :
void drawScene(void) {
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glMatrixMode(GL_MODELVIEW);
glColor3f(0.0f, 0.0f, 0.0f);
ortho();
robot.draw(); // only sone basic lines and quads
glLoadIdentity();
sprintf(title, "robot creation link:%i/joint:%i", robot.linkNumber, robot.jointNumber);
glutSetWindowTitle(title);
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE|GLUT_MULTISAMPLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(1360,768);
glEnable(GL_MULTISAMPLE_ARB | GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
id = glutCreateWindow("robot creation");
glutDisplayFunc (drawScene);
glutKeyboardFunc(keyboardHandler);
glutSpecialFunc (specialKeyHandler);
glutMouseFunc (mouseHandler);
glutReshapeFunc (reshapeHandler);
glutMainLoop();
return 0;
}
I wonder if my code need to be completly re-done to work properly with such things or if I'm not using them properly.
Atm I've tried to add this after the window creation :
glViewport(0, 0, 1360, 768);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(180.0f,1360.0f/768.0f,0.1f,1000.0f);
and this in the drawScene function after the drawing part :
gluLookAt(
10.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
);
I know I'm facing the object because I can see a dot in the center of the screen that came from the robot.
You have both matrix modes (model view and projection. It is better to activate one. For gmu perspective try something like gluPerspective(170, 1.33, 0.00001, 1000); or put the camera closer to check if you can see a difference in the object. If you are not able to see the object your matrices are overwriting each other. You can check their values by:Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelMatrix);
Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projMatrix);.
Another option is also gluunproject which is easier to work than look at function (in my opinion)
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.
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).