I am running a simple application to open a window and show some points in it in xcode 13. Everything seems to work fine, no import errors, all correct includes and dynamic libraries etc... but upon running the app the error is
freeglut (/Users/sukhacoder02/Library/Developer/Xcode/DerivedData/Test-gxuvnapyfsfufeeabywtmpmjgbtg/Build/Products/Debug/Test): failed to open display ''
Program ended with exit code: 1
The code is
#define GL_SILENCE_DEPRECATION
//#include <GLFW/glfw3.h>
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2f(10.0, 10.0);
glVertex2f(150.0, 80.0);
glVertex2f(100.0, 20.0);
glVertex2f(200.0, 100.0);
glEnd();
glFlush();
}
void myinit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Points");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
I've read many solutions but they all are complex and trying to do some ssh on server etc... Some suggest display environment variable setting, I've also tried, but nothing seems to work.
Any help would be highly appreciated.
Thanks.
Related
I'm new with OpenGl and I try to run this example (source):
#include<GL/glu.h>
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
int xa,xb,ya,yb;
void display (void)
{
int dx=xb-xa;
int dy=yb-ya;
int p0 = 2*dy - dx;
float x=xa,y=ya;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
int p =p0;
int k;
for(k=0;k<dx;k++)
{
if(p<0)
{
x = x+1;
glVertex2i(x,y);
}
else
{
x = x+1; y = y+1;
glVertex2i(x,y);
}
}
glEnd();
glFlush();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
printf("Enter the points\n(X1,Y1,X2,Y2):-\n");
scanf("%d %d %d %d",&xa,&ya,&xb,&yb);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Breshanman Line Algorithm ");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
But any printf command does not show the message on the console, I tested other code using OpenGL with printf and got the same result.
The CodeBlocks doesnt show any error.
Worked when I added the libraries gdi32, comdlg32 and user32 to the link libraries.
Can some one just mention easy steps to initialize openGL libraries for visual studio. I tried nehe tutorials but it is not working for me.
Open.gl does a good job at explaining context creation for Modern OpenGL. They even took the time to write implementations for the few popular libraries someone might want to use like SFML/SDL/GLFW.
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <glut.h>
using namespace std;
#include <stdlib.h>
static GLfloat spin = 0.0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0); glRectf(-25.0, -25.0, 25.0, 25.0);
glBegin(GL_LINES);
glColor3f(0.5, 0.5, 0.4);
glVertex3f(0.0,0.0,0.0);
glVertex3f(5.0,10.0,0.0);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
/*
* Request double buffer display mode.
* Register mouse input callback functions
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
I am new at opengl. I am starting with a simple triangle using GLFW , but I dont get anything drawn.
My system is :
OSX Mavericks 10.9.2 Intel HD 4000
compilation cmd line:
g++ main.cpp -o Test -I/usr/local/include -L/usr/local/lib -lglfw -framework Opengl
First thing I noticed odd was, on compilation , it did not identify
glGenVertexArrays , and suggested: glGenVertexArraysAPPLE same for:glBindVertexArray and suggested: glBindVertexArrayAPPLE
On using APPLE ext , didnt draw anything.
What am I missing here?
#include <iostream>
#include <OpenGL/gl.h>
#include <GLFW/glfw3.h>
#include <string.h>
GLuint vao=0;
GLuint shader_programme;
void CheckGLErrors(std::string str)
{
int errCount = 0;
GLenum currError = glGetError();
if(currError != GL_NO_ERROR)
{
//Do something with `currError`.
std::cout<<"\n"<<str<<" Error raised:"<<currError<<"\n";
}
}
void initVertex()
{
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
static const float colors[] =
{
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0
};
glGenVertexArraysAPPLE(1, &vao);
glBindVertexArrayAPPLE (vao);
GLuint vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
glEnableVertexAttribArray (0);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main () {"
" gl_Position = vec4 (vp, 1.0);"
"}";
const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
GLint status = GL_TRUE;
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
if(status != GL_TRUE) std::cout<<"VS FAILED\n";
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
status = GL_TRUE;
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
if(status != GL_TRUE) std::cout<<"PS FAILED\n";
shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);
}
int main(int argc, char * argv[])
{
GLFWwindow* window;
int ver[3];
glfwGetVersion(&ver[0], &ver[1], &ver[2]);
std::cout << "major " <<ver[0] <<"minor: "<<ver[1]<<" ver: "<<ver[2];
/* Initialize the library */
if (!glfwInit())
{
std::cout << "Error";
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
std::cout << "Create window Error";
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
std::cout<<"Version: " << glGetString(GL_VERSION);;
initVertex();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(40.0f);
glClearColor(0.0f,1.0f,0.0f,0.0f);
glUseProgram(shader_programme);
glBindVertexArrayAPPLE(vao);
glDrawArrays(GL_TRIANGLE, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
On the missing declaration for glGenVertexArrays(), you need to include gl3.h instead of gl.h to get GL3 level functionality:
#include <OpenGL/gl3.h>
One problem I see in your code is that you use vertex attribute location 0 for setting up your coordinates. The attribute location is the first argument to glEnableVertexAttribArray() and glVertexAttribPointer(). But you don't really know that the location of vp in your shader program is 0. The easiest way to ensure this is that you specify it directly in your shader code by extending the declaration of vp:
layout(location = 0) in vec3 vp;
This works with GL 3.3 and higher. An alternative with older GL versions is to make the following API call before the glLinkProgram() call:
glBindAttribLocation(shader_programme, 0, "vp");
My intention is to draw using QtOpenGL and to move around the scene using scroll bars.
The weird thing: it works well on windows and linux and it does not work on osx. I suspect that building qt from source may solve this problem.
You may get a copy of following source code(with executables for windows, linux and mac os x) here.
main.cpp
#include <QtGui>
#include <QtOpenGL>
class GraphicsView: public QGraphicsView {
public:
GraphicsView(QWidget* parent = 0) : QGraphicsView(parent) {}
protected:
void resizeEvent(QResizeEvent *event) {
if (scene()) {
scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
}
QGraphicsView::resizeEvent(event);
}
};
class GraphicsScene : public QGraphicsScene {
public:
void drawBackground(QPainter *painter, const QRectF &rect) {
if (painter->paintEngine()->type() != QPaintEngine::OpenGL && painter->paintEngine()->type() != QPaintEngine::OpenGL2) {
qWarning("GraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view");
return;
}
painter->beginNativePainting();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width(), height());
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(-0.25, 0.25, 0.0);
glVertex3f(-0.25, -0.25, 0.0);
glVertex3f(0.25, -0.25, 0.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glVertex3f(-0.25, 0.25, 0.0);
glEnd();
painter->endNativePainting();
}
};
class WindowViewer: public QMainWindow {
public:
WindowViewer() : m_area(this), m_view(this), m_widget(), m_canvas() {
m_view.setScene(&m_canvas);
m_view.setViewport(&m_widget);
m_view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
m_view.resize(1000, 1000);
m_area.setWidget(&m_view);
setCentralWidget(&m_area);
setWindowTitle("WindowViewer");
show();
raise();
}
virtual ~WindowViewer(){}
private:
QScrollArea m_area;
GraphicsView m_view;
QGLWidget m_widget;
GraphicsScene m_canvas;
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
WindowViewer w;
w.resize(800, 600);
return app.exec();
}
main.pro
TEMPLATE = app
TARGET = main
SOURCES += main.cpp
QT += opengl
I have built it on following setups. But many other setups may suffice as well.
OS X 10.9 Mavericks
Qt version 4.8.5, QMake version 2.01a
gcc version 4.8.2
Ubuntu 12.04
Using Qt version 4.8.1, QMake version 2.01a
gcc version 4.8.1
Windows 7
Qt version 4.8.5, QMake version 2.01a
gcc version 4.8.1
I am porting some code from OpenGL to the ES version. I am using glDrawArrays() to draw a triangle in conjuction with glVertexPointer(). However, it doesn't draw on the screen. The complete code is:
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glEnableClientState (GL_COLOR_ARRAY);
glClear (GL_COLOR_BUFFER_BIT);
glColor4f (0.0, 0.0, 1.0, 1.0);
glLoadIdentity ();
glTranslatef(0, 0, -20);
const GLfloat triVertices[] = {
0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f
};
glVertexPointer(3, GL_FLOAT, 0, triVertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
GLUT opens up the window, it clears with black, but doesn't draw anything.
Can someone please spot what I am doing wrong? Thanks.
I see no call to glEnableClientState(GL_VERTEX_ARRAY);. I also see glEnableClientState (GL_COLOR_ARRAY); but no call to glColorPointer(). Perhaps you wrote one when you meant the other?