Question

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()
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top