• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mariusz Pilipczuk

GLSL 1.5 and Sending Vertices

12 posts in this topic

Hello.

 

In my old game, I used GLSL 1.2 in my shaders, and I could still call glColor*(), glTexCoord*(), etc. to pass info about vertices, and my vertex shader was still able to read it.

 

Now I'm trying to use GLSL 1.5, and I know that I cannot use glColor*() etc to pass such information any more, but it doesn't matter since I can use vertex attributes. However, gl_Vertex was also removed. This means that calling glVertex*() does not make sense anymore, so how do I send the vertices to the GPU?

 

Do I have to use VBOs? If so, is there any good tutorial with a nice example on how to do this?

 

Thank you.

0

Share this post


Link to post
Share on other sites

I'm not up to date with what is in which version of GLSL, but from a modern perspective, everything has moved into generic vertex attributes and replaced specific vertex attributes. Specific functions like glVertex, glColor, glTexCoord, and more specifically their vertex array equivalents glVertexPointer, glColorPointer, and so on, have all been replaced by generic functions like glVertexAttribPointer.

 

The corresponding attributes have been removed from GLSL as well, and been replaced by your own attributes. You no longer use glVertex to get the position into the gl_Vertex variable, but instead you create your own names and bind the generic attributes to the inputs to your vertex shader.

 

I suggest you go through some of the links on the forum front page to study the new approach in the modern API. If you don't want to entirely migrate to the enw API, then at least you will learn about the generic attributes. The main idea is that you no longer use specific attributes, but generic attributes. For example, in the modern API you are required to use vertex arrays with VBO, but if you're not migrating entirely to the new API then you can still use vertex arrays without VBO.

1

Share this post


Link to post
Share on other sites

Thank you, but I cannot seem to find any tutorial which shows how to use VBOs (yes, I'm trying to completely migrate to the new API). I did see somewhere on this forum that a person made a struct for holding each vertex and I believe this struct was treated as the only attribute. If I want to use this approach, can someone please post some example code for how I would create a VBO with this data and then draw? Thank you.

0

Share this post


Link to post
Share on other sites

A quick search for "vbo tutorial" gives me plenty of results. The link collection I mentioned also contains information on how to use VBO.

1

Share this post


Link to post
Share on other sites

You can still use glBegin/glVertexAttrib/glEnd if you wish, with "glVertexAttrib (0, ..." being the "moral equivalent" of glVertex.  glBegin/glEnd are deprecated in GL3.x, but so long as you create a compatibility context (which you'll get by default) you'll be OK.

 

Of course in the long run you should switch over to VBOs, but for quick prototyping and easing the transition there's nothing wrong with this approach.

1

Share this post


Link to post
Share on other sites

Thank you for helping guys. I attempted to use VBOs, but something doesn't work. I was attempting to (for now) draw a red triangle in orthographic mode on the screen. Here's the buffer creating code:

    int xValues[3] = {100, 40, 140};
    int yValues[3] = {50, 70, 70};

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint vbo[2];
    glGenBuffers(2, vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(int), xValues, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)0, 1, GL_INT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(int), yValues, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)1, 1, GL_INT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glBindVertexArray(0);

Then I draw as follows:

        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 1);
        glBindVertexArray(0);

My vertex shader:

// gui.vert
// The GUI vertex shader.
// This software, including the source code, is release under the terms of
// the License.

#version 150

// vertex input
in int in_x;
in int in_y;

void main()
{
	float x = (float(in_x) / 320) - 1;
	float y = (float(in_y) / 240) - 1;
	gl_Position = vec4(x, y, 0.0, 1.0);
};

My fragment shader:

// gui.frag
// The GUI fragment shader.
// This software, including the source code, is release under the terms of
// the License.

#version 150

void main()
{
	gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
};

But the only thing I get is a black screen. Does anyone know what I'm doing wrong here?

0

Share this post


Link to post
Share on other sites

The last parameter to glDrawArrays is the number of vertices to pull from the arrays; to draw a triangle you need three vertices.

 

Another comment though, but not an error, is that you don't have to keep your coordinates in separate component-arrays like that. Make one array of coordinates, instead of two arrays with X and Y coordinates individually, and bind it to a vec2 (or ivec3 if you want integer vectors) type in the shader instead.

1

Share this post


Link to post
Share on other sites

The last parameter to glDrawArrays is the number of vertices to pull from the arrays; to draw a triangle you need three vertices.

 

Another comment though, but not an error, is that you don't have to keep your coordinates in separate component-arrays like that. Make one array of coordinates, instead of two arrays with X and Y coordinates individually, and bind it to a vec2 (or ivec3 if you want integer vectors) type in the shader instead.

I know about the coordinates. I just wanted to see if I'm sending more than one attribute properly.

 

I changed the last argument of glDrawArrays() to 3, but it still draws nothing :(

0

Share this post


Link to post
Share on other sites

You should do error checking then to see if something is wrong. And also query the shader for the actual locations so you don't hard code the wrong attribute locations. One thing that just occurred to me now though is that you may have to set the pointer to integer type attributes with glVertexAttribIPointer instead.

1

Share this post


Link to post
Share on other sites

Pretty sure gl_FragColor is deprecated in glsl version 150.

Edited by Saucy
0

Share this post


Link to post
Share on other sites

You should do error checking then to see if something is wrong. And also query the shader for the actual locations so you don't hard code the wrong attribute locations. One thing that just occurred to me now though is that you may have to set the pointer to integer type attributes with glVertexAttribIPointer instead.

Thanks. That solved the problem.

 

Pretty sure gl_FragColor is deprecated in glsl version 150.

I'm sorry, I tried upvoting your post and accidentally clicked the wrong thing and now i don't know how to reverse it -.-

 

If gl_FragColor is deprecated, then how do I tell the GPU what color the pixel should be??

0

Share this post


Link to post
Share on other sites

 

Pretty sure gl_FragColor is deprecated in glsl version 150.

I'm sorry, I tried upvoting your post and accidentally clicked the wrong thing and now i don't know how to reverse it -.-

Unfortunately, votes are final immediately. I would definitely like to see some grace period though so silly mistakes like that can be corrected. I'll bump him up a step so the post doesn't show a -1 at least.

 

If gl_FragColor is deprecated, then how do I tell the GPU what color the pixel should be??

You use custom out variables in the fragment shader, just like the inputs to the vertex shader are custom in variables. The variable on location 0 goes to the primary render target, the variable on location 1 goes to the second render target, and so on. If you're not working with multiple render targets, then just define an out variable in the fragment shader and write to it.

1

Share this post


Link to post
Share on other sites

 

 

Pretty sure gl_FragColor is deprecated in glsl version 150.

I'm sorry, I tried upvoting your post and accidentally clicked the wrong thing and now i don't know how to reverse it -.-

Unfortunately, votes are final immediately. I would definitely like to see some grace period though so silly mistakes like that can be corrected. I'll bump him up a step so the post doesn't show a -1 at least.

 

If gl_FragColor is deprecated, then how do I tell the GPU what color the pixel should be??

You use custom out variables in the fragment shader, just like the inputs to the vertex shader are custom in variables. The variable on location 0 goes to the primary render target, the variable on location 1 goes to the second render target, and so on. If you're not working with multiple render targets, then just define an out variable in the fragment shader and write to it.

 

Aaah, thanks, that makes sense. Thank you to everyone who helped :)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0