I have attempted to start programming with OpenGl using this tutorial, and have the following code so far:
#include <gl/glut.h> // Include the GLUT header file
void display (void)
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
// Set up a basic display buffer (only single buffered for now)
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
// Set the title for the window
glutCreateWindow("Your first OpenGL Window!");
glutDisplayFunc(display);
glutMainLoop();
glClearColor(1.f, 0.f, 0.f, 1.f); // Clear the background of our window to red
return 0;
}
I build and run the project in Eclipse, it compiles all fine, but nothing happens (no windows pop up or anything). Can anybody tell me what I could be doing wrong?
glutInitDisplayMode (GLUT_SINGLE);
You also need to define the kind of framebuffer format you want, i.e. add a GLUT_RGBA, at least (and you probably also want a depth buffer). And there are only few cases where one not wants a double buffer. So: glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
Then your display function will not be called, unless you add a glutDisplayFunc(display); after glutCreateWindow.
glutMainLoop();
glClearColor(1.f, 0.f, 0.f, 1.f);
glutMainLoop does not return. And even if it did, glClearColor had no effect at that place in the program.
Related
The background color of my scene is black. How can I change this color?
Looks like I'm doing something wrong because glClearColor() function is not working: I tried to change the values but nothing happened. I'm new to OpenGL and programming in general.
#include <GL/glut.h>
void Ayarlar(void);
void CizimFonksiyonu(void);
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("ilk OpenGL programim");
glutDisplayFunc(CizimFonksiyonu);
glutMainLoop();
Ayarlar();
return 0;
}
void Ayarlar(void) {
glClearColor(1 ,0 ,0 , 1);
glShadeModel(GLU_FLAT);
}
void CizimFonksiyonu(void) {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
Ayarlar() has to be called be for glutMainLoop(). glutMainLoop enters the GLUT event processing loop and never returns. You have to set the OpenGL states before.
glutDisplayFunc(CizimFonksiyonu);
Ayarlar();
glutMainLoop();
I used a book called Computer Graphices using OpenGL.
at page number 51 i found this code
#include <windows.h>
#include "glut.h"
//<<<<<<<<<<<<<<<<< method(s) >>>>>>>>>>>>>>>>>>>
void My_Display(void);
void My_Inti(void);
//<<<<<<<<<<<<<<<<< main method >>>>>>>>>>>>>>>>>>
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("my second try ");
glutDisplayFunc(My_Display);
My_Inti();
glutMainLoop();
return 0;
}
//<<<<<<<<<<<<<<<<<<<<<<<< IMPLEMENTING METHOD(S) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
//<<<<<<<<<<<<<<<< My_Inti >>>>>>>>>>>>>>>>>>>>>>>>>
void My_Inti(void)
{
glClearColor(1, 1, 1, 0); // white color
glColor3f(0, 0, 0); // Black color
glPointSize(10); // point size is 10 pixel this is big .
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
//<<<<<<<<<<<<<<<< My_Display >>>>>>>>>>>>>>>>>>>>>>
void My_Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINT);
glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);
glEnd();
glFlush();
}
all what i add to this code is the comments and i change a little at the variable ; nothing more .
When we come to the problem this code work fine but it didn't creat the three points at the display method ?
The problem is just one missing letter. Instead of this:
glBegin(GL_POINT);
The correct value is:
glBegin(GL_POINTS);
The first thing I would do any time you get no rendering, or not the expected rendering, is to call glGetError(), and see if it returns an error. I admit that I didn't see this problem initially, but calling glGetError() would have pointed it out quickly.
BTW: In case anybody else is surprised that there are both GL_POINT and GL_POINTS enums in OpenGL. GL_POINT is one of the possible arguments to glPolygonMode(), as opposed to GL_POINTS which denotes one of the possible primitive types for draw calls.
It does not work because there is an error in the source code (unlikely) or you mis-copied the argument for glBegin(GL_POINT); (likely).
Using
glBegin(GL_POINTS);
I got this image:
i want to change a wireSphere during an animation with openGL.
But it won´t work.
What must i do that it changes a color during an animation?
So far i only have this :
glColor3f(1.0,1.0,0.0); //yellow color
glutWireSphere(0.5,60,50);
Works for me. Here's a minimal GLUT program that uses your code, and successfully displays a yellow sphere on my Mac:
#include <GLUT/glut.h>
static void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 0.0f);
glutWireSphere(0.5f, 50, 60);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutCreateWindow("WireSphere");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
I suspect that there's some state you're setting outside the code you copied that prevents this from working for you. But it's hard to speculate what that could be. I think you'll need to post more code, or at least be more specific about the unexpected behavior than "it won't work".
The exact same code snippet is working on another machine but its not working properly for me. The GLUT is working absolutely fine as it open the created window but the line segment is not shown on the window which means there is a problem with opengl. It is not even changing the background color of the window.
I even test the opengl on my windows with a testing application and its working fine.
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
void init(void){
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 400.0, 0.0, 400.0);
}
void linesegment(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2i(180, 15);
glVertex2i(10, 145);
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(400, 400);
glutCreateWindow("Testing Open GL");
init();
glutDisplayFunc(linesegment);
glutMainLoop();
return 0;
}
Most likely you're running into the rather new class of problems introduced by compositing graphics systems (Aero in Windows, Quartz Extreme on MacOS X and a multitude of various programs on Linux/X11). The gist of the problem is, that compositing is inherently double buffered: There's always an offscreen (back) buffer for the window to be drawn. And when a program indicates that it's finished with drawing the compositor will integrate it into the on-screen image.
This however brings a few caveats. Most importantly, single buffered drawing somehow needs to indicate that it's finished. While OpenGL's glFinish call should suffice from a implementors point of view, most compositing systems are not sensitive to it. You'll have to create a double buffered window pixel format and do a buffer swap to make the compositor present your image.
So for your program:
replace GLUT_SINGLE with GLUT_DOUBLE in glutInitDisplayMode
replace glFlush with glutSwapBuffers
I just started trying out OpenGL and I'm having a strange issue.
If I compile and execute using g++ test.c -lGL -lGLU -lglut then ./a.out, a sepearate window opens (of the size specified in the code). But instead of having the output of the code in it, it has a screenshot of the screen inside it (of the portion of the screen it overlaps).
This is definitely not something to do with the code, since it's running fine on my friend's computer. But I need to fix it on my PC.
I'm on Linux Mint 15 64bit (HP DV6 6121tx).
So I'm not able to proceed further. Here's my code:
#include <GL/gl.h>
#include <GL/glut.h>
#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D (-600,600,-400,400);
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(1200,800);
glutCreateWindow("ABCD");
init();
glutMainLoop();
return 0;
}
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
^^^^^^^^^^^
You've asked for a single-buffered window so you'll have to glFlush()/glFinish() to force queued output to the screen.
Add a glFinish() to the end of init().