OpenGL:no matching function for call to 'glutDisplayFunc' - opengl

My operating system is mac. However, when I put this code into the Xcode compiler and run it. The console says "no matching function for call to 'glutDisplayFunc'".
#include "GLUT/glut.h"
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void lineSegement(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE);
glVertex2i(180, 15);
glVertex2i(10, 145);
glEnd();
glFlush();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(400, 300);
glutCreateWindow("An Example OpenGL program");
init();
glutDisplayFunc(lineSegement());
glutMainLoop();
return 1;
}

glutDisplayFunc takes a function pointer as its argument. The syntax you are trying to use does not match that:
glutDisplayFunc(lineSegement());
In this statement, lineSegment() is a call to the lineSegment function, with the return value (which is void) being passed to glutDisplayFunc.
To pass the function pointer, you simply write the name of the function for glutDisplayFunc argument:
glutDisplayFunc(lineSegement);

Related

Draw shape with GLUT_DOUBLE mode

I have a code:
#include <gl/glut.h>
#include <stdio.h>
#define WinW 1000
#define WinH 500
/* ----------------------------------------------------------------------- */
bool mousedown = false;
void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, WinW, WinH, 0.0, -1.0, 1.0);
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(50, 50);
glEnd();
//glFlush();
glutSwapBuffers();
}
void myMouse(int button, int state, int x, int y){
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
mousedown = true;
}
else
mousedown = false;
}
void myMovedMouse(int mouseX, int mouseY){
if (mousedown){
//printf("%d %d\n", mouseX, mouseY);
glBegin(GL_POINTS);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(mouseX, mouseY);
glEnd();
//glFlush();
glutSwapBuffers();
}
}
/* ----------------------------------------------------------------------- */
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WinW, WinH);
glutInitWindowPosition(100, 150);
glutCreateWindow("Computer Graphic");
myInit();
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutMotionFunc(myMovedMouse);
glutMainLoop();
}
I want to draw free shape by mouse dragging.
I have try with glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)and glFlush(), it work for me.
But when I use GLUT_DOUBLE and glutSwapBuffers() in myMovedMouse(), the screen is blinking (black-white-black-white...)
I am new in OpenGL, anyone have solution for this.
Thanks for help!
When using double buffering, you have to draw all your points on each redraw. So you have to maintain a list of all the points. The steps are then:
On startup, create an empty point list. If you want nice code, define a class/struct that contains two values for the x and y position, and use a C++ container like std::vector for your list of points.
When you get a new point from mouse input, add that point to the point list, and call glutPostRedisplay().
In the myDisplay() function, draw all the points in the list.
Having to maintain a list of points may seem to add complexity compared to using single buffering. But as soon as you go a little farther, you will probably need it anyway. For example, if the user resizes the window, you will have to be able to redraw all the points anyway. So drawing the points one by one in single buffering mode will not get you very far.

OpenGL Won't Close on Mouse Click (mouseFunc) - glutKeyboardFunc is fine?

I have my program configured to close when I press the 'q' key. It was simple enough to orchestrate after reading the glutKeyboard prototype, so I thought I would also add the option to close with the right mouse button. However, no matter what I do, I can't get this to work. I'm curious if there is some subtle difference between mouseFunc and keyboardFunc that I'm missing? Here is my code:
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>
//#include <assert.h>
void init (void)
{
glClearColor (1.0, 1.0, 0.0, 0.0); /* Set background to yellow */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2d (0.0, 0.0);
glVertex2d (1.0, 0.0);
glVertex2d (0.5, 0.866);
glEnd();
glFlush (); //Display immediately
}
void keyEscape( unsigned char key, int x, int y )
{
switch ( key )
{
case 113: // 'Q' key for escape
int windowID = glutCreateWindow ("triangle");
glutDestroyWindow (windowID);
exit (0);
break;
}
glutPostRedisplay();
}
void mouseEscape( int button, int state, int x, int y )
{
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
int windowID = glutCreateWindow ("triangle");
glutDestroyWindow (windowID);
exit (0);
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition ((glutGet(GLUT_SCREEN_WIDTH)-250)/2, (glutGet(GLUT_SCREEN_HEIGHT)-250)/2);
glutCreateWindow ("triangle");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyEscape);
glutMouseFunc(mouseEscape);
glutMainLoop();
return 0;
}
I parsed out a few more keyboard shortcuts (such as z for zooming) that all work similarly, hence my use of a switch statement as opposed to an if, but that is the only real difference I can see, and attempting to use a switch case has not worked for me either. I've also tried moving the redisplay command outside the if, to no avail. Any idea why mouse closing won't cooperate, but key closing will?
Turns out I was somehow building against an old .exe that wouldn't updated even with a 'Clean Project' operation. I changed projects and now all is fine... Thanks for the assistance all the same!

