Texturing a VBO

Started by
10 comments, last by JoshuaWaring 10 years, 7 months ago

! New question at bottom !

I've been wanting to draw a quad and have it textured, but this seems to be a larger struggle than I first thought it would of been.

I create my quad and they appear nicely on the screen, in a plain white colour... which is the problem, I can't change that. (Well I can, but I don't know how), Things might me a bit messy because I've been trying alsorts of things on it. Even read the Specifications, but that doesn't really help me on how it operates as a whole.

The code is inside a function and is passed a Vector and a Texture

At the bottom I do infact recieve a error of GL_INVALID_ENUM


const GLdouble vertex_buffer[] = {
        (position.x / windowSizeHalf.x) - 1, (position.y / windowSizeHalf.y) - 1, position.z,
        ((position.x + tex.width) / windowSizeHalf.x) - 1, (position.y / windowSizeHalf.y) - 1, position.z,
        (position.x / windowSizeHalf.x) - 1 , ((position.y + tex.height) / windowSizeHalf.y) - 1, position.z,
        ((position.x + tex.width) / windowSizeHalf.x) - 1, ((position.y + tex.height) / windowSizeHalf.y) - 1, position.z    
    };

    const GLint uv_buffer[] = {
        0, 0,
        1, 0,
        1, 1,
        0, 1
    };

    unsigned int VertexBufferObject[1];
    unsigned int UVBufferObject[1];

    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glClientActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex.textureID);


    glGenBuffers(1, UVBufferObject);
    glGenBuffers(1, VertexBufferObject);

    glBindBuffer(GL_ARRAY_BUFFER, *UVBufferObject);
    glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(unsigned int), uv_buffer, GL_STATIC_DRAW);
    glTexCoordPointer(1, GL_INT, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, *VertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer), vertex_buffer, GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);

    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(0);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glBindVertexArray(0);

    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);

    GLenum error = glGetError();
    if(states.RENDERING_DEBUGGING && error != GL_NO_ERROR)
        logError((char*)glewGetErrorString(error));

    return;

Thank you for any help

Advertisement

If you have an error, then you should also figure out which exact line is giving you that error.

I did that at it came to glEnable(GL_TEXTURE_2D); giving the code 1280

I don't believe that to be correct because that line is fine. I'd say it is coming from before that. Verify that the error is in fact 0 before that line.

glEnable(GL_TEXTURE_2D); // 1280
glEnableClientState(GL_TEXTURE_COORD_ARRAY); // 1282
glClientActiveTexture(GL_TEXTURE0); // 1282
glBindTexture(GL_TEXTURE_2D, tex.textureID); // 0

in the comments are the errors returns. something doesn't seem right

I removed the lines that get the error, but after each command I check an error so the next command is a new error.

And what about before the first line? Is it actually zero before entering that function in the first place?

Or, are you perhaps using a post-OpenGL 3.0 core profile rendering context? If so, then those errors actually make sense, since those functions are not available any more.

What do you use for post 3 then?

because right now I'm using OpenGL 4.1

Shaders and generic attribute arrays is the only way to go. Check out the links in the Getting Started box on the forum page if you need some documentation covering the new API.

So I've gone agead with the shaders, but it fails to compile and I'm not going to lie, the shader code has been checked to match identically to the tutorial, since there was an error I was thourah. The fragment shaders has no error and at no point returns any. If it means anything, the quad still draws to the screen without a texture.

ERROR: Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "v" parse error
ERROR: error(#273) 1 compilation errors. No code generated


const GLchar* vertexShader = ""
		"void main(void){"
		"	gl_TexCoord[0] = gl_MultiTexCoord0;"
		"	gl_Position = ftransform();"
		"}";

	const GLchar* fragShader = " "
		"uniform sampler2D colorMap;"

		"void main (void)"
		"{"
		"	gl_FragColor = texture2D(colorMap, gl_TexCoord[0].st);"
		"}";


	static GLuint vertexID, fragID, shaderProgram;
	vertexID = glCreateShader(GL_VERTEX_SHADER);
	fragID = glCreateShader(GL_FRAGMENT_SHADER);

	GLint vertexSize = sizeof(*vertexShader);
	GLint fragSize = sizeof(*fragShader);

	glShaderSource(vertexID, 1, &vertexShader, &vertexSize); // Shaders length is defines by a NULL terminator
	glShaderSource(fragID, 1, &fragShader, &fragSize);

	glCompileShader(vertexID);
	glCompileShader(fragID);

	static char* errorLog = new char[256];

	GLint status = glGetError();
	if(states.RENDERING_DEBUGGING && status != GL_NO_ERROR)
		logError((char*)glewGetErrorString(status));
	glGetObjectParameterivARB(vertexID, GL_COMPILE_STATUS, &status);

	if(!status){ 
		glGetShaderInfoLog(vertexID, 256, NULL, errorLog);
		logError(errorLog);
	}

I've solved the problem, my error was coming from the sizeof commands, as they would return the size of a char and not the string.

I used a null terminator to define the length of the shader, by adding \0 to the end of the shaders and replace the size with 0.

Now I have a black quad, somethings happening :)

This topic is closed to new replies.

Advertisement