I just started learning OpenGL with the help of FLTK because I need some GUI functionality. My IDE is VS13 on Windows 7 and my graphics card driver supports OpenGL 2.1.
What I did was simply to run one of their examples and try to modify it a little bit but it failed to compile with something that should be pretty basic - the glCreateShader method which is included in OpenGL 2.1, it says "Error: identifier "glCreateShader" is undefined" although there are a lot of other gl- functions that are recognized. Here is the code, my function is at the end. If I comment it out it builds and runs:
//
// "$Id: cube.cxx 8864 2011-07-19 04:49:30Z greg.ercolano $"
//
// Another forms test program for the Fast Light Tool Kit (FLTK).
//
// Modified to have 2 cubes to test multiple OpenGL contexts
//
// Copyright 1998-2010 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
#include <config.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Radio_Light_Button.H>
#include <FL/Fl_Slider.H>
#include <stdlib.h>
#include <string>
#include <FL/Fl_Gl_Window.H>
#include <FL/gl.h>
class cube_box : public Fl_Gl_Window {
void draw();
int handle(int);
public:
double lasttime;
int wire;
double size;
double speed;
cube_box(int x, int y, int w, int h, const char *l = 0)
: Fl_Gl_Window(x, y, w, h, l) {
lasttime = 0.0;
}
};
/* The cube definition */
float v0[3] = { 0.0, 0.0, 0.0 };
float v1[3] = { 1.0, 0.0, 0.0 };
float v2[3] = { 1.0, 1.0, 0.0 };
float v3[3] = { 0.0, 1.0, 0.0 };
float v4[3] = { 0.0, 0.0, 1.0 };
float v5[3] = { 1.0, 0.0, 1.0 };
float v6[3] = { 1.0, 1.0, 1.0 };
float v7[3] = { 0.0, 1.0, 1.0 };
#define v3f(x) glVertex3fv(x)
void drawcube(int wire) {
/* Draw a colored cube */
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
glColor3ub(0, 0, 255);
v3f(v0); v3f(v1); v3f(v2); v3f(v3);
glEnd();
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
glColor3ub(0, 255, 255); v3f(v4); v3f(v5); v3f(v6); v3f(v7);
glEnd();
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
glColor3ub(255, 0, 255); v3f(v0); v3f(v1); v3f(v5); v3f(v4);
glEnd();
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
glColor3ub(255, 255, 0); v3f(v2); v3f(v3); v3f(v7); v3f(v6);
glEnd();
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
glColor3ub(0, 255, 0); v3f(v0); v3f(v4); v3f(v7); v3f(v3);
glEnd();
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
glColor3ub(255, 0, 0); v3f(v1); v3f(v2); v3f(v6); v3f(v5);
glEnd();
}
void cube_box::draw() {
lasttime = lasttime + speed;
if (!valid()) {
glLoadIdentity();
glViewport(0, 0, w(), h());
glEnable(GL_DEPTH_TEST);
glFrustum(-1, 1, -1, 1, 2, 10000);
glTranslatef(0, 0, -10);
gl_font(FL_HELVETICA_BOLD, 16);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(float(lasttime*1.6), 0, 0, 1);
glRotatef(float(lasttime*4.2), 1, 0, 0);
glRotatef(float(lasttime*2.3), 0, 1, 0);
glTranslatef(-1.0, 1.2f, -1.5);
glScalef(float(size), float(size), float(size));
drawcube(wire);
glPopMatrix();
gl_color(FL_GRAY);
glDisable(GL_DEPTH_TEST);
gl_draw(wire ? "Cube: wire" : "Cube: flat", -4.5f, -4.5f);
glEnable(GL_DEPTH_TEST);
}
int cube_box::handle(int e) {
switch (e) {
case FL_ENTER: cursor(FL_CURSOR_CROSS); break;
case FL_LEAVE: cursor(FL_CURSOR_DEFAULT); break;
}
return Fl_Gl_Window::handle(e);
}
Fl_Window *form;
Fl_Slider *speed, *size;
Fl_Button *button, *wire, *flat;
cube_box *cube;
void makeform(const char *name) {
form = new Fl_Window(510 + 390, 390, name);
new Fl_Box(FL_DOWN_FRAME, 20, 20, 350, 350, "");
new Fl_Box(FL_DOWN_FRAME, 510, 20, 350, 350, "");
speed = new Fl_Slider(FL_VERT_SLIDER, 390, 90, 40, 220, "Speed");
size = new Fl_Slider(FL_VERT_SLIDER, 450, 90, 40, 220, "Size");
wire = new Fl_Radio_Light_Button(390, 20, 100, 30, "Wire");
flat = new Fl_Radio_Light_Button(390, 50, 100, 30, "Flat");
button = new Fl_Button(390, 340, 100, 30, "Exit");
cube = new cube_box(23, 23, 344, 344, 0);
Fl_Box *b = new Fl_Box(FL_NO_BOX, cube->x(), size->y(),
cube->w(), size->h(), 0);
form->resizable(b);
b->hide();
form->end();
}
int main(int argc, char **argv) {
makeform(argv[0]);
// added to demo printing
form->begin();
form->end();
// end of printing demo
speed->bounds(4, 0);
speed->value(cube->speed = 1.0);
size->bounds(4, 0.01);
size->value(cube->size = 1.0);
flat->value(1);
cube->wire = 0;
form->label("cube");
form->show(argc, argv);
cube->show();
for (;;) {
if (form->visible() && speed->value()) {
if (!Fl::check()) break;
} // returns immediately
else {
if (!Fl::wait()) break;} // waits until something happens
cube->wire = wire->value();
cube->size = size->value();
cube->speed = speed->value();
cube->redraw();
if (Fl::readqueue() == button) break;
}
return 0;
}
//////////////////////////// Testing
GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
{
GLuint shader = glCreateShader(eShaderType);
return shader;
}
Related
The screenshot below shows the desired result.
below is the actual result
I used the glutWireSphere API for drawing my sphere, however, it is not showing.
#include <GL/glut.h>
const int RED_COLOR[3] = {255, 0, 0};
const int GREEN_COLOR[3] = { 0, 0, 255};
// color to draw in
void setDrawColor(const int rgb[3]) {
float d = 255.0; // d = max value in an rgb color spectrum
glColor3f(rgb[0]/d, rgb[1]/d, rgb[2]/d); // set draw color R=x/255 G=y/255 B=z/255 where x,y,z are values in rgb channel respectively
}
void drawPlane() {
GLfloat A[3] = { -1, 0, 1 };
GLfloat B[3] = { 1, 0, 1 };
GLfloat C[3] = { 1, 0, -1 };
GLfloat D[3] = { -1, 0, -1 };
glBegin(GL_POLYGON);
glVertex3fv(A);
glVertex3fv(B);
glVertex3fv(C);
glVertex3fv(D);
glEnd();
}
GLfloat CamX = 0, CamY = 2, CamZ = 2;
// Display Call Back
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear all drawings in buffer
glLoadIdentity();
gluLookAt(CamX, CamY, CamZ, 0, 0, 0, 0, 1, 0);
drawPlane();
setDrawColor(GREEN_COLOR); // sets the drawing color to GREEN
glutWireSphere(0.2, 20, 20);
glutSwapBuffers();// Render Now i.e convert Buffer to Picture
return;
}
// Initialization
void initialize()
{
glClearColor(0.1f, 0.1f, 0.1f, 0.1f); // Set Background Color
setDrawColor(RED_COLOR); // sets the drawing color to red
glEnable(GL_DEPTH_TEST); // enable viewing the 3d
glMatrixMode(GL_PROJECTION); // change to perspective projection
glLoadIdentity(); // what does this do?
glFrustum(-1, 1, -1, 1, 2, 10); // what does this do?
glMatrixMode(GL_MODELVIEW); // what does this do?
}
// Main
int main(int argc, char* argv[])
{
glutInit(&argc, argv); // Initialize GLUT
int x = 512, y = 512; // x and y value
glutInitWindowPosition(
(int)(glutGet(GLUT_SCREEN_WIDTH) - x) / 2,
(int)(glutGet(GLUT_SCREEN_HEIGHT) - y) / 2); // Position the window's center
glutInitWindowSize(x, y); // Set the window's initial width & height
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // initialise the buffers needed. double buff, one for RGB color the other for x,y, z
glutCreateWindow("3D Bowling Game"); // Create a window with the given title
initialize(); // Custom initialisation
glutDisplayFunc(draw); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
what am i doing wrong?
When I execute the code I get a hot air balloon formed of three elements. My issue is that when I move the objects from the keyboard, the objects loose color, and become more like wire than solid.
From what I discovered until now, my trouble comes from this function call:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
But I need it to make the ropes...
/*
This program shows a hot air balloon rotating around its own axe to the left and to the right
*/
#include "glos.h"
#include<math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
void myinit(void);
void CALLBACK display(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK rotateRight(void);
void CALLBACK rotateLeft(void);
static GLfloat x = 0;
static GLfloat y = 0;
static GLfloat z = 0;
static GLfloat alfa = 0;
double PI = 3.14159265;
void myinit (void) {
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
}
void CALLBACK rotateLeft(void) { y -= 5; }
void CALLBACK rotateRight(void) { y += 5; }
void CALLBACK display (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(x, 1, 0, 0);
glRotatef(y, 0, 1, 0);
glTranslatef(0, -80, 0);
//cube = basket
glColor3f(1, 1, 0);
auxSolidCube(50);
//full sphere = baloon
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0,200,0);
glRotatef(-90, 1, 0, 0);
auxSolidSphere(130.0);
glPopMatrix();
//polygon cylinder = ropes
glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUAD_STRIP);
glColor3f(0.0, 1.0, 1.0);
for (alfa = 0; alfa <= 360; alfa+=30) {
glVertex3f(65 * sin((PI * alfa) / 180), 100, 65 * cos((PI * alfa) / 180));//top of the cylinder
glVertex3f(15 * sin((PI * alfa) / 180),0, 15 * cos((PI * alfa) / 180));//base of the cylinder
}
glEnd();
glPopMatrix();
glPopMatrix();
glFlush();
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
if (!h) return;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-400,400, -400 *(GLfloat)h / (GLfloat)w, +400.0*(GLfloat)h/(GLfloat)w, -1000.0, 1000.0);
else
glOrtho (-400*(GLfloat)w / (GLfloat)h, 400.0*(GLfloat)w/(GLfloat)h, -400, 400.0, -1000.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
auxInitPosition (100, 0, 600, 400);
auxInitWindow ("Hot air balloon");
myinit ();
auxKeyFunc(AUX_RIGHT, rotateRight);
auxKeyFunc(AUX_LEFT, rotateLeft);
auxReshapeFunc (myReshape);
auxMainLoop(display);
return(0);
}
OpenGL is a state engine. Once a state has been set, it is retained until it is changed again, even beyond frames. Therefore, you need to set the polygon mode GL_FILL before rendering the solid geometry:
void CALLBACK display (void)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// render solid geometry
// [...]
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render wireframe geometry
// [...]
}
VS2019 says that GLFWwindow* is not initialized. It says it as a warning and then does not compile.
Also it worked before and it says it only in the start_window function.
...main.cpp(19): error C4700: uninitialized local variable 'window' used
...Done building project "CityEngine.vcxproj" -- FAILED.
I tried:
(Advice from BDL) To initialize it with nullptr - invisible and unresponsive window.
(VS IntelliSense) To do window{}. Same result.
I have "Treat warnings as errors" off.
main.cpp
#include "city/stdheader.h"
#include "city/city.h"
//Target board :)
//put colors in front of stuff (thanks stack overflow)
float color1[3] = { 1.0, 1.0, 1.0 };
float color2[3] = { 1.0, 0.0, 0.0 };
float colorA[3] = { 0.3, 0.3, 0.3 };
float vertices1[8] = { 0.9, 0.9, -0.9, 0.9, -0.9, -0.9, 0.9, -0.9 };
float vertices2[8] = { 0.7, 0.7, -0.7, 0.7, -0.7, -0.7, 0.7, -0.7 };
float vertices3[8] = { 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5 };
float vertices4[8] = { 0.3, 0.3, -0.3, 0.3, -0.3, -0.3, 0.3, -0.3 };
float vertices5[8] = { 0.1, 0.1, -0.1, 0.1, -0.1, -0.1, 0.1, -0.1 };
float vertices1A[6] = { 0, 0, -0.2, 0.2, -0.2, -0.1 };
float vertices2A[6] = { -0.4, 0.3, -0.6, 0.5, -0.6, 0.2 };
float vertices3A[6] = { 0.4, -0.6, 0.2, -0.4, 0.2, -0.7 };
int main() {
glfwInit();
GLFWwindow* window;
City::start_window(window, 680, 480);
while (!CtWINDOW_OPEN(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
City::drawpoly(vertices1, color2);
City::drawpoly(vertices2, color1);
City::drawpoly(vertices3, color2);
City::drawpoly(vertices4, color1);
City::drawpoly(vertices5, color2);
//Arrows
City::drawtriangle(vertices1A, colorA);
City::drawtriangle(vertices2A, colorA);
City::drawtriangle(vertices3A, colorA);
CtPROCESS(window);
}
glfwTerminate();
}
cityengine.cpp
#include "stdheader.h"
#include "city.h"
#include <GLFW/glfw3.h>
namespace City {
void terminate_window() {
glfwTerminate();
}
void start_window(GLFWwindow* window, float width, float height) {
glfwInit();
window = glfwCreateWindow(width, height, "CITYEngine", 0, 0);
glfwMakeContextCurrent(window);
if (!window)
{
terminate_window();
}
//while (!WINDOW_OPEN(CITYwindow)) {
// PROCESS(CITYwindow);
// }
}
void drawpoly(float vertices[8], float color[3]) {
glBegin(GL_POLYGON);
glColor3f(color[0], color[1], color[2]);
glVertex2f(vertices[0], vertices[1]);
glVertex2f(vertices[2], vertices[3]);
glVertex2f(vertices[4], vertices[5]);
glVertex2f(vertices[6], vertices[7]);
glEnd();
}
void drawtriangle(float vertices[6], float color[3]) {
glBegin(GL_TRIANGLES);
glColor3f(color[0], color[1], color[2]);
glVertex2f(vertices[0], vertices[1]);
glVertex2f(vertices[2], vertices[3]);
glVertex2f(vertices[4], vertices[5]);
glEnd();
}
}
city.h
#pragma once
#define CtWINDOW_OPEN(window) glfwWindowShouldClose(window)
#define CtPROCESS(window) glfwSwapBuffers(window); glfwPollEvents()
namespace City {
void start_window(GLFWwindow* window, float width, float height);
void terminate_window();
void drawpoly(float vertices[8], float color[3]);
void drawtriangle(float vertices[6], float color[3]);
}
stdheader (probably not needed for this question but im going to include it anyway)
#pragma once
#include <iostream>
#include <GLFW/glfw3.h>
using std::string;
using std::cout;
using std::cin;
Any clue?
PS. Do anyone know why?
C4700 is usually a warning and only behaves like an error when "Treat warnings as errors" is enabled
To get rid of this warning, you should initialize the variable with:
GLFWwindow* window = nullptr;
makes the window invisible and not responding
This happens because the window parameter of start_window does not do what you want. You want to set the value of a pointer from inside the method, but for this you have to pass a reference to a pointer (or a pointer to a pointer). Your current code passes the pointer by value but does not return the new window point to the calling method. You can use something like the next code sample to fix the problem:
void start_window(GLFWwindow*& window, float width, float height)
{
glfwInit();
auto createdWindow = glfwCreateWindow(width, height, "CITYEngine", 0, 0);
glfwMakeContextCurrent(createdWindow );
if (!createdWindow )
{
terminate_window();
window = nullptr;
}
else
{
window = createdWindow;
}
}
I am trying to generate the Sierpinski Gasket using a function that draws dot patterns and will generate the gasket.
But when I compile and run the program it displays nothing but black screen. What's causing the problem?
Here is my code:
#include <Windows.h>
#include <gl/GL.h>
#include <glut.h>
void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
class GLintPoint {
public:
GLint x, y;
};
int random(int m) {
return rand() % m;
}
void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void Sierpinski(void) {
GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}};
int index = random(3);
GLintPoint point = T[index];
for (int i = 0; i < 1000; i++)
{
index = random(3);
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(drawDot);
myInit();
glutMainLoop();
}
If you want to draw black dots on a white background, then you have to clear glClear the background:
glClear( GL_COLOR_BUFFER_BIT );
Note, glClearColor sets the color which is used to clear the view port, but doesn't clear anything itself.
Your code should look somehow like this:
void drawDot(GLint x, GLint y)
{
glVertex2i(x, y);
}
void Sierpinski(void) {
GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}};
int index = random(3);
GLintPoint point = T[index];
glClearColor(1.0, 1.0, 1.0, 1.0); // set up white clear color
glClear( GL_COLOR_BUFFER_BIT ); // clear the back ground (white)
glMatrixMode( GL_MODELVIEW );
glPushMatrix(); // setup model matrix
glScalef( 1.5f, 1.5f, 1.0f ); // scale the point distribution
glColor3f( 0.0f, 0.0f, 0.0f ); // set black draw color
glPointSize( 5.0f ); // set the size of the points
glBegin(GL_POINTS);
for (int i = 0; i < 1000; i++)
{
index = random(3);
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glEnd();
glPopMatrix(); // reset model matrix
glFlush();
}
I've got a taks from university and have to make a small example of solar system, the objects have to rotate etc. The problem is that when I do not call GluLookAt() everything looks fine, but I would like to change the view and when I call the function, there occurs that one orbit renders completely strangely.
I do not know if problem is with wrong creation of the first orbit, or with the proper values in gluLookAt parameters. Can anyone help?
Here's how it looks without calling gluLookAt():
Here's how it looks after gluLookAt():
Here's the code:
#include "stdafx.h"
#include <GL\glut.h>
#include <math.h>
GLfloat yRotated=1;
GLfloat movement = 0;
void drawCircle(float r) { // radius
glBegin(GL_LINE_LOOP);
for (int i = 0; i <= 300; i++) {
double angle = 2 * 3.14 * i / 300;
double x = r*cos(angle);
double y = r*sin(angle);
glVertex3d(x, y, -5.5);
}
glEnd();
}
void display(void) {
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//gluLookAt(5, 5, 5, 0, 0, -8, 0, 1, 0); // 3rd coordinate - depth
float radius1 = 6;
float radius2 = 1;
//first orbit
glColor3f(1, 1, 1);
glPushMatrix();
glTranslatef(0, 0, -5.5);
drawCircle(radius1);
glPopMatrix();
//second orbit with rotation
glPushMatrix();
glRotatef(yRotated, 0, 0, 1);
glPushMatrix();
glTranslatef(radius1 / 2, 0, 0);
drawCircle(radius2);
glPopMatrix();
glPopMatrix();
//first czajnik
glColor3f(0.8, 0.2, 0.1);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.5);
// glScalef(1.0, 1.0, 1.0);
glRotatef(yRotated, 0, 0, 1);
glRotatef(90, 1, 0, 0);
glutSolidSphere(1,20,20);
//second czajnik
glPushMatrix();
glColor3f(0, 0, 1);
glTranslatef(radius1/2, 0, 0);
glRotatef(yRotated, 0, 1, 0);
glutSolidSphere(0.5, 20, 20);
//third czajnik
glPushMatrix();
glTranslatef(radius2, 0, 0);
glColor3f(1, 1, 0);
glRotatef(yRotated, 0, 1, 0);
glutSolidSphere(0.2, 20, 20);
glPopMatrix();
//second czajnik pop
glPopMatrix();
//first czajnik pop
glPopMatrix();
glFlush();
}
void idle() {
yRotated += 0.1;
Sleep(2);
display();
}
void myReshape(int w, int h) {
if (w == 0 || h == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0, (GLdouble)w / (GLdouble)h, 0.5, 20.0);
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(900, 600);
glutCreateWindow("Solar system");
//window with a title
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClearColor(0, 0, 0, 1.0);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
Some of your objects are at different z values, e.g. 1st orbit at -5.5, second at 0, because you "popped" the matrix.
In general, do not do so many push\pops nested into each other, matrix stack isn't made of rubber.
There is more efficient circle drawing procedure than to calculate sine and cosine for each step, e.g. to get advantage of circle being a figure of rotation:
inline void circle(F32 r, U32 quality)
{
if (r < F_ALMOST_ZERO) return;
F32 th = M_PI /(quality-1);
F32 s = sinf(th);
F32 c = cosf(th);
F32 t;
F32 x = r;
F32 y = 0;
::glBegin (GL_LINE_LOOP);
for(U32 i = 0; i < quality; i++)
{
glVertex2f(x, y);
t = x;
x = c*x + s*y;
y = -s*t + c*y;
}
::glEnd();
}
it can be optimized further by using symmetry, but this one is the basis.