#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;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to run a very simple example on OpenGlut for my class assignment and for some reason the code is not working on my xcode. I am currently using Xcode 10 on macOS Mojave.
Following is the code:
#include <GLUT/glut.h>
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f( -0.5, -0.5 );
glVertex2f( 0.5, -0.5 );
glVertex2f( 0.0, 0.5 );
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Hello, GL");
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
This is the error message:
2018-09-29 14:38:03.737378-0700 gluttest[18974:837022] MessageTracer: load_domain_whitelist_search_tree:73: Search tree file's format version number (0) is not supported
2018-09-29 14:38:03.738891-0700 gluttest[18974:837022] MessageTracer: Falling back to default whitelist
2018-09-29 14:38:03.857540-0700 gluttest[18974:837022] flock failed to lock maps file: errno = 35
2018-09-29 14:38:03.858276-0700 gluttest[18974:837022] flock failed to lock maps file: errno = 35
To make things more clear, I am adding more information about what I have already tried:
The same code is running absolutely fine on Xcode 10 in High Sierra.
I have changed destination target to macOS 10.8 in Xcode
I have made sure OpenGL and Glut framework binaries are linked to the project.
For some reason, the window isn't drawn initially whenever glutMainLoop() is called on Mojave. You can kind of work around this by calling it in the keyboard function:
void keyboard(unsigned char key, int x, int y) {
glutPostRedisplay();
}
int main(int argc, char** argv) {
// ...
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
EDIT: A better idea:
bool hasDrawn = false;
void display() {
// ...
if (!hasDrawn) {
glutPostRedisplay();
hasDrawn = true;
}
}
void main() {
glutDisplayFunc(display);
}
i was writing a code in C/C++ and i face an error .
#include "glut.h"
#include <random>
// Classes and structs //
struct GLPoint {
GLfloat x, y;
};
// Method(s) Declration //
void drawDot(GLfloat, GLfloat);
void serpinski_render(void);
void myInti(void);
// Method(s) Implementation //
void drawDot(GLfloat x, GLfloat y){
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void serpinski_render(void)
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen from anything is displayed on it
GLPoint T[3] = { { 10, 10 }, { 600, 10 }, { 300, 600 } }; // the three points of parent triangle
int index = rand() % 3; // this mean i will choose a random number between 0 , 3
GLPoint point = T[index];
drawDot(point.x, point.y);
for (unsigned int i = 0; i < 5500; i++) // a loop that going to run 5500 ( a very big number )
{
index = rand() % 3;
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glFlush();
}
void myInti(void)
{
glClearColor(1, 1, 1, 0); // a white background
glColor3f(0, 0, 0); // black points
glPointSize(3); // 3 pixel point size
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
// Main Method //
void main(int argc ,char ** argv )
{
glutInit(&argc, argv); // intilize toolkit
glutInitWindowPosition(100, 150);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480); // windows size is 640 x 480
glutDisplayFunc(serpinski_render);
myInti();
glutMainLoop();
}
i dont know if it will work fine but this code should produce me Sierpinski triangle .
and i face every time i use C++ library in this case the random lib this problem in the stdlib.h making me confused never face something like it before
Error 1 error C2381: 'exit' : redefinition; __declspec(noreturn) differs c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdlib.h 376
There is an incompatibility between glut.h and Visual Studio .NET, which is the usage of both "glut.h" and in your case.
You can solve it by just declaring:
#include <random>
#include "glut.h"
instead of:
#include "glut.h"
#include <random>
Please read this description for further information and for an another solution. ("Header (.h) files" section)
Also your code will possibly fail because of not creating window. You can use glutCreateWindow to create a window. You can also solve this issue by arranging your main like below:
void main(int argc ,char ** argv )
{
glutInit(&argc, argv); // intilize toolkit
glutInitWindowPosition(100, 150);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480); // windows size is 640 x 480
glutCreateWindow("A title");
myInti();
glutDisplayFunc(serpinski_render);
glutMainLoop();
}
Please also read this information for glutCreateWindow function.
You probably have this code in glut.h:
# ifndef GLUT_BUILDING_LIB
extern _CRTIMP void __cdecl exit(int);
# endif
The glut.h header is quite old. This was probably a workaround for an old VC deficiency. Visual C now seems to have a declaration that conflicts with this one. The easy solution is to just delete these lines from the header, since there is a valid definition in stdlib.h.
By the way, all the glVertex, glBegin, glEnd, matrix stack, and many other OpenGL calls are deprecated in favor of shaders.
Perhaps there is also a newer/better glut available. I'd check that out.
I am trying to figure out how to make opengl work in Windows 8.1 using Visual Studio 2012.
My program compiles and runs, but nothing happens in the windows which is created, I can't even change background color or see the mouse.
My program looks as follows:
main.cpp
#include <cstdio>
#include <iostream>
#include "simpleViewer.h"
using namespace std;
int main(int argc, char **argv){
simpleViewer Viewer;
Viewer.initOpenGL(argc, argv);
Viewer.run();
}
simpleViewer.h
#pragma once
#ifndef _SIMPLEVIEWER_H_
#define _SIMPLEVIEWER_H_
#include "GL\GL\glut.h"
#include <iostream>
enum DisplayModes {
DISPLAY_MODE_OVERLAY,
DISPLAY_MODE_DEPTH,
DISPLAY_MODE_IMAGE
};
class simpleViewer
{
public:
simpleViewer(void);
~simpleViewer(void);
virtual void run();
virtual void initOpenGL(int argc, char **argv);
virtual void initOpenGLHooks();
virtual void display();
virtual void displayPostDraw(){};
DisplayModes m_eViewState;
private:
static simpleViewer* ms_self;
static void glutIdle();
static void glutDisplay();
};
#endif
and simpleViewer.cpp
#include "simpleViewer.h"
#define GL_WIN_SIZE_X 1280
#define GL_WIN_SIZE_Y 1024
// Undeprecate CRT functions
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
simpleViewer* simpleViewer::ms_self;
simpleViewer::simpleViewer(void)
{
ms_self = this;
}
simpleViewer::~simpleViewer(void)
{
}
void simpleViewer::initOpenGL(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("Test");
// glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);
initOpenGLHooks();
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glClearColor(1.0f,0.0f,0.0f,1.0f);
}
void simpleViewer::initOpenGLHooks()
{
glutDisplayFunc(glutDisplay);
glutIdleFunc(glutIdle);
}
void simpleViewer::run(){
glutMainLoop();
}
void simpleViewer::display(){
glBegin(GL_TRIANGLES);
glColor3f(1.0,0,0);
glVertex3f(0.1,0,0);
glVertex3f(0,0,0);
glVertex3f(0,0,0.1);
glEnd();
glFlush();
}
void simpleViewer::glutIdle(){
glutPostRedisplay();
}
void simpleViewer::glutDisplay(){
simpleViewer::ms_self->display();
}
I have checked so that it really goes into display(), but nothing happens. The background is totally white even if it should be red.
You've requested double-buffering via GLUT_DOUBLE.
Use glutSwapBuffers() instead of glFlush() at the end of simpleViewer::display().
glClearColor() only latches some state. glClear() actually does the clear. Add a glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) at the top of simpleViewer::display().
That triangle is wonky for the default projection/modelview matrices. Try this instead:
glBegin( GL_TRIANGLES );
glVertex2i( 0, 0 );
glVertex2i( 1, 0 );
glVertex2i( 1, 1 );
glEnd();
You'll probably want to change the triangle color too. Red on red is pretty hard to see :)
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();
}
I have created a simple Visual Studio Express 2010 C++ project using GLUT and OpenGL,
it compiles and runs ok, except that the window it creates receives no events..
the close/minimize buttons don't do anything (not even mouseover) no context menu on the task bar on right click, and the window doesn't come to the foreground when clicked if partially covered.
The project is set up as a console application, I can close the program by closing the console.
I have this in main:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("MyApp");
glutIdleFunc(main_loop_function);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
if (GLEW_VERSION_1_3)
{
fprintf(stdout, "OpenGL 1.3 is supported \n");
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
GL_Setup(window_width, window_height);
glutMainLoop();
}
You miss a display callback. Could you try:
void display();
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("MyApp");
/// glutIdleFunc(main_loop_function);
glutDisplayFunc(display);
// ...
glutMainLoop();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
Also, your idle function seems bad, if you effectively loop inside that function. Glut is callback oriented. You don't need to create a loop, but rather rely on idle, mouse, keyboard, display and resize callbacks. If you don't do so, you are going to miss window manager events.
EDIT:
If you want to create an animation, you could use:
glutIdleFunc(idleFunc);
void idleFunc(){
glutPostRedisplay();
}