Cube texturing in opengl3

Just doing my computer graphics assignment - put texture (600x400 bitmap with different numbers) on a cube to form a proper dice. I managed to do it using "classical" texture mapping: creating verices and adding corresponding texture coordinates to it:

int arrayindex = 0;
float xpos = 0.0f;
float xposEnd = 0.32f;
float ypos = 0.0f;
float yposEnd = 0.49f;
int count = 0;
void quad( int a, int b, int c, int d ) {
    colors[arrayindex] = vertex_colors[a];
    points[arrayindex] = vertices[a];
    tex_coord[arrayindex] = new Point2(xpos, ypos);
    arrayindex++;
    colors[arrayindex] = vertex_colors[b];
    points[arrayindex] = vertices[b];
    tex_coord[arrayindex] = new Point2(xpos, yposEnd);
    arrayindex++;
    colors[arrayindex] = vertex_colors[c];
    points[arrayindex] = vertices[c];
    tex_coord[arrayindex] = new Point2(xposEnd, yposEnd);
    arrayindex++;
    colors[arrayindex] = vertex_colors[a];
    points[arrayindex] = vertices[a];
    tex_coord[arrayindex] = new Point2(xpos, ypos);
    arrayindex++;
    colors[arrayindex] = vertex_colors[c];
    points[arrayindex] = vertices[c];
    tex_coord[arrayindex] = new Point2(xposEnd, yposEnd);
    arrayindex++;
    colors[arrayindex] = vertex_colors[d];
    points[arrayindex] = vertices[d]; 
    tex_coord[arrayindex] = new Point2(xposEnd, ypos);
    arrayindex++;
    xpos = xpos + 0.34f;
    xposEnd = xpos + 0.32f;
    count++;
    if (count == 3) {
        xpos = 0.0f;
        xposEnd = 0.33f;
        ypos = 0.51f;
        yposEnd = 1.0f;
    }
}

void colorcube() {
    quad( 1, 0, 3, 2 );
    quad( 2, 3, 7, 6 );
    quad( 3, 0, 4, 7 );
    quad( 6, 5, 1, 2 );
    quad( 5, 4, 0, 1 );
    quad( 4, 5, 6, 7 );

    pointsBuf = VectorMath.toBuffer(points);
    colorsBuf = VectorMath.toBuffer(colors);
    texcoord = VectorMath.toBuffer(tex_coord);
}

Passing all this stuff to shaders and just putting it up together.

But reviewing the slides i noticed this method is supposed to be "pre opengl3". Is there any other method to do this stuff?

In lecture examples i noticed putting it up together in the vertex shader but it was just for a simple 2d plane, not a 3d cube

tex_coords = vec2(vPosition.x+0.5,vPosition.z+0.5);

and later passed to fragment shader to create the texture.


But reviewing the slides i noticed this method is supposed to be "pre opengl3".

I think your slides refer to the old immediate mode. In immediate mode each vertex and its attributes are sent to OpenGL by calling functions that immediately draw them.

In your code however you're initializing a buffer with vertex data. This buffer may then passed as a whole to OpenGL and drawn as a batch by only a single OpenGL call. I wrote "may" because there's not a single OpenGL call in your question.

链接地址: http://www.djcxy.com/p/38494.html

上一篇: SDL2 / Opengl3无法显示任何内容

下一篇: 在opengl3中的立方体纹理