error in OpenGL glutKeyboardFunc

I'm trying to use glutKeyboardFunc in OpenGL. But when I use this function in main it's giving me an error that No matching function for call to 'glutKeyboardFunc'. Why is it so?
#include<GLUT/glut.h>
#include<OpenGL/OpenGL.h>
#include<iostream>
using namespace std;
static void display(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.6, 1, 0.4);
glFlush();
}
void mykeypressed(char key, int x,int y){
}
int main(int argc, char * argv[]){
glutInit(&argc, argv);
glutInitWindowPosition(300, 300);
glutInitWindowSize(500, 500);
glutCreateWindow("ps4_1");
glutDisplayFunc(display);
glutKeyboardFunc(mykeypressed);
glutTimerFunc(10000, exit, 0);
glutMainLoop();
return 0;
}
The signature of glutKeyboardFunc is
void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));
// ^^^^^^^^
Your mykeypressed()'s first parameter is a char, not unsigned char. The two types are different.

I tried to make a window using GLUT to use OpenGL, but it gave me errors I didn't understand

I just started using Graphics Library Utility Toolkit, and Open Graphics Library (GLUT+OpenGL).
I wanted to create a window and I tried this code using various tutorials, but it didn't work.
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
void display(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glFlush();
}
int main (int argc, char **argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("My First GLUT/OpenGL Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
It gave me the error:
cannot convert 'char***' to 'char**' for argument '2' to 'void glutInit(int*, char**)'"
It gave me the error at line 13.
Could anybody please give me some information on this? Thank you!
The error message is telling you that you're passing a pointer-to-pointer-to-pointer-to-char as a second argument to glutInit. That's not what that function expects, it takes a pointer-to-pointer-to-char.
The problem comes from your signature for main. The standard two-argument main function takes an int and a pointer-to-pointer-to-char.
int main(int argc, char **argv)
Which can also be written:
int main(int argc, char *argv[])
(The two forms are equivalent.)
You're adding one more indirection level.

OpenGL particle system

I'm trying to simulate a particle system using OpenGl but I can't get it to work, this is what I have so far:
#include <GL/glut.h>
int main (int argc, char **argv){
// data allocation, various non opengl stuff
............
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
glutInitWindowPosition(100,100);
glutInitWindowSize(size, size);
glPointSize (4);
glutCreateWindow("test gl");
............
// initial state, not opengl
............
glViewport(0,0,size,size);
glutDisplayFunc(display);
glutIdleFunc(compute);
glutMainLoop();
}
void compute (void) {
// change state not opengl
glutPostRedisplay();
}
void display (void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(i = 0; i<nparticles; i++) {
// two types of particles
if (TYPE(particle[i]) == 1) glColor3f(1,0,0);
else glColor3f(0,0,1);
glVertex2f(X(particle[i]),Y(particle[i]));
}
glEnd();
glFlush();
glutSwapBuffers();
}
I get a black window after a couple of seconds (the window has just the title bar before that). Where do I go wrong?
LE: the x and y coordinates of each particle are within the interval (0,size)
Try to make these changes in your code:
move the Main function at the end of the file
glPoinSize call belongs to the Display function
then you should provide a function to handle resizing of the window glutReshapeFunc(reshape), something like this
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
glFlush is called from glutSwapBuffers function so you don't need it there
insert this code (after glutCreateWindow call) to set the initial position for the projection
glClearColor(0.2, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 10, 0.0, 10, -1.0, 1.0);