Jump to content

  • Log In with Google      Sign In   
  • Create Account

Texturing a VBO


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 22 August 2013 - 03:45 AM

! 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


Edited by Joshhua5, 25 August 2013 - 10:14 AM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 22 August 2013 - 04:02 AM

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



#3 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 22 August 2013 - 04:36 AM

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



#4 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 22 August 2013 - 04:40 AM

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.



#5 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 22 August 2013 - 04:42 AM

    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.


Edited by Joshhua5, 22 August 2013 - 04:43 AM.


#6 Brother Bob   Moderators   -  Reputation: 8194

Like
2Likes
Like

Posted 22 August 2013 - 04:51 AM

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.



#7 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 22 August 2013 - 05:45 AM

What do you use for post 3 then?

because right now I'm using OpenGL 4.1


Edited by Joshhua5, 22 August 2013 - 05:49 AM.


#8 Brother Bob   Moderators   -  Reputation: 8194

Like
2Likes
Like

Posted 22 August 2013 - 06:08 AM

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.



#9 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 22 August 2013 - 08:23 AM

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);
	}


#10 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 22 August 2013 - 08:45 AM

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 :)



#11 Brother Bob   Moderators   -  Reputation: 8194

Like
2Likes
Like

Posted 22 August 2013 - 08:45 AM

Since vertexShader is a pointer to a char, *vertexShader is of type char, and thus sizeof(*vertexShader) is equivalent to sizeof(char). In order to use sizeof to get the length of the string, you have to make vertexShader an array, and take the size of the array. I also recommend you insert new-line characters at each new line so that you can get some sensible line numbers from the error messages.

const char vertexShader[] = ""
    "void main(void){\n"
    "    gl_TexCoord[0] = gl_MultiTexCoord0;\n"
    "    gl_Position = ftransform();\n"
    "}\n";
 
int vertexSize = sizeof(vertexShader);


#12 Joshhua5   Members   -  Reputation: 439

Like
0Likes
Like

Posted 25 August 2013 - 10:14 AM

I'm still having issues drawing a simple texture to the screen :(, now nothing will apear and the drawArrays now returns a 1282 which has stumped me. Instead of creating a new thread I thought I would revive this one. I wanted to make sure that all my functions are correct.

Here is the link to the whole function to keep the post size down. http://www.pastebucket.com/19567

Thank you for any help at all.

        unsigned int VertexBufferObject[1]; 
	GLuint error = glGetError();
	glEnable(GL_TEXTURE_2D);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, tex.textureID);

	// Pass texture to shader
	glUniform1i(glGetUniformLocation(shaderProgram, "colorMap"), GL_TEXTURE0);

	glGenBuffers(1, VertexBufferObject);
	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); 

	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);// Returns error 1282

	glDisableVertexAttribArray(0);

	glBindTexture(GL_TEXTURE_2D, 0); 
	glDeleteBuffers(1, VertexBufferObject);
	glDisable(GL_TEXTURE_2D); 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS