vec4 texel = texture2D(tex, gl_TexCoord[0].st);
if (texel.rgb == vec3(0,0,0)) {...
FS use tex or color?
#1
Posted 21 July 2012 - 05:12 PM
#2
Posted 22 July 2012 - 03:43 AM
#3
Posted 22 July 2012 - 04:13 AM
if there is a texture for this poly, then... glBindTexture(GL_TEXTURE_2D, TextureList[j].texID); glColor4f(1,1,1,1); else glBindTexture(GL_TEXTURE_2D, 0); glColor4f(clr.r, clr.g, clr.b, clr.a);
Now, in the fragment shader, I want to know if I need to use the solid color, or the texture color... gl_FragColor = ?
#4
Posted 22 July 2012 - 05:11 AM
#5
Posted 22 July 2012 - 09:53 AM
This saves the overhead of swapping shaders.
Then in the fragment shader you can do ...
vec4 texel = texture2D(tex,texcoord); return texel * colour;
#6
Posted 22 July 2012 - 11:00 AM
Also, why are you using glColor? You should be using VBOs. Also, you don't need to waste cycles by resetting the active texture to null. Best just to leave it at the last texture used and switch it when you're ready to use a new texture. Not a major performance boost, but gets you into the habit of reducing state switching.
#7
Posted 22 July 2012 - 04:05 PM
#8
Posted 22 July 2012 - 04:10 PM
TheNut, on 22 July 2012 - 11:00 AM, said:
hmmm, I don't know! It's just what I've been learning to do!
How can I use VBO to do this instead? I mean, don't I have to creat a VBO the size of the viewport and fill it with the pixel color? That would take too long!
#9
Posted 22 July 2012 - 04:50 PM
Alienizer said:
To understand this better, you need to learn about VBOs, or vertex buffer objects. I'm rather surprised you don't already know this if you're working with shaders. I find it bizarre that any learning material on shaders would still be using classic OpenGL rendering routines. You can google for topics on this, but I would recommend reading the specification guides at OpenGL.org to avoid learning deprecated APIs.
#10
Posted 22 July 2012 - 05:40 PM
I'm learning from this board and from docs found on google, but it seems that I'm getting more and more confused. Some tutorials tells you to do it "this way" and other "that way". When I try one way and finally make it somewhat work, and then use something I need from another tutorial requiring #version 130 or something, then everything I've done before gives me a bunch of deprecated messages, so I have to redo everything from scratch, and don't know how to start!
Now I'm looking at the PDF from http://www.khronos.org/opengl/ and in the deprecated section E, looks like everything is deprecated!!?? So I'm lost, very very lost.
Where do you guys turn to when you need to lookup some references? What docs should I read to get in the right direction and be on the same wavelength as you guys?
#11
Posted 22 July 2012 - 05:50 PM
#12
Posted 22 July 2012 - 08:54 PM
Vertex colouring + texturing will definitely give you what you want. FYI you can pass in any vertex colouring format, including RGBA data in byte, short, or floating point format. Your shader should look something like this.
// Vertex shader
attribute vec4 Vertex; // xyzw data from VBO
attribute vec2 Uv; // uv data from VBO
attribute vec4 Colour; // RGBA data from VBO
uniform mat4 ProjectionMatrix; // camera projection
uniform mat4 ViewMatrix; // camera transformation
uniform mat4 ModelMatrix; // object transformation
varying vec4 vertexColour; // Output to fragment shader
varying vec2 vertexUv; // Output to fragment shader
void main ()
{
// Don't use ftransform() - assume everything is your responsibility
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * Vertex;
vertexColour = Colour
vertexUv = Uv
}
// Fragment shader
uniform sampler2D MyTexture; // Set via C++. Objects without a texture will pass in a white texture by default.
// Inputs interpolated from vertex shader
varying vec4 vertexColour;
varying vec2 vertexUv;
void main ()
{
gl_FragColor = vertexColour * texture2D(MyTexture, vertexUv);
}
#13
Posted 22 July 2012 - 09:36 PM
So I see what you mean. Use glsl as computation only, and everything else camera(pos,dir), lights etc are all defined in the main program. So no need to use one or more of the 8 bilt-in lights anymore, but we have to do every shading, and that's what the fragment shader will do right?
What I'm not sure about your code example is the VBO part, then rest I understand fully, if I'm right about what I just said above.
So what is the VBO? Is it like a texture2d where we put in info to be passed to the vertex shader?
Thanks for the info and the time to explain! I appreciate it.
#14
Posted 23 July 2012 - 02:45 AM
Alienizer said:
To understand VBOs, you have to rewind the clock a bit. It all started with glBegin, glVertex3f x 1000 calls and finally glEnd. At first this was simple and it just worked. Then better hardware came out and more polygons were being pushed. The function call overhead on the CPU was starting to become problematic and so they moved to vertex arrays. Instead of calling glVertex a couple thousand times, you now prepared an array of all your vertices, uvs, normals, etc. and made one call to render the whole thing in one shot. This was great for a while, but then hardware again improved and more polygons were being pushed. This time, the bottleneck was the bus between system memory and video memory. Sending tens of thousands of polygons every frame was creating New York city style traffic congestion, slowing everything down. To resolve this, they brought in VBOs. Like vertex arrays, except the geometry data is now optimally stored in video memory, like texture data. When it comes time to draw, instead of pushing all those polygons down the bus, the video card just accesses the data from it's own memory, which is blazing fast in comparison. It's like how every game developer sleeps on the company sofa. No need to waste time going home. Just wake up right at the office
As for the buffers themselves, it's up to you to decide how you want to craft them. The vertex shader accesses this data via the attribute variables. Some people create one massive array and stride the data. Others may create several buffers, one to store each component of the geometry. For example, one buffer for vertices, one for UVs, another for colour. This is how I often organize my data because sometimes I may want to update a VBO, but not have to resend the entire geometry back to the video card. For example, with particle engines you will update the vertex coordinates of each particle using something called a dynamic VBO. You don't need to reupload UV data, so it's much more efficient to just update the vertex buffer and leave the others alone. The only drawback is that instead of activating a single VBO, you have to activate several for each object before you render it. As you can see, it starts to add a little function overhead, but from my tests it's quite negligible. To each their own.
#15
Posted 23 July 2012 - 03:44 AM
So we don't need to use any of the gl_* built-in variables anymore? Or do we still need to use gl_Vertex, gl_Normal, gl_FragColor and others? But none of the gl_Lights and stuff?
What about...
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
do we still need this if everything is in the VBO?
I don't know how to access the VBO from a shader. Is it via the uniform? Something like...
glBindBufferARB(GL_ARRAY_BUFFER_ARB, FragmentsVBO);
Could you guide me to a good site, or have, a skeleton in C++ on how to use the VBO for the basic stuff? I understand clearly your explanation, but now I just need a little kick start to know the layout of the c++ code and how the VBOs are accessed in the shaders. Sorry if I'm so demanding, but I really want to do it right this time and come out of the cave I'm in right now!
#16
Posted 23 July 2012 - 04:14 AM
http://www.opengl.or...-_just_examples
why is it deprecated??? or am I worrying for nothing?
#17
Posted 23 July 2012 - 10:48 AM
Alienizer said:
Alienizer said:
// Create vertex buffer
int vboVertex;
glGenBuffers(1, vboVertex);
if ( vboVertex == 0 )
{ error! }
// Upload vertices to vertex buffer
float vertices[] = {x, y, z, x, y, z, x, y, z......};
glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
glBufferData(GL_ARRAY_BUFFER, NumberOfVertices * sizeof(float), vertices, GL_STATIC_DRAW);
// You can optionally delete the vertices array at this point to save memory. The video card now has everything.
// Now set the shader attribute (assuming you compiled and loaded your shader)
int atribVertex = glGetAttribLocation(MyShaderProgramID, "Vertex"); // In vertex shader, you should have "attribute vec* Vertex", otherwise this function will return 0.
glEnableVertexAttribArray(atribVertex);
// Bind the vertex buffer to the attribute
glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
glVertexAttribPointer(atribVertex, 3, GL_FLOAT, false, 0, 0);
// Draw
glDrawElements(...) or glDrawArrays(...)
That's the gist of it. Basically do this for each buffer, or create one massive buffer with everything and define the vertex stride parameter when you call glVertexAttribPointer. You'll note that I haven't suffixed any ARB extension to the end of these methods, as you will see many websites do. A long long time ago these were APIs introduced by the ARB to extend OpenGL. They have since become part of the core with OpenGL 2.X. Many people haven't caught on yet
#18
Posted 23 July 2012 - 08:21 PM
glDrawArrays(GL_TRIANGLES, 0, totPoly);
and in the vertex shader...
attribute vec3 Vertex;
void main(void)
{
gl_Position = vec4(Vertex, 1);
}
and in the fragment shader...
void main(void) {
gl_FragColor = vec4(0.1, 0.4, 0.9, 1.0);
}
But nothing is drawn, just the blue background set by glClearColor
Can you enlighten me a little more please?
#19
Posted 23 July 2012 - 08:47 PM
-- Edit
BTW, GLSL is quite formal in that you should declare any floating point values with a decimal notation. For example, vec4(Vertex, 1) is considered bad form and will throw errors on certain GLSL profiles. You should declare it as vec4(Vertex, 1.0). Make sure to check for compiler errors to avoid these sort of mishaps.
#20
Posted 23 July 2012 - 09:28 PM
I also added glEnableClientState(GL_VERTEX_ARRAY); before glDrawArrays and glDisableClientState(GL_VERTEX_ARRAY); after it. Is this correct? Or I don't need to do this?
I did now I had to clamp my vertices to -1.0...1.0 I just passed them as they are. ow do you make them in this range? I mean, a scene bbox could be -100..1000 or even 500...600 !??
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












