Jump to content
  • Advertisement
Sign in to follow this  
amp47

OpenGL how can i draw tow shapes?

This topic is 1509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello every one

i finally did it draw a triangle on the screen ,i want now to draw multi-shapes but i don't know where i must add a second  vertex buffer ,

 

this is the code source :

#include <GL/glew.h>
// include GLEW and new version of GL on Windows
#define GLFW_DLL
#include <GL/glfw3.h> // GLFW helper library
#include <stdio.h>

int main () {
  // start GL context and O/S window using the GLFW helper library
  if (!glfwInit ()) {
    fprintf (stderr, "ERROR: could not start GLFW3\n");
    return 1;
  }

	// uncomment these lines if on Apple OS X
  /*glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/

  GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
  if (!window) {
    fprintf (stderr, "ERROR: could not open window with GLFW3\n");
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent (window);

  // start GLEW extension handler
  glewExperimental = GL_TRUE;
  glewInit ();

  // get version info
  const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
  const GLubyte* version = glGetString (GL_VERSION); // version as a string
  printf ("Renderer: %s\n", renderer);
  printf ("OpenGL version supported %s\n", version);

  // tell GL to only draw onto a pixel if the shape is closer to the viewer
  glEnable (GL_DEPTH_TEST); // enable depth-testing
  glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

  /* OTHER STUFF GOES HERE NEXT */
float points[] = {
    -0.5f,  0.5f,  0.0f,
    -0.25f, -0.0f,  0.0f,
    -0.75f, -0.0f,  0.0f,

};

float points01[] = { //the rictangele
    0.25f,  0.25f,  0.0f,
    0.75f, 0.25f,   0.0f,
    0.75f, -0.25f,  0.0f,
    0.25f, -0.25f,  0.0f

};
//create a vertex buffer and bind it for triangle
GLuint vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
//create a vertex buffer and bind it for rectangle
GLuint vbo01=0;
glGenBuffers (1, &vbo01);
glBindBuffer (GL_ARRAY_BUFFER, vbo01);
glBufferData (GL_ARRAY_BUFFER, 12 * sizeof (float), points01, GL_STATIC_DRAW);


// Here we create a vertex attribute Object
GLuint vao = 0;
glGenVertexArrays (1, &vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//second vattribute
GLuint vao01 = 0;
glGenVertexArrays (1, &vao01);
glEnableVertexAttribArray (1);
glBindBuffer (GL_ARRAY_BUFFER, vbo01);
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

//shader parts
//vertex shader
const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main () {"
"  gl_Position = vec4 (vp, 1.0);"
"}";
//fragment shader
const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"
"void main () {"
"  frag_colour = vec4 (0.8, 0.5, 0.5, 1.0);"
"}";
//load shader

GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);

//load fragment shader
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
//create the program
GLuint shader_programme = glCreateProgram ();

//attach the vertex shader and fragment shader to the program
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);

//link the program
glLinkProgram (shader_programme);

//drawing
while (!glfwWindowShouldClose (window)) {
  // wipe the drawing surface clear
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glUseProgram (shader_programme);
  glBindVertexArray (vao);

  // draw points 0-3 from the currently bound VAO with current in-use shader
  glBindVertexArray (vao01);
  glDrawArrays (GL_TRIANGLES, 1,3);

  // draw points 0-3 from the currently bound VAO with current in-use shader
  glDrawArrays (GL_QUADS,0,4);
  // update other events like input handling
  glfwPollEvents ();
  // put the stuff we've been drawing onto the display
  glfwSwapBuffers (window);
}

// close GL context and any other GLFW resources
  glfwTerminate();
  return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Looking at what you have dome you have copy & pasted from a site without understanding what you have done try following www.arcsynthesis.org/gltut . Try changing your GL_QUADS to GL_TRIANGLE_STRIP but you have to bind each vbo to each vao if thats the way you want to do it. Work through the tutorials on the site i just gave you and understand each step you take

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!