Beispiel #1
0
void 
initVertexBufferObject(void)
{
    g_size_vertex   = g_faces.size()*3*3;
    g_size_normal   = g_faces.size()*3*3;

    g_sceneVBOVertices  = new GLfloat[g_size_vertex];
    g_sceneVBONormals   = new GLfloat[g_size_normal];

    std::cout << "faces: "          <<  g_faces.size()  << std::endl;
    std::cout << "vbo_vertices: "   <<  g_size_vertex     << std::endl;
    std::cout << "vbo_normals: "    <<  g_size_normal     << std::endl;

	//
    // Collapse indexed mesh
    //
    for (unsigned j = 0; j < g_faces.size(); ++j)
    {
        for (unsigned i = 0; i < 3; ++i)
        {
            // per vertex of face do...
            g_sceneVBOVertices[j*9+i*3+0]  = g_vertices[g_faces[j].v[i]][0];
            g_sceneVBOVertices[j*9+i*3+1]  = g_vertices[g_faces[j].v[i]][1];
            g_sceneVBOVertices[j*9+i*3+2]  = g_vertices[g_faces[j].v[i]][2];

            g_sceneVBONormals[j*9+i*3+0]   = g_normals[g_faces[j].n[i]][0];
            g_sceneVBONormals[j*9+i*3+1]   = g_normals[g_faces[j].n[i]][1];
            g_sceneVBONormals[j*9+i*3+2]   = g_normals[g_faces[j].n[i]][2];

        }//endfor

    }//endfor

    glGenBuffers(1, &g_sceneVBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_sceneVBO);
    GET_GLERROR(0);

    // reserve buffer but not initialize it...
    glBufferData(GL_ARRAY_BUFFER, (g_size_vertex+g_size_normal) * sizeof(GLfloat), 0, GL_STATIC_DRAW);
    GET_GLERROR(0);

    // copy vertices starting from 0 offset
    glBufferSubData(GL_ARRAY_BUFFER, 0, g_size_vertex* sizeof(GLfloat), g_sceneVBOVertices);                             
    GET_GLERROR(0);

    // copy normals after vertices
    glBufferSubData(GL_ARRAY_BUFFER, g_size_vertex* sizeof(GLfloat) , g_size_normal* sizeof(GLfloat), g_sceneVBONormals);
    GET_GLERROR(0);

    return;
}
Beispiel #2
0
void intiFB(int w, int h)
{
	////////framebuffer init
	if(glIsTexture(tex))
	{
		glDeleteRenderbuffers(1, &depth_rb);
		glDeleteTextures(1,&tex);
		glDeleteFramebuffers(1, &fb);
	}

	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0, 0.0, 0.0, 1.0);

	glGenFramebuffers(1, &fb);
	glGenTextures(1, &tex);
	glGenRenderbuffers(1, &depth_rb);

	glBindFramebuffer(GL_FRAMEBUFFER, fb);    

	// init texture
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, 
		GL_RGBA, GL_FLOAT, NULL);
	GET_GLERROR(NULL);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 
		GL_TEXTURE_2D, tex, 0);

	GET_GLERROR(0);

	// initialize depth renderbuffer
	glBindRenderbufferEXT(GL_RENDERBUFFER, depth_rb);
	glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, texWidth, texHeight);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 
		GL_RENDERBUFFER, depth_rb);

	glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0);

	GET_GLERROR(0);

	CheckFramebufferStatus();

	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
}
Beispiel #3
0
GLuint
loadTextureRAW2D(const char * filename, bool wrap, unsigned int width, unsigned int height, unsigned int depth)
{
    GLuint texture;
    char * data;
    FILE * file;

    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;

    data = (char *)malloc( width * height * depth );

    fread( data, width * height * depth, 1, file );
    fclose( file );

    glGenTextures( 1, &texture );
	GET_GLERROR(0);
    glBindTexture( GL_TEXTURE_2D, texture );
	GET_GLERROR(0);
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
	GET_GLERROR(0);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
	GET_GLERROR(0);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	GET_GLERROR(0);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast<GLfloat>(wrap ? GL_REPEAT : GL_CLAMP) );
	GET_GLERROR(0);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, static_cast<GLfloat>(wrap ? GL_REPEAT : GL_CLAMP) );
	GET_GLERROR(0);
    gluBuild2DMipmaps( GL_TEXTURE_2D, depth, width, height, (depth == 1) ? GL_LUMINANCE : GL_RGB, GL_UNSIGNED_BYTE, data );
	GET_GLERROR(0);

    free( data );

    return texture;
}
Beispiel #4
0
GLuint loadRGBATexture(char * data, bool wrap, unsigned int width, unsigned int height)
{
    GLuint texture;
    glGenTextures(1, &texture);
	GET_GLERROR(0);
    glBindTexture(GL_TEXTURE_2D, texture);
	GET_GLERROR(0);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	GET_GLERROR(0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	GET_GLERROR(0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	GET_GLERROR(0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP);
	GET_GLERROR(0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP);
	GET_GLERROR(0);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
	GET_GLERROR(0);

    free( data );

    return texture;
}
Beispiel #5
0
void
on_display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //
    // Apply model transformation
    //
    glPushMatrix();
    glRotatef(static_cast<GLfloat>(angle), 0.0f, 1.0f, 0.0f);
	glRotatef(-45, 1.0f, 0.0f, 0.0f);

	//
	// Bind Textures
	//
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, g_texTerrain);
    GET_GLERROR(0);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_1D, g_texGradient);
    GET_GLERROR(0);

	//
	// Use Program
	//
    glUseProgramObjectARB(g_terrainProgram);
    GET_GLERROR(0);

    if(s_draw_vbo)
    {
        // bind VBOs with IDs and set the buffer offsets of the bound VBOs
        // When buffer object is bound with its ID, all pointers in gl*Pointer()
        // are treated as offset instead of real pointer.
        glBindBuffer(GL_ARRAY_BUFFER, g_sceneVBO);

        // enable vertex arrays
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);

        // before draw, specify arrays with their offsets
        glNormalPointer(GL_FLOAT, 0, (void*)(g_size_vertex* sizeof(GLfloat)));
        glTexCoordPointer(2, GL_FLOAT, 0, (void*)((g_size_vertex+g_size_normal)* sizeof(GLfloat)));
        glVertexPointer(3, GL_FLOAT, 0, 0);

        glDrawArrays(GL_TRIANGLES, 0, g_faces.size()*3);

        glDisableClientState(GL_VERTEX_ARRAY);  // disable vertex arrays
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);

        // it is good idea to release VBOs with ID 0 after use.
        // Once bound with 0, all pointers in gl*Pointer() behave as real
        // pointer, so, normal vertex array operations are re-activated
        glBindBuffer(GL_ARRAY_BUFFER, 0);

    } else {

        if(s_draw_array)
        {
            //
            // Enable vertex arrays
            //
            glEnableClientState(GL_NORMAL_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glEnableClientState(GL_VERTEX_ARRAY);

            //
            // Specify vertex arrays
            //
            glNormalPointer(GL_FLOAT, 0, g_sceneVBONormals);
            glVertexPointer(3, GL_FLOAT, 0, g_sceneVBOVertices);
    
            //
            // Draw 
            //
            glDrawArrays(GL_TRIANGLES, 0, g_faces.size()*3);

            //
            // Enable vertex arrays
            //
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            glDisableClientState(GL_NORMAL_ARRAY);

        } else {

            //
            // Draw using intermediate mode
            //
            glBegin(GL_TRIANGLES);
            {
                for (unsigned j = 0; j < g_faces.size(); ++j)
                {
                    for (unsigned i = 0; i < 3; ++i)
                    {
                        glNormal3fv(&g_normals[g_faces[j].n[i]][0]);
                        glVertex3fv(&g_vertices[g_faces[j].v[i]][0]);

                    }//endfor

                }//endfor
            }
            glEnd();

        }//endif

    }//endif

    glUseProgramObjectARB(0);
    glPopMatrix();

    glutSwapBuffers();

    return;
}
Beispiel #6
0
void
initShader(void)
{
    ///////////////////////////////////////////////////////////////////////////
    //
    // Create earth shader
    //
    GLhandleARB terrain_vertex_shader;
    GLhandleARB terrain_fragment_shader;

    //
    // Create and load resources
    //
    g_terrainProgram          = glCreateProgramObjectARB();
    terrain_vertex_shader     = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    terrain_fragment_shader   = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

    loadShaderFromFile(terrain_vertex_shader, "shader/terrain.vert");
    loadShaderFromFile(terrain_fragment_shader, "shader/terrain.frag");

    //
    // Compile
    //
    glCompileShaderARB(terrain_vertex_shader);
    GET_GLERROR(0);
    printInfoLog(terrain_vertex_shader);

    glCompileShaderARB(terrain_fragment_shader);
    GET_GLERROR(0);
    printInfoLog(terrain_fragment_shader);

    //
    // Link
    //
    glAttachObjectARB(g_terrainProgram, terrain_vertex_shader);
    glAttachObjectARB(g_terrainProgram, terrain_fragment_shader);
    glLinkProgramARB(g_terrainProgram);
    GET_GLERROR(0);
    printInfoLog(g_terrainProgram);

    ///////////////////////////////////////////////////////////////////////////
    //
    // Setup terrain shader
    //

    //
    // Sampler
    //
    glUseProgramObjectARB(g_terrainProgram);
	GET_GLERROR(0);

	float test;
    //
    // Aufgabe 1.b - Begin
    //
	
	int samplerTerrain = glGetUniformLocationARB(g_terrainProgram, "samplerTerrain");
	GET_GLERROR(0);
	int samplerGradient = glGetUniformLocationARB(g_terrainProgram, "samplerGradient");
	GET_GLERROR(0);

	glUniform1i(samplerTerrain, 0);
	GET_GLERROR(0);
	glUniform1i(samplerGradient, 1);
	GET_GLERROR(0);
    //
    // Aufgabe 1.b - End
    //

	//
	// Bounding Box
	//
	int LLFLocation = glGetUniformLocationARB(g_terrainProgram, "v3LLF");
	GET_GLERROR(0);
	int URBLocation = glGetUniformLocationARB(g_terrainProgram, "v3URB");
	GET_GLERROR(0);

	vec3 LLF = vec3(g_sceneAABB.xMin, g_sceneAABB.yMin, g_sceneAABB.zMin);
	vec3 URB = vec3(g_sceneAABB.xMax, g_sceneAABB.yMax, g_sceneAABB.zMax);

    glUniform3fARB(LLFLocation, g_sceneAABB.xMin, g_sceneAABB.yMin, g_sceneAABB.zMin);
	GET_GLERROR(0);
	glUniform3fARB(URBLocation, g_sceneAABB.xMax, g_sceneAABB.yMax, g_sceneAABB.zMax);
	GET_GLERROR(0);
    return;
}