math_3d.h with opengl - c++

So, I was reading http://ogldev.atspace.co.uk/www/tutorial02/tutorial02.html and it said I needed math_3d.h for Vector3f.
I tried to include it:
#include <stdio.h>
#include "GL/glew.h"
#include "GL/gl.h"
#include "GL/freeglut.h"
#include "math_3d.h"
void render() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL - First window demo");
/* Set */
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
Vector3f vertices[1];
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
G++ said "main.cpp:7:21: fatal error: math_3d.h: No such file or directory". I looked for an Arch Linux package for it, but I found nothing.
I found the file here:
http://ogldev.googlecode.com/svn-history/r75/trunk/tutorial36/math_3d.h
Am I supposed to download that file and place it my project directory, or is there a cleaner way of doing it?
Also, if I do include it in my directory, how can I add it to the g++ line?
gcc main.cpp -o main -lGLEW -lglut -lGL

Am I supposed to download that file and place it my project directory?
Yes

It's sufficient download the file in your directory. Is not required to add it to gcc command line.

Yes you have to download it, if you need this library in more than one project you can save it in /usr/include and use it as a C library with #include <math_3d.h>, avoiding to copy it in every single project's directory.

Related

Undefined reference when using glew and mingw?

I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a
I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.
The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.
Please see these screenshots: http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD
C++.cpp
#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"
int main(int argv, char** args){
display x(800,600,"something");
while(!x.isClosed()){
glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
x.Update();
}
return 0;
}
display.cpp
#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit(); //NO ERRORS OCCUR
if(status != GLEW_OK){
std::cerr << "glew failed to initialize" << std::endl;
}
m_isClosed = false;
}
display::~display(){
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool display::isClosed(){
return m_isClosed;
}
void display::Update(){
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
m_isClosed = true;
}
}
}
display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/
class display{
public:
display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~display();
private:
display(const display& other){}
display& operator=(const display& other){}
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
#endif /* DISPLAY_H_ */
To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).
Create OpenGL context and window
A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).
Initilize SDL
If you are using SDL you have to create the window and you have to create the OpenGL context.
SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
SDL_GLContext glContext = SDL_GL_CreateContext( window );
Note, you should check for errors with SDL_GetError.
The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.
SDL_GL_MakeCurrent( window, glContext );
Initilize GLUT
To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.
glutInit(&argc, argv);
glutCreateWindow("OGL window");
Initilize GLFW
Note, glfwInit returns GLFW_TRUE if succeded:
if ( glfwInit() != GLFW_TRUE )
return;
GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
return;
}
glfwMakeContextCurrent( wnd );
After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.
Set up GLEW
Note that glewInit returns GLEW_OK if succeded:
if ( glewInit() != GLEW_OK )
return;
To link the GLEW library correctly you have to set up proper preprocessor definitions:
On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll
See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
and finnally:
gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)
Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test
Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.

Mac OS X refuses to create my OpenGL Window

#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
void GraphiqueAffichage() {
glClearColor(1.0, 1.0, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glFlush();
}
int main(int argc, const char * argv[]) {
// insert code here...
glutInitWindowPosition(10, 10);
glutInitWindowSize(480, 272);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutCreateWindow("Bonjour");
glutDisplayFunc(GraphiqueAffichage);
glutMainLoop();
return 0;
}
Hello
I am on a Mac using OS X 10.12, ans with this code, no window is displayed, is it normal ? Why ? Please help me.
The compilation is correct, no error, build successful, but no window is created !
I tried this code that works with windows but I have a Mac and it does not work, how to make it working ?
The compilation is correct, no error, build successful ...
but you get a list of errors when you run the program, right? "Successfully compiling" does not (alas) mean your code is correct.
Looking up the very first error message, it seems you forgot to call glutInit first:
int main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitWindowPosition(10, 10);
...
(right where your code says, "insert code here"...)
man glutInit tells you why it failed as well:
glutInit will initialize the GLUT library and negotiate a session with the window system.
where "the window system" is Mac OS X.
In addition, your main is wrong. argv is not a const char * – with that const specifier, your compiler will yell at you.
With these changes, I get a nice yellow window – your glClearColor – and with the custom title "Bonjour".
You need initializer glut
glutInit(&argc, argv);
in your main.
//#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
void GraphiqueAffichage() {
glClearColor(1.0, 1.0, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glFlush();
}
int main(int argc, const char * argv[]) {
// insert code here...
glutInit(&argc, argv);
glutInitWindowPosition(10, 10);
glutInitWindowSize(480, 272);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutCreateWindow("Bonjour");
glutDisplayFunc(GraphiqueAffichage);
glutMainLoop();
return 0;
}

Setting up GLUT with VisualStudio 2012 on Windows 8

I'm running into trouble setting up GLUT (3.7.6 binaries obtained from Nate Robins) on Windows 8 64bit with VS2012. The glut32.dll is copied to the SysWOW64 dir, both include and lib path are set in my project files and the libraries are set in the Linker->Input settings ("...;glut32.lib;glu32.lib;opengl32.lib;...").
My code looks like this:
#include <GL/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutDisplayFunc(display);
glutMainLoop();
}
The build process is successful but the application crashes with the following error message:
Unhandled exception at 0x1000BBAE (glut32.dll) in HelloOpenGL.exe: 0xC0000005: Access violation writing location 0x000000A8.
The setup seems fairly simple. Any ideas what I'm missing?
The call to glutDisplayFunc() without opening a window caused the crash. This is the updated code that opens a new window before passing the display function:
#include <GL/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//Set the window size
glutInitWindowSize(250,250);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("Hello OpenGL");
//Set the display function
glutDisplayFunc(display);
//Enter the main loop
glutMainLoop();
}

GLUT program link error

I want to compile a GLUT program
#include <GL/glut.h>
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Lighthouse3D- GLUT Tutorial");
return 1;
}
Compile and link command:
gcc -o main.exe main.c -lglut32
Result:
main.o:main.c:(.text.startup+0x1c): undefined reference to `glutInit'
main.o:main.c:(.text.startup+0x28): undefined reference to `glutInitDisplayMode'
main.o:main.c:(.text.startup+0x3c): undefined reference to `glutInitWindowPosition'
main.o:main.c:(.text.startup+0x50): undefined reference to `glutInitWindowSize'
main.o:main.c:(.text.startup+0x5c): undefined reference to `glutCreateWindow'
collect2: ld returned 1 exit status
The actual lib file of glut (3.7.6, called glut32.lib) is in the lib folder of mingw and the include file in include/GL.
What to do now?
Try it with libglut.a and libglut32.a instead of glut32.lib. To compile glut programs with minGW.
Do GLUT for Win32 is a Windows port of the original GLUT library. It’s no longer maintained or supported. The MinGW “w32api” package already comes with two GLUT import libraries, “libglut.a” and “libglut32.a”, but doesn’t come with a glut header file.
If you’ve ever downloaded a GLUT header from the internet, and attempted to link an application against either of these import libraries, you likely would have seen it fail with various undefined references.
Setting Up GLUT for Win32 With MinGW Look for Setting Up GLUT for Win32 With MinGW
If you want to have your own "build" of libglut32.a and glu32.dll.
Download Source glut-3.7.6-src.zip (4.76 MB)
Download makefile
Make the following modifications:
Add the following lines to include/GL/glut.h starting at line 12:
#ifdef __MINGW32__
#define APIENTRY __stdcall
#define CALLBACK __stdcall
#endif
Comment out line 21 in lib/glut/win32_winproc.c so that it looks:
//#include <crtdbg.h>
makefile line 5 replace:
-enable-auto-import with --enable-auto-import
run make
Have tried it with the example.c file from the guidance in the link Setting Up GLUT for Win32 With MinGW.
With the prebuild libs and the self compiled libs. Works both.
below is the result.
Example GLUT Application
example.c from the link above Setting Up GLUT for Win32 With MinGW.
#include <stdlib.h>
#include <GL/glut.h>
void keyboard(unsigned char key, int x, int y);
void display(void);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("GLUT Test");
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();
return EXIT_SUCCESS;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush();
}

how to compile some code in visual studio in my MAC

I'm running win 7 in virtual machine in my mac (parallels).
I wrote some small code and I try to run it and I get this problem:
C:\Users\Administrator\Documents\Visual Studio 10\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
It tells me that he can't find the path in the users.
I check and all the path exists.
what I can do now?!
Edit: Here's the sample code being built:
#include<glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void myinit()
{
glClearColor(0.,0.,0.,0.);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-5., 5., -5., 5);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200, 200);
glutInitWindowPosition(100, 100);
glutCreateWindow("Black Window");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
This is not a path problem, this is a linker error.
What linker is telling you is that you need an external library to complete compilation of your code (maybe you're using some function of another library and you forgot to add them in your project parameters).