Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


povilaslt2

Member Since 14 Jul 2011
Online Last Active Today, 07:19 AM

#5194746 Vertex buffer object issue

Posted by povilaslt2 on 26 November 2014 - 04:33 AM

Which version of OpenGL are you using? It looks like you're using compatibility mode if you're using OpenGL 3. If your context is using OpenGL 3.3+, then you'll have to use something known as a vertex array object (VAO).

 

I'd also suggest putting the following line:

glViewport(0, 0, 640, 480);

Put that in there right after you call glewInit(); Some desktop implementations of OpenGL don't need this, but I've found that some do. Here's some replacement code to try out. Put this code at the top of your main file:

#include <stdio.h>
#include <string>


// PUT ALL OF THIS ABOVE YOUR main() FUNCTION:

// VAO/VBO handles
GLuint vao = 0;
GLuint vbo = 0;
GLuint program = 0;

// basic vertex shader source
std::string vertSource =
"#version 330 core\n"
"layout(location = 0) in vec3 in_position;"
"void main()"
"{"
"	gl_Position = vec4(in_position.xyz, 1.0);" // probably could have done better here :-/
"}";

// basic fragment shader source
std::string fragSource =
"#version 330 core\n"
"out vec4 out_color;"
"void main()"
"{"
"	out_color = vec4(1.0, 1.0, 1.0, 1.0);" // color the fragment "white"
"}";

This code contains variables used for your VAO and VBO handles, and the source code that gets compiled to produce your shader.

 

Comment out this code:

// Setup triangle vertices
    fTriangle[0] = -0.4f; fTriangle[1] = 0.1f; fTriangle[2] = 0.0f;
    fTriangle[3] = 0.4f; fTriangle[4] = 0.1f; fTriangle[5] = 0.0f;
    fTriangle[6] = 0.0f; fTriangle[7] = 0.7f; fTriangle[8] = 0.0f;

    // Setup quad vertices

    fQuad[0] = -0.2f; fQuad[1] = -0.1f; fQuad[2] = 0.0f;
    fQuad[3] = -0.2f; fQuad[4] = -0.6f; fQuad[5] = 0.0f;
    fQuad[6] = 0.2f; fQuad[7] = -0.1f; fQuad[8] = 0.0f;
    fQuad[9] = 0.2f; fQuad[10] = -0.6f; fQuad[11] = 0.0f;

	glGenBuffers(2, uiVBO);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiVBO[0]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, 9*sizeof(float), fTriangle, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiVBO[1]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, 12*sizeof(float), fQuad, GL_STATIC_DRAW);

Then, put this where your code should be:

// 3D vertices that make up a triangle in screen-space
float triangle[12] = {
	 0.0f, 1.0f, 0.0f, // top vertex
	-1.0f, 0.0f, 0.0f, // lower-left point
	 1.0f, 0.0f, 0.0f, // lower-right point
};

// generate and bind vertex array object (VAO)
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

// generate, bind and setup vertex buffer object (VBO)
glGenBuffers(1, &vbo); // find a free vbo ID
glBindBuffer(GL_ARRAY_BUFFER, vbo); // binding for the first time actually allocates the buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 9, triangle, GL_STATIC_DRAW); // fill the buffer with data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);

// create the shaders, and compile them
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, &vertStr, NULL);
glCompileShader(vertShader);

// display the compiler log
glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0)
{
	char *log = new char[logLength];
	glGetShaderInfoLog(vertShader, logLength, &logLength, log);
	std::cout << "Vertex Shader Compile Log:\n" << log << std::endl;
	if(log) delete [] log;
}
	
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, &fragStr, NULL);
glCompileShader(fragShader);

// display the compiler log
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0)
{
	char *log = new char[logLength];
	glGetShaderInfoLog(fragShader, logLength, &logLength, log);
	std::cout << "Fragment Shader Compile Log:\n" << log << std::endl;
	if(log) delete [] log;
}

// create the shader program, and attach the shaders
program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);

// get the program's info log
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0)
{
	char *log = new char[logLength];
	glGetProgramInfoLog(program, logLength, &logLength, log);
	std::cout << "Log:\n" << log << std::endl;
	if(log) delete [] log;
}

// delete the shaders
glDeleteShader(vertShader);
glDeleteShader(fragShader);

// use the newly-created shader, and set the clear color
glUseProgram(program);
glClearColor(0.25f, 0.25f, 0.25f, 1.0f);

There's quite a bit going on, but this is the very basic you need to get something drawing onscreen in OpenGL 3.3+ (assuming that's your minimum target). I wasn't able to check this code to see if it compiles as I'm currently at work. Try this out, and get back to us! I'd also suggest looking at this tutorial. Ironically, this tutorial seems to do exactly what the code I prepared above does.

Thank you, it all works, but when I disable shaders it don't. So it means that I always have to use shaders?




PARTNERS