Jump to content

  • Log In with Google      Sign In   
  • Create Account

Icosahedron not rendering properly


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 chinu3d   Members   -  Reputation: 126

Like
0Likes
Like

Posted 07 September 2012 - 06:28 AM

Hi everyone,

I am just a noob to OpenGL and this is in fact my first OpenGL code written for Mac OSX. I really got stuck into an odd problem of this simple icosahedron not rendering up properly when I light it up. The problem could either be that the polys are not oriented properly or that the normal computations are incorrect. I checked both of the posdibilities but couldn't really figure out what went wrong. Here is the full code -


#import "oglView.h"
#import <GLUT/glut.h>
#import "Utilities.h"

#define X 52.5731112119133606
#define Z 85.0650808352039932
#define ORIGIN_X 200.0
#define ORIGIN_Y 200.0
#define ORIGIN_Z 0.0

@implementation oglView

void reshape (int w, int h);
void subdivide(float *v1, float *v2, float *v3, long depth);
void drawtriangle(float *v1, float *v2, float *v3);
void normalize(float *v);
void scaleVector(float *v, float scaleFactor);

- (id)initWithFrame:(NSRect)frameRect
pixelFormat:(NSOpenGLPixelFormat *)format {

self = [super initWithFrame:frameRect pixelFormat:format];
[self initOpenGL];
return self;
}

- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder: coder];
[self initOpenGL];
return self;
}

- (void)initOpenGL {
NSOpenGLContext *glcontext;
glcontext = [self openGLContext];
[glcontext makeCurrentContext];

glClearColor(0.1, 0.1, 0.1, 0.0); // background color is dark gray
glClear(GL_COLOR_BUFFER_BIT); // clear the view
glOrtho(0.0, 400, 0.0, 400, -1.0, 1.0); // setup our 400x400 coords

glutReshapeFunc(reshape);
}

- (void)drawRect:(NSRect)dirtyRect {
NSLog(@"drawRect");

glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT); // erase the screen

glMatrixMode(GL_MODELVIEW); // use model matrix
glLoadIdentity(); // reset matrix
glOrtho(0.0, dirtyRect.size.width, 0.0, dirtyRect.size.height, -1000.0, 1000.0); // we reset, so setup coords again

glTranslatef(ORIGIN_X, ORIGIN_Y, ORIGIN_Z); // move to where we want to draw the triangle. Calling this will also translate the origin to that particular position

//Set state variables:

static GLfloat vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
static GLuint tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
int i;


//Lighting and material calculations
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 50.0, 50.0, 50.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

for (int i = 0; i < 20; i++)
subdivide(vdata[tindices[i][0]], vdata[tindices[i][1]], vdata[tindices[i][2]], 3);


glFinish();

}

void subdivide(float *v1, float *v2, float *v3, long depth)
{
if (depth == 0)
{
drawtriangle(v1, v2, v3);
return;
}

GLfloat v12[3], v23[3], v31[3];

for (int i=0 ; i<3 ; i++)
{
v12[i] = v1[i] + v2[i];
v23[i] = v2[i] + v3[i];
v31[i] = v3[i] + v1[i];
}

normalize(v12);
normalize(v23);
normalize(v31);

scaleVector(v12, 100.0);
scaleVector(v23, 100.0);
scaleVector(v31, 100.0);

subdivide(v1, v12, v31, depth-1);
subdivide(v2, v23, v12, depth-1);
subdivide(v3, v31, v23, depth-1);
subdivide(v12, v23, v31, depth-1);
}

void drawtriangle(float *v1, float *v2, float *v3)
{
glBegin(GL_TRIANGLES);
glNormal3fv(v1);
glVertex3fv(v1);
glNormal3fv(v2);
glVertex3fv(v2);
glNormal3fv(v3);
glVertex3fv(v3);
glEnd();
}

void normalize(float *v)
{
float modulus = sqrt((v[0]) * (v[0]) + (v[1]) * (v[1]) + (v[2]) * (v[2]));

v[0] = v[0] / modulus;
v[1] = v[1] / modulus;
v[2] = v[2] / modulus;
}

void scaleVector(float *v, float scaleFactor)
{
v[0] = scaleFactor * (v[0]);
v[1] = scaleFactor * (v[1]);
v[2] = scaleFactor * (v[2]);
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

@end


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS