PDA

View Full Version : Having some problems getting a shaded model on the screen



Roleparadise
11-23-2014, 02:43 PM
Can someone help me figure out how to get this bunny rendered correctly? The first image below is what it's intended to look like; the second picture is the first frame that renders after I run it; the third picture is every frame after the first (no idea why it changes).

1512 1515 1516

Here's my code:




load_mesh* bunny;

void lighting() {
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat light_specular[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat light_position0[] = {-1.0,-1.0,-1.0, 0.0 };

GLfloat material_ambient[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat material_diffuse[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat material_specular[] = { 0.0, 0.0, 0.0, 0.0 };

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);

glMaterialfv(GL_FRONT, GL_SPECULAR, material_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);
}

void immediateMode(std::vector<load_mesh::Triangle> gTriangles, std::vector<load_mesh::Vector3> gPositions, std::vector<load_mesh::Vector3> gNormals) {
glBegin(GL_TRIANGLES);
for (int i = 0; i < gTriangles.size(); i++) {
unsigned int k0 = gTriangles[i].indices[0];
unsigned int k1 = gTriangles[i].indices[1];
unsigned int k2 = gTriangles[i].indices[2];
glVertex3f(gPositions[k0].x * 10 + 0.1, gPositions[k0].y * 10 - 1, gPositions[k0].z * 10 /*- 1.5*/);
glNormal3f(gNormals[k0].x, gNormals[k0].y, gNormals[k0].z);
glVertex3f(gPositions[k1].x * 10 + 0.1, gPositions[k1].y * 10 - 1, gPositions[k1].z * 10 /*- 1.5*/);
glNormal3f(gNormals[k1].x, gNormals[k1].y, gNormals[k1].z);
glVertex3f(gPositions[k2].x * 10 + 0.1, gPositions[k2].y * 10 - 1, gPositions[k2].z * 10 /*- 1.5*/);
glNormal3f(gNormals[k2].x, gNormals[k2].y, gNormals[k2].z);
}
glEnd();
}

void display()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

start_timing();

// Create a light source
lighting();

//Load the model
if (!bunny) bunny = new load_mesh ("bunny.obj");

immediateMode((*bunny).gTriangles, (*bunny).gPositions, (*bunny).gNormals); // Render the model using immediate mode

// Create a camera
gluLookAt(0.0, 0.0, 0.0, // Position
0.1, -1, -1.5, // At
0.0, 1.0, 0.0); // Up vector

// Determine a window to display what the camera is viewing
glViewport (0, 0, 512, 512);

// Specify the perspective
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.1, // Left
0.1, // Right
-0.1, // Bottom
0.1, // Top
-0.1, // Near
-1000); // Far

glutPostRedisplay();
glutSwapBuffers();
}

int main(int a, char** c) {
glutInit(&a, c);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(512, 512);
glClearColor(1.0, 0.0, 0.0, 0.0);
glutCreateWindow("OpenGL Exercise");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}



The load_mesh code was provided so I know that is not the problem. Someone please point me in the right direction. Thank you!

Roleparadise
11-24-2014, 06:43 AM
Updated the first post with my current predicament; please take a look and see if you can tell what the problem is. Thanks!