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();
Related
I am changing the window background color using the Glut library, which isn't working. The code works perfectly on my peer's computers but not on mine. I am using Visual Studio on Windows 10, with intel i5,8th gen, with integrated graphics.
I searched on Reddit threads, and the only thing relevant was that integrated graphics may be the issue.
Is integrated graphics really the issue or something else?
Are there any solutions to this problem, except changing the graphic cards?
Note- The library should be OpenGL, glut, glu only...
Code:
#include <GL/glut.h>
#include <gl/GLU.h>
#include <iostream>
using namespace std;
void draw() {}
void display() {
glClearColor(1.0, 1.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, 640, 0, 480);
// draw();
glFlush();
return;
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("House");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
I tried this code, and although it renders a window, it is black in color, which it should not be. It should be (as the question) any random color. Are there any solutions i can try?
#include <stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<GL/glut.h>
void display (void)
{
glClearColor(1.f, 0.f, 0.f, 1.f);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Colorcube Viewer");
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
i am not able to figure out whats the problem with this code ?
it does not give me a red window.
You need to call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); after setting the clear color (because you have depth test enabled, make sure that you're clearing both the color buffer AND the depth buffer
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".
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.
Hi my program is supposed to display a solid red colored sphere in the center of the screen, all i am getting is the boundary of the sphere :
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutCreateWindow("Sphere");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutMainLoop();
return 0;
}
void renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glutSolidSphere(2.5, 50, 40);
glutSwapBuffers();
}
Try adding glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); before your glutSolidSphere(2.5, 50, 40);
What do you mean "boundary"?
Solid doesn't mean filled, it means the surface contains no openings. This is in contrast to glutWireSphere, which is just the wire frame.