0

I am trying to render a sphere without using the gluSphere() function. But this code is not rendering any sphere. I am unable to find out exactly where the error lies.

import sys
import math
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import pygame


user_theta = 0
user_height = 0

def draw(radius,lats,longs) :
    for i in range(0,lats):
        lat0 = math.pi*(-0.5 + (i-1)/lats)
        z0   = math.sin(lat0)
        zr0 = math.cos(lat0)

        lat1 = math.pi*(-0.5 + i/lats)
        z1 = math.sin(lat1)
        zr1 = math.cos(lat1)

        glColor3f(0.0,0.0,1.0)
        glBegin(GL_QUAD_STRIP)
        for j in range(0,longs):
            lng = 2*math.pi*(j-1)/longs
            x=  math.cos(lng)
            y = math.sin(lng)

            glNormal3f(x * zr0, y * zr0, z0)
            glVertex3f(x * zr0, y * zr0, z0)
            glNormal3f(x * zr1, y * zr1, z1)
            glVertex3f(x * zr1, y * zr1, z1)
        glEnd()

        glFlush()

def display() :
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glColor3f(1.0, 1.0, 1.0)
    glShadeModel(GL_SMOOTH)
    draw(1.0, 10, 10)
    glutSwapBuffers()

def computeLocation():
        x = 2 * math.cos(user_theta)
        y = 2 * math.sin(user_theta)
        z = user_height
        d = math.sqrt(x * x + y * y + z * z)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        #glFrustum(-d * 0.5, d * 0.5, -d * 0.5, d * 0.5, d - 1.1, d + 1.1)
        #gluLookAt(x, y, z,  0, 0, 0,  0, 0, 1)
        gluOrtho2D(0.0, 640.0, 0.0, 480.0)

def init():
    glClearColor(0.0, 0.0, 0.0, 0.0)
    computeLocation()




glutInit(sys.argv)
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize (500, 500)
glutInitWindowPosition (100, 100)
glutCreateWindow ('ROBOT')
init ()
glutDisplayFunc(display)
##glutReshapeFunc(reshape)
##glutKeyboardFunc(keyboard)
glutMainLoop()
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • Just a point - it looks like you're running glFlush() for each slice of your sphere. Take that out. It's really not necessary to even call glFlush() at all for this example. – AudioGL Apr 18 '14 at 22:19
  • removed but the code is still not working – user3049348 Apr 18 '14 at 22:22

2 Answers2

0

I see a couple of problems here. While I haven't used python for this kind of code, I believe it produces an integer result when you divide two integers, like most programming languages. Take a close look at the following line, and a couple of other similar lines:

lat0 = math.pi*(-0.5 + (i-1)/lats)

The (i-1)/lats division will always produce 0, resulting in all of your polygons to be degenerate, and nothing being drawn.

The other main problem is your coordinate range. Your gluOrtho2D call sets the coordinate ranges to (0..640) and (0..480). But your coordinates are in the range (-1..1). So your whole geometry ends up on about one pixel.

Removing the gluOrtho2D call, and adding a type cast to execute the divisions in float, gives me a blue and mostly circular shape. This is with the code converted to C++, my system didn't have these python modules.

The code has more bugs. For example, your first loop starts the index at 0, but then subtracts one from the index to calculate the first angle. But I'm sure that you can figure out the remaining issues once you have something rendering.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Thanks.You were right (i-1)/lats was giving 0 because of type int. Typecasting int to float worked for me. And changed the glOrtho2D. Infact put a comment to glOrtho2D and removed the comment from the glFrustum and gluLookAt. – user3049348 Apr 19 '14 at 13:31
-1

I see the place where the vertices and normals are uploaded, but do not see any glDraw* call being made, without which nothing will be rendered.

vallentin
  • 23,478
  • 6
  • 59
  • 81
Prabindh
  • 3,356
  • 2
  • 23
  • 25
  • There's no `glDraw*()` call, because OP is using the old and deprecated fixed-function pipeline... – vallentin Apr 18 '14 at 14:53
  • Thats the first time I hear that the Fixed function pipeline does not need a draw call. The fixed function pipeline also needs a draw call. Can you point me to code that does not use a glDraw* call, and able to render, or the spec ? – Prabindh Apr 18 '14 at 15:02
  • Or are you referring to the GL pipeline (not GLES) ? – Prabindh Apr 18 '14 at 15:35
  • In the fixed-function pipeline, using `glBegin(...)` and `glEnd()` doesn't require any further `glDraw*(...)` call! – vallentin Apr 18 '14 at 16:06