Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


my first gl 3 app: displaying a cube


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
5 replies to this topic

#1 rogerdv   Members   -  Reputation: 164

Like
0Likes
Like

Posted 20 May 2014 - 09:45 AM

Im trying to learn opengl 3 and I started by the traidtional triangle program. But I tried to improve it to display a cube, it doesnt shows anything. Here is the relevant code:

//the shaders
const char* vertex_shader =
"#version 150\n"
"in vec3 vp;"
"void main () {"
"  gl_Position = vec4 (vp, 1.0);"
"}";

const char* fragment_shader =
"#version 150\n"
"out vec4 frag_colour;"
"void main () {"
"  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";

//the cube coordinates
GLfloat box[] = {  
	0.0f, 0.0f, 0.0f,
	1.0f, 0.0f, 0.0f,
	1.0f, 1.0f, 0.0f,
	0.0f, 1.0f, 0.0f,
	0.0f, 0.0f, -1.0f,
	1.0f, 0.0f, -1.0f,
	1.0f, 1.0f, -1.0f,
	0.0f, 1.0f, -1.0f
};

// Create and bind the VBO for the vertices.
    glGenBuffers(1, &g_verticesVBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);

    // Transfer the vertices from CPU to GPU.
    glBufferData(GL_ARRAY_BUFFER,  9 * sizeof(GLfloat), triangle, GL_STATIC_DRAW);
    //glBindBuffer(GL_ARRAY_BUFFER, 0);

  glGenVertexArrays (1, &g_vao);
  glBindVertexArray (g_vao);
  glEnableVertexAttribArray (0);
  glBindBuffer (GL_ARRAY_BUFFER, g_verticesVBO);
  glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

 //here we draw
 // wipe the drawing surface clear
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_programme);
glBindVertexArray (g_verticesVBO);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays (GL_QUADS, 0, 3); //also tried glDrawArrays (GL_QUADS, 0, 8); 
SDL_GL_SwapWindow(mainwindow);

Can somebody see what Im doing wrong here?



Sponsor:

#2 h3xl3r   Members   -  Reputation: 165

Like
3Likes
Like

Posted 20 May 2014 - 10:25 AM

Only time for a quick look, if there's time later I'll try to post some code as well, but here's some remarks:

 

- You're using the deprecated (in OpenGL 3) GL_QUADS primitive type. You should be using two triangles really to draw a "quad". Your driver might still support this primitive type of course as it's only depreacted and might not be removed completley (yet), but you'll have to switch sooner or later and it's the way to go in general.

 

- Your vertex data does not have enough vertices to draw a cube without using an index buffer. Eight points are enough for drawing an indexed cube as the indexes can reference each vertex multiple times. If you're using glDrawArrays ( = non-indexed) you'll have to specify each vertex(-position) multiple times to get the same result. Take a look at glDrawElements for indexed drawing.

 

My suggestion would be to not go for the cube immediately but try to draw one quad composed of two triangles first, preferably using an index buffer, as that's what you'll need extending the code for the full cube later.

 

Edit:

 

Take a look at:

http://www.open.gl/drawing

 

The example there is also starting with a triangle and extending it to a quad with glDrawArrays first, and then adding indexing with glDrawElements.


Edited by h3xl3r, 20 May 2014 - 10:40 AM.


#3 radioteeth   Prime Members   -  Reputation: 1102

Like
2Likes
Like

Posted 20 May 2014 - 11:45 AM

I see you have your 'box[]' array, but your glBufferData call references 'triangle' instead. And why is it trying to put 9 floats in there ?



#4 rogerdv   Members   -  Reputation: 164

Like
0Likes
Like

Posted 20 May 2014 - 01:16 PM

Yep, wrong number, is 24. 9 is for the triangle, and should be box. I just copied the wrong code.



#5 Irlan   Members   -  Reputation: 1553

Like
0Likes
Like

Posted 20 May 2014 - 02:00 PM

Don't use mixed bindings for creation. Just do:

 

glBind(foo)

glUnbind(foo)

 

glBind(foo1)

glBind(foo) // if uses foo and ind more than once

glUnbind(foo)

glUnbind(foo1)

 

It increases the readability this way.



#6 Gooey   Members   -  Reputation: 253

Like
0Likes
Like

Posted 20 May 2014 - 02:07 PM

After quickly flicking through as radioteeth said you glBufferData is pointing at the wrong array also i noticed with your draw call


glDrawArrays (GL_QUADS, 0, 3)

you are only trying to draw using 3 vertices the call is

glDrawArrays(mode, first, last);

where mode can be GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN. first and last point at the places in your enabled array.

to draw a cube with 8 vertices you would need to use an ebo but you will learn about them with whatever tutorial your using






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