//--------------------------------------------------------------
// [Assignment4]
// Fill the following function to create display list
// of the obj file to show it as polygon, using texture and normal information (if any)
//--------------------------------------------------------------
void G308_Geometry::CreateGLPolyGeometry(GLuint shaderID) {
	if (m_glGeomListPoly != 0)
		glDeleteLists(m_glGeomListPoly, 1);

	// Assign a display list; return 0 if err
	m_glGeomListPoly = glGenLists(1);
	glNewList(m_glGeomListPoly, GL_COMPILE);

	//Your code here
	for (int i = 0; i < m_nNumPolygon; i++){
		G308_Triangle t = m_pTriangles[i];
			if (normal && shaderID){
				glm::vec3 v0 = glm::vec3(m_pVertexArray[t.v1].x, m_pVertexArray[t.v1].y, m_pVertexArray[t.v1].z);
				glm::vec3 v1 = glm::vec3(m_pVertexArray[t.v2].x, m_pVertexArray[t.v2].y, m_pVertexArray[t.v2].z);
				glm::vec3 v2 = glm::vec3(m_pVertexArray[t.v3].x, m_pVertexArray[t.v3].y, m_pVertexArray[t.v3].z);

				glm::vec2 uv0 = glm::vec2(m_pUVArray[t.t1].u, m_pUVArray[t.t1].v);
				glm::vec2 uv1 = glm::vec2(m_pUVArray[t.t2].u, m_pUVArray[t.t2].v);
				glm::vec2 uv2 = glm::vec2(m_pUVArray[t.t3].u, m_pUVArray[t.t3].v);

				glm::vec3 deltaPos1 = glm::normalize(v1-v0);
				glm::vec3 deltaPos2 = glm::normalize(v2-v0);

				glm::vec2 deltaUV1 = glm::normalize(uv1 - uv0);
				glm::vec2 deltaUV2 = glm::normalize(uv2 - uv0);

				float r = 1.f/ ((deltaUV1.x * deltaUV2.y) - (deltaUV1.y * deltaUV2.x));
				glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y)*r;
				glMultiTexCoord3fv(GL_TEXTURE1, &tangent[0]);


			}
		glBegin( GL_TRIANGLES );
			glNormal3fv(&m_pNormalArray[t.n1].x);
//			if (texture) glTexCoord2fv(&m_pUVArray[t.t1].u);
			if (texture || normal) glTexCoord2f(m_pUVArray[t.t1].u*textureScale, m_pUVArray[t.t1].v*textureScale);
			glVertex3fv(&m_pVertexArray[t.v1].x);

			glNormal3fv(&m_pNormalArray[t.n2].x);
//			if (texture) glTexCoord2fv(&m_pUVArray[t.t2].u);
			if (texture || normal) glTexCoord2f(m_pUVArray[t.t2].u*textureScale, m_pUVArray[t.t2].v*textureScale);
			glVertex3fv(&m_pVertexArray[t.v2].x);

			glNormal3fv(&m_pNormalArray[t.n3].x);
//			if (texture) glTexCoord2fv(&m_pUVArray[t.t3].u);
			if (texture || normal) glTexCoord2f(m_pUVArray[t.t3].u*textureScale, m_pUVArray[t.t3].v*textureScale);
			glVertex3fv(&m_pVertexArray[t.v3].x);


		glEnd();
	}
//	glFlush();


	glEndList();
}
Пример #2
0
void
fge_mesh_draw(struct fge_mesh mesh)
{
	int	i;

	glBegin(GL_TRIANGLES);
	for (i = 0; (unsigned)i < mesh.count_faces; i++ ){
		glNormal3fv((float*)&mesh.vertices[mesh.faces[i].index[0]].n);
		glMultiTexCoord3fv(GL_TEXTURE1_ARB, (float*)&mesh.vertices[mesh.faces[i].index[0]].t);
		glMultiTexCoord3fv(GL_TEXTURE2_ARB, (float*)&mesh.vertices[mesh.faces[i].index[0]].b);
		glMultiTexCoord2fv(GL_TEXTURE0_ARB, (float*)&mesh.vertices[mesh.faces[i].index[0]].tex);
		glVertex3fv((float*)&mesh.vertices[mesh.faces[i].index[0]].pos);

		glNormal3fv((float*)&mesh.vertices[mesh.faces[i].index[1]].n);
		glMultiTexCoord3fv(GL_TEXTURE1_ARB, (float*)&mesh.vertices[mesh.faces[i].index[1]].t);
		glMultiTexCoord3fv(GL_TEXTURE2_ARB, (float*)&mesh.vertices[mesh.faces[i].index[1]].b);
		glMultiTexCoord2fv(GL_TEXTURE0_ARB, (float*)&mesh.vertices[mesh.faces[i].index[1]].tex);
		glVertex3fv((float*)&mesh.vertices[mesh.faces[i].index[1]].pos);

		glNormal3fv((float*)&mesh.vertices[mesh.faces[i].index[2]].n);
		glMultiTexCoord3fv(GL_TEXTURE1_ARB, (float*)&mesh.vertices[mesh.faces[i].index[2]].t);
		glMultiTexCoord3fv(GL_TEXTURE2_ARB, (float*)&mesh.vertices[mesh.faces[i].index[2]].b);
		glMultiTexCoord2fv(GL_TEXTURE0_ARB, (float*)&mesh.vertices[mesh.faces[i].index[2]].tex);
		glVertex3fv((float*)&mesh.vertices[mesh.faces[i].index[2]].pos);
	}
	glEnd();
}
Пример #3
0
void CubeMap::draw(float scale)
{
    GLenum texture_unit = GL_TEXTURE3;
    glUseProgram(0);

    bind(texture_unit);
    glActiveTexture(texture_unit);
    glEnable(GL_TEXTURE_CUBE_MAP);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    pushGLMatrix(GL_MODELVIEW); {
        glScalef(scale, scale, scale);
        glBegin(GL_QUADS); {
            /* For each vertex of each face of the cube... */
            for (int i=0; i<4*6; i++) {
                glMultiTexCoord3fv(texture_unit, vertex[i]);
                glVertex3f(vertex[i][0], vertex[i][1], vertex[i][2]);
            }
        } glEnd();
    } popGLMatrix(GL_MODELVIEW);
    glDisable(GL_TEXTURE_CUBE_MAP);
    glActiveTexture(GL_TEXTURE0);
}
Пример #4
0
GLuint HalfEdgeModel::CreateEdgeDisplayList( unsigned int flags )
{
	bool normals =    (flags & WITH_NORMALS) > 0;
	bool facetNorms = (flags & WITH_ADJACENT_FACE_NORMS) > 0;
	bool failure = false;
	float fnorm1[3], fnorm2[3];
	Solid *sobj = (Solid *)solid;
	GLuint list = glGenLists( 1 );
	glNewList( list, GL_COMPILE );


	glBegin( GL_LINES );
	Edge *currEdge = sobj->sedges;
	for ( ; currEdge; ) 
	{
		
		if( !currEdge->he1 && !currEdge->he2)
		{
			currEdge = currEdge->next;
			if (!currEdge || currEdge == sobj->sedges) break;
			continue;
		}
		
		if (facetNorms) 
		{
			if(!currEdge->he1)
			{ fnorm1[0] = 0; fnorm1[1] = 0; fnorm1[2] = 0;}
			else
				ComputeFaceNormal( fnorm1, (void *)(currEdge->he1->hloop->lface) );
			if(!currEdge->he2)
			{ fnorm2[0] = 0; fnorm2[1] = 0; fnorm2[2] = 0; }
			else
				ComputeFaceNormal( fnorm2, (void *)(currEdge->he2->hloop->lface) );
			glMultiTexCoord3fv( GL_TEXTURE6, fnorm1 );
			glMultiTexCoord3fv( GL_TEXTURE7, fnorm2 );
		}
		if(!currEdge->he1 || !currEdge->he2)
		{
			HalfEdge *he = currEdge->he1 ? currEdge->he1 : currEdge->he2;
			//only one halfedge was valid.
			if (normals) 
				glNormal3dv( he->hvert->ncoord );
			glVertex3dv( he->hvert->vcoord );
			// the other halfedge is next in the loop
			he = he->next;
			if(!he) 
			{
				printf("Error: Corrupted half-edge loop!\n");
				failure=true;
				break;
			}
			else
			{
				if(normals) glNormal3dv(he->hvert->ncoord);
				glVertex3dv(he->hvert->vcoord);
			}
		}
		else
		{
			if (normals) glNormal3dv( currEdge->he1->hvert->ncoord );
			glVertex3dv( currEdge->he1->hvert->vcoord );
			if (normals) glNormal3dv( currEdge->he2->hvert->ncoord );
			glVertex3dv( currEdge->he2->hvert->vcoord );
		}
		
		currEdge = currEdge->next;
		if (!currEdge || currEdge == sobj->sedges) break;
	}
	glEnd();
	glEndList();

	if (failure)
	{
		glDeleteLists( list, 1 );
		return 0;
	}

	return (edgeList = list);
}
Пример #5
0
void RenderScene(void) {
    // Clear buffer
    glClearColor(b_red, b_green, b_blue, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(scene_obj->viewport[0], scene_obj->viewport[1], scene_obj->viewport[2], scene_obj->viewport[3]);

    // Reset transformations
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(scene_obj->fovy, (GLfloat)WindowSize[0]/(GLfloat)WindowSize[1], 
            scene_obj->dnear, scene_obj->dfar);

    // viewing and modeling transformation
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // Set the camera
    gluLookAt(scene_obj->eye[0], scene_obj->eye[1], scene_obj->eye[2],
             scene_obj->vat[0], scene_obj->vat[1], scene_obj->vat[2],
             scene_obj->vup[0], scene_obj->vup[1], scene_obj->vup[2]);

    Light();

    int lastMaterial = -1;
    for (int k = 0, l = scene_obj->object.size(); k < l; ++k) {
        mesh* object = scene_obj->object[k].mesh_object;
        model model_tmp = scene_obj->object[k];
        glPushMatrix();
        glTranslatef(model_tmp.Tx, model_tmp.Ty, model_tmp.Tz);
        glRotatef(model_tmp.Angle, model_tmp.Rx, model_tmp.Ry, model_tmp.Rz);
        glScalef(model_tmp.Sx, model_tmp.Sy, model_tmp.Sz);

        // select texture
        std::string obj_file = object->obj_file;
        int texture_num = 0;
        if (obj_file == "ChessScene/Room.obj") {
            glActiveTexture(GL_TEXTURE0);
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, texObject[3]);
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
            texture_num = 1;
        }
        else if (obj_file == "ChessScene/Chessboard.obj") {
            glActiveTexture(GL_TEXTURE0);
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, texObject[1]);
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
            glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);

            glActiveTexture(GL_TEXTURE1);
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, texObject[2]);
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
            glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
            texture_num = 2;
        }
        else if (k > 17) { // cube
            glEnable(GL_TEXTURE_CUBE_MAP);
            glBindTexture(GL_TEXTURE_CUBE_MAP, texObject[0]);
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
            glEnable(GL_TEXTURE_GEN_S);
            glEnable(GL_TEXTURE_GEN_T);
            glEnable(GL_TEXTURE_GEN_R);
            texture_num = -1;
        }

        for(size_t i=0;i < object->fTotal;++i) {
            // set material property if this face used different material
            if(lastMaterial != object->faceList[i].m) {
                lastMaterial = (int)object->faceList[i].m;
                glMaterialfv(GL_FRONT, GL_AMBIENT  , object->mList[lastMaterial].Ka);
                glMaterialfv(GL_FRONT, GL_DIFFUSE  , object->mList[lastMaterial].Kd);
                glMaterialfv(GL_FRONT, GL_SPECULAR , object->mList[lastMaterial].Ks);
                glMaterialfv(GL_FRONT, GL_SHININESS, &object->mList[lastMaterial].Ns);
            }

            glBegin(GL_TRIANGLES);
            for (size_t j=0;j<3;++j) {
                //textex corrd. object->tList[object->faceList[i][j].t].ptr
                for (int mm = 0; mm < texture_num; ++mm) {
                    glMultiTexCoord3fv(GL_TEXTURE0 + mm, object->tList[object->faceList[i][j].t].ptr);
                }
                glNormal3fv(object->nList[object->faceList[i][j].n].ptr);
                glVertex3fv(object->vList[object->faceList[i][j].v].ptr);	
            }
            glEnd();
        }
        glActiveTexture(GL_TEXTURE1);
        glDisable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 0);

        glActiveTexture(GL_TEXTURE0);
        glDisable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 0);

        glDisable(GL_TEXTURE_CUBE_MAP);
        glDisable(GL_TEXTURE_GEN_R);
        glDisable(GL_TEXTURE_GEN_T);
        glDisable(GL_TEXTURE_GEN_S);
        glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
        glPopMatrix();
    }

    glutSwapBuffers();
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL13_nglMultiTexCoord3fv(JNIEnv *__env, jclass clazz, jint texture, jlong vAddress, jlong __functionAddress) {
	const GLfloat *v = (const GLfloat *)(intptr_t)vAddress;
	glMultiTexCoord3fvPROC glMultiTexCoord3fv = (glMultiTexCoord3fvPROC)(intptr_t)__functionAddress;
	UNUSED_PARAMS(__env, clazz)
	glMultiTexCoord3fv(texture, v);
}