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.
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'm a complete novice in OpenGL and have started following a book on this topic. The first program that they demonstrate is the Sierpinski Gasket program. Here's what the code looks like in the book:
#include<GL/glut.h>
#include<stdlib.h>
void myinit()
{
//attributes
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0); //draw in red
//set up viewing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; //triangle
GLfloat p[2]={7.5,5.0}; //arbitrary point
glClear(GL_COLOR_BUFFER_BIT); //clear the window
glBegin(GL_POINTS);
//Gasket Program
for(int i=0;i<5000;i++){
int j=rand()%3;
//compute points halfway between random vertex and arbitrary point
p[0]=(vertices[j][0]+p[0])/2;
p[1]=(vertices[j][1]+p[1])/2;
//plot the point
glVertex2fv(p);
}
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
However, when I compile the program it only displays a window completely filled with white and nothing else. Why isn't the above code working the way it should?
Swap glFlush() for glutSwapBuffers():
#include<GL/glut.h>
void display()
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT); //clear the window
//set up viewing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; //triangle
GLfloat p[2]={7.5,5.0}; //arbitrary point
glColor3f(1.0,0.0,0.0); //draw in red
glBegin(GL_POINTS);
//Gasket Program
for(int i=0;i<5000;i++)
{
int j=rand()%3;
//compute points halfway between random vertex and arbitrary point
p[0]=(vertices[j][0]+p[0])/2;
p[1]=(vertices[j][1]+p[1])/2;
//plot the point
glVertex2fv(p);
}
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
glFlush() won't actually swap the back/front buffers requested by GLUT_DOUBLE. You need glutSwapBuffers() for that.
#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".
Main function:
int _tmain(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize (500, 500);
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glutInitWindowPosition (700, 100);
glutCreateWindow("Result");
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutMouseFunc(main_mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Display2 function:
void display2()
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Green square
glColor3f(0.0,1.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.5,0.5,-1.0);
glVertex3f(0.5,1.5,-1.0);
glVertex3f(1.5,1.5,-1.0);
glVertex3f(1.5,0.5,-1.0);
glEnd();
//Red square
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0,0.0,-2.0);
glVertex3f(0.0,1.0,-2.0);
glVertex3f(1.0,1.0,-2.0);
glVertex3f(1.0,0.0,-2.0);
glEnd();
glutSwapBuffers();
}
reshape2 function
void reshape2(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glOrtho(-3.0,3.0,-3.0,3.0,0.01,3.0);
}
I have problem with depth buffer in openGL. I try to draw 2 square red and green. The red one is located behind the green one.
Red square have z value -2 while green square have z value -1. But the red square displayed at front of green square. I have enabled the depth test but still not work. What is wrong with my program?
Don't call any gl* function before you call glutCreateWindow, so move glEnable and glDepthFunc after it.
Every gl functions make calls to the context which is only created with the GLUT window. No OpenGL functions is effective before that.