OpenGL flickering screen - c++

I've written a simple opengl running in my ubuntu laptop. It is a small solar system including the sun and the earth, the earth rotates around the sun. The problem with my program is the screen keep blinking continuously every time I try to run it.
#include <GL/glut.h>
#define SUN_RADIUS 0.4
#define EARTH_RADIUS 0.06
#define MOON_RADIUS 0.016
GLfloat EARTH_ORBIT_RADIUS = 0.9;
GLfloat year = 0.0;
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void renderScene() {
gluLookAt(
0.0, 0.0, -4.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
glColor3f(1.0, 1.0, 0.7);
glutWireSphere(SUN_RADIUS, 50, 50);
glPushMatrix();
glRotatef(year, 0.0, 1.0, 0.0);
glTranslatef(EARTH_ORBIT_RADIUS, 0.0, 0.0);
glColor3f(0.0, 0.7, 1.0);
glutWireSphere(EARTH_RADIUS, 10, 10);
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
renderScene();
glFlush();
glutSwapBuffers();
}
void idle() {
year += 0.2;
display();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Solar System");
init();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
}

gluLookAt() multiplies by the current matrix, it does not load a new one. Multiple gluLookAt()s multiplied together aren't very meaningful.
Reload proj/modelview matrices each frame, helps prevent matrix oddities.
Let GLUT do it's job, don't call display() from idle(), use glutPostRedisplay() instead. That way GLUT knows to call display() the next time through the event loop.
All together:
#include <GL/glut.h>
#define SUN_RADIUS 0.4
#define EARTH_RADIUS 0.06
#define MOON_RADIUS 0.016
GLfloat EARTH_ORBIT_RADIUS = 0.9;
GLfloat year = 0.0;
void renderScene()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -1, 1, -1, 1, -100, 100 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
0.0, 0.0, -4.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
glColor3f(1.0, 1.0, 0.7);
glutWireSphere(SUN_RADIUS, 50, 50);
glPushMatrix();
glRotatef(year, 0.0, 1.0, 0.0);
glTranslatef(EARTH_ORBIT_RADIUS, 0.0, 0.0);
glColor3f(0.0, 0.7, 1.0);
glutWireSphere(EARTH_RADIUS, 10, 10);
glPopMatrix();
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(10.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderScene();
glutSwapBuffers();
}
void idle()
{
year += 0.2;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Solar System");
glutDisplayFunc(display);
glutIdleFunc( idle );
glutMainLoop();
}

This may be due to tearing if you aren't doing any kind of v-sync (Which it doesn't look like your code is). Try adding a sleep time to your display method (like sleep(500)). This isn't the correct way to fix this, but this will allow you to verify that it is the issue. If it is, look into adding v-sync to your application.

Related

Why does my second 3D object not have four faces in Open GL

As the title says I'm tyring to model a simple giraffe out of arraycubes in open GL wiht C++, now I got the concepts done, but ran into an issue, when I start on the neck for some reaosn I lose 5 out of the 6 faces of my cube, the example I'm following doesn't result in this. I linked a small video below to show the visual result and I'm wondering what might be causing this. If there's an easier way to go about this as well please do let me know.
Visual Result
Code Sample
#include <glut.h>
float angle[4];
GLfloat corners[8][3] = { {-0.5,0.5,-0.5},{0.5,0.5,-0.5},
{0.5,-0.5,-0.5},{-0.5,-0.5,-0.5},
{-0.5,0.5,0.5},{0.5,0.5,0.5},
{0.5,-0.5,0.5},{-0.5,-0.5,0.5} };
void drawFace(int a, int b, int c, int d) {
glBegin(GL_POLYGON);
glVertex3fv(corners[a]);
glVertex3fv(corners[b]);
glVertex3fv(corners[c]);
glVertex3fv(corners[d]);
glEnd();
}
void ArrayCube() {
glColor3f(1.0, 1.0, 1.0);
drawFace(0, 3, 2, 1);
glColor3f(1.0, 1.0, 1.0);
drawFace(3, 0, 4, 7);
glColor3f(1.0, 1.0, 1.0);
drawFace(2, 3, 7, 6);
glColor3f(1.0, 1.0, 1.0);
drawFace(1, 2, 6, 5);
glColor3f(1.0, 1.0, 1.0);
drawFace(4, 5, 6, 7);
glColor3f(1.0, 1.0, 1.0);
drawFace(5, 4, 0, 1);
}
void LowerNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.25, -0.125);
glScalef(0.0, 0.5, 0.25);
ArrayCube();
glPopMatrix();
}
void MainBody()
{
glPushMatrix();
glScalef(1.25, 0.25, 0.5);
ArrayCube();
glPopMatrix();
}
void DrawGiraffe()
{
glRotatef(angle[0], 0.0, 1.0, 0.0);
MainBody();
LowerNeck();
}
void rotate() {
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.6, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
DrawGiraffe();
glutSwapBuffers();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Basic 3D");
glutDisplayFunc(display);
init();
glutIdleFunc(rotate);
glutMainLoop();
}
For the second object (the neck) you apply a scale transformation on x that scales the x component of all the following drawn vertices to 0.0:
glScalef(0.0, 0.5, 0.25);
That 0.0 should've probably been a 1.0.
That's the reason you only see one quad in the render video: That's the quad/face (actually two faces) which still have a dimension in Y and Z. The faces that have a dimension on x are squished to degenerate quads and not displayed at all.

OpenGL rotating glRectf() function?

I'am working on a project which one my homework. I need rotate a car(not exactly but like a car this is not important I think). My car 2d. I create it with glReactf(); function. Because of I can create a rectangle with this function pixel by pixel. Like;
// Create a rectangle (0,0) to (30,30) :) Like a square, yeap :) I use it.
// Because I am working on a lot of rectangles and you know, squares are rectangles in math :)
glRectf(0, 0, 30, 30);
But I have a code. It works but it is 3d. I can't turn it 2d. Can you help me? This is not important, I am working on 2d. While I say "I can't turn it 2d" I mean I didn't get algorithm and logic on this code. This is 3d quads code, and I can turn it with rotatef() function;
#include <stdio.h>
#include <GL/glut.h>
double rotate_y = 0;
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.5, 0.5, 0.5);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, 0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(int key, int x, int y) {
if (key == GLUT_KEY_RIGHT) {rotate_y += 45;}
else if (key == GLUT_KEY_LEFT) {rotate_y -= 45;}
glutPostRedisplay();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 800);
glutCreateWindow("Rotating Test");
glutDisplayFunc(display);
glutSpecialFunc(keyboard);
glutMainLoop();
return 0;
}
I need a car(like 30x30 px quad in 2d) and I need turn it 180 degree on y axis. I want to create it like write it my first code.
I solve it, how I did it I don't know, I am serious but it done. This is my display function;
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(1, 1, 11, 11);
glFlush();
glutSwapBuffers();
}

OpenGL - The object moving everytime the menu clicked

I am developing a project and after I added menu for changing color,the object or shape in window changes color but keep moving other way or out of range every time an item in menu selected such as color. I also tried changing GLUT_DOUBLE to GLUT_SINGLE but still no luck.
#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4 //white colour for sphere
float red =1.0,green =1.0,blue=0.0,white = 0.0;
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glFlush();
}
void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
void mouse(int option){
switch(option){
case RED:
red = 1.0,green = 0.0,blue = 0.0;
break;
case GREEN:
red = 0.0,green = 1.0,blue = 0.0;
break;
case BLUE:
red = 0.0,green = 0.0,blue = 1.0;
break;
case WHITE:
red = 1.0,green = 1.0,blue = 1.0;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Testing");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
int sphere = glutCreateMenu(mouse); //change colour of pacman
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Green",GREEN);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("White",WHITE);
glutCreateMenu(mouse);
glutAddSubMenu("Pacman",sphere);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init ();
glutMainLoop();
return 0;
}
You have to do one glPopMatrix for each glPushMatrix. Ther is missing 1 glPopMatrix in the function display:
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glPopMatrix(); // <--------------------- this is missing
glFlush();
}
Since the pop is missing, the translation of the first rendering is kept on the top of the matrix stack. When the rendering is done a 2nd time with the new color, translation is applied again. This causes a progressive change of position.

OpenGL code doesn't show anything in window mode

I wanna create a program with OpenGL in my PC. I use this code
#include "stdafx.h"
#include <GL/glut.h>
bool bFullsreen = false;
int nWindowID;
void display(void)
{
//Clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
//draw white polygon (rectangle) with corners at
// (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
// Don't wait start processing buffered OpenGL routines)
//glFlush();
glutSwapBuffers();
}
void init(void)
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void keyboardFunc(unsigned char key, int x, int y) {
switch (key) {
case 'f':
bFullsreen = !bFullsreen;
if (bFullsreen)
glutFullScreen();
else {
glutSetWindow(nWindowID);
glutPositionWindow(100, 100);
glutReshapeWindow(640, 480);
}
break;
}
}
void idleFunc(void) {
glutPostRedisplay();
}
int _tmain(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
nWindowID = glutCreateWindow("Hello World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboardFunc);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}
when i run it in my PC it coesn't show anything and only show background. like this picture:
But when I switch to FullScreen mode, it shows correctly. I searched about it and I found changing GLUT_Single to GLUT_DOUBLE and glFlush() to glutSwapBuffers() but it doesn't work for me.
I have windows 10 with NVIDIA GForce 7300 LE. when I run my program in other PC it runs correctly. so
What is my problem? Is it about my graphic card or my code is incorrect? and
How can I fixed it?

How can i change my background color?

How can i change my background color?
I can't change my background color.....
i trying uninstall my vs2015... and install again again...
other my friends background color is white. it's same code.
my other laptop is good working for vs2015(same code and setting)
but my new laptop is make this problem.
please, i need help this problem
#include <glut.h>
void MyDisplay(){
float vertices[3][2]={{ 0.0,0.0 },{ 250.0,500.0 },{ 500.0,0.0 }};
float p[2] = { 75.0, 50.0 };
int i, j;
glClear(GL_COLOR_BUFFER_BIT);
for (j = 0; j<50000; j++) {
i = rand() % 3;
p[0] = (p[0] + vertices[i][0]) / 2.0;
p[1] = (p[1] + vertices[i][1]) / 2.0;
glBegin(GL_POINTS);
glVertex2fv(p);
glEnd();
} glFlush();
}
void MyInit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 500.0, 0.0, 500.0, 1, -1);
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(MyDisplay);
MyInit();
glutMainLoop();
}
void MyInit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 500.0, 0.0, 500.0, 1, -1);
}
You should play with the line glClearColor(1.0, 1.0, 1.0, 1.0);
Just change the values from 0 to 1.