creating my first simple geometry shader - opengl

I'm trying to create the simplest geometry shader ever.
I have a working vertex and fragment shaders and I'm drawing triangles with
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
Vertex Shader:
// vertex shader
#version 120
attribute vec3 in_position;
uniform mat4 modelViewProjectionMatrix;
void main() {
gl_Position = modelViewProjectionMatrix * vec4(in_position, 1.0);
}
Fragment Shader:
// fragment shader
#version 120
void main() {
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
Geometry Shader
//geometry shader
#version 120
#extension GL_EXT_geometry_shader4: enable
void main(void) {
for (int i = 0; i < gl_VerticesIn; i++) {
gl_Position = gl_PositionIn[i];
EmitVertex();
}
EndPrimitive();
}
my compile method which compiles all my shaders works without returning any gl_error
This is my attach and link method and this is where I get when I try to specify the output values for my shader
void attached_and_link_shaders() {
glAttachShader(programid, shaderV);
glAttachShader(programid, shaderG);
glAttachShader(programid, shaderF);
glProgramParameteriEXT(programid, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
gl_error("EXT INPUT TYPE");
// error here
glProgramParameteriEXT(programid, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLES);
gl_error("EXT OUTPUT TYPE"); // OUTPUT TYPE 1281 invalid value
glProgramParameteriEXT(programid, GL_GEOMETRY_VERTICES_OUT_EXT, 3);
gl_error("EXT VERTICES OUT");
glLinkProgram(programid);
}
Where could my problem be? any references to good geometry shader tutorials for version 120 would be helpful,
Edit my out vertices were in correct, the solution is using GL_TRIANGLE_STRIP
my problem is solved and I feel like crying
Thanks

Related

Textures from one VAO being applied to another VAO

I am currently trying to render text in OpenGL using bitmap files. When it's by itself, the font looks as expected.
Exhibit A:
When adding a separate texture (a picture) in a separate VAO OR more text in the same VAO, "This engine can render text!" still looks the same.
However, when adding both the texture in a separate VAO AND more text in the same VAO, the texture of "This engine can render text!" gets modified.
Exhibit B:
What's really strange to me is that the textures seem to be blended, and that it only affects a few vertices rather than the entire VBO.
Is this a problem of OpenGL/poor drivers, or is it something else? I double checked the vertices and the 'his' aren't being rendered with the picture texture active. I am using OSX which is notorious for poor OpenGL support, if it might help.
My rendering loop:
//scene is just a class that bundles all the necessary information for rendering.
//when rendering text, it is all batched inside of one scene,
//so independent textures of text characters should be impossible
glUseProgram(prgmid);
for(auto& it : scene->getTextures() )
{
//load textures
const Texture::Data* data = static_cast<const Texture::Data*>(it->getData() );
glActiveTexture(GL_TEXTURE0 + it->getID() );
glBindTexture(GL_TEXTURE_2D, it->getID() );
glUniform1i(glGetUniformLocation(prgmid, data->name), it->getID() );
}
for(auto& it : scene->getUniforms() )
{
processUniforms(scene, it, prgmid);
}
glBindVertexArray(scene->getMesh()->getVAO() );
glDrawElements(GL_TRIANGLES, scene->getMesh()->getDrawCount(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
Shaders of the text:
//Vertex
#version 330 core
layout (location = 0) in vec4 pos;
layout (location = 1) in vec2 fontTexCoord;
out vec2 fontTexCoords;
uniform mat4 __projection;
void main()
{
fontTexCoords = vec2(fontTexCoord.x, fontTexCoord.y);
gl_Position = __projection * pos;
}
//Frag
#version 330 core
in vec2 fontTexCoords;
out vec4 color;
uniform sampler2D fontbmp;
void main()
{
color = texture(fontbmp, fontTexCoords);
if(color.rgb == vec3(0.0, 0.0, 0.0) ) discard;
}
Shaders of the picture:
//vert
#version 330 core
layout (location = 0) in vec4 pos;
layout (location = 1) in vec2 texCoord;
out vec2 TexCoords;
uniform mat4 __projection;
uniform float __spriteFrameRatio;
uniform float __spriteFramePos;
uniform float __flipXMult;
uniform float __flipYMult;
void main()
{
TexCoords = vec2(((texCoord.x + __spriteFramePos) * __spriteFrameRatio) * __flipXMult, texCoord.y * __flipYMult);
gl_Position = __projection * pos;
}
//frag
#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D __image;
uniform vec4 __spriteColor;
uniform bool __is_texture;
void main()
{
if(__is_texture)
{
color = __spriteColor * texture(__image, TexCoords);
}
else
{
color = __spriteColor;
}
}
EDIT:
I believe the code that is causing the problem has to do with generating the buffers. It's called everytime when rendered for each scene (VAO, VBO, EBO, texture) object.
if(!REALLOCATE_BUFFER && !ATTRIBUTE_ADDED) return;
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
if(REALLOCATE_BUFFER)
{
size_t vsize = _vert.size() * sizeof(decltype(_vert)::value_type);
size_t isize = _indc.size() * sizeof(decltype(_indc)::value_type);
if(_prevsize != vsize)
{
_prevsize = vsize;
glBufferData(GL_ARRAY_BUFFER, vsize, &_vert[0], _mode);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, isize, &_indc[0], _mode);
}
else
{
glBufferSubData(GL_ARRAY_BUFFER, 0, vsize, &_vert[0]);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, isize, &_indc[0]);
}
}
if(ATTRIBUTE_ADDED)
{
for(auto& itt : _attrib)
{
glVertexAttribPointer(itt.index, itt.size, GL_FLOAT, itt.normalized, _currstride * sizeof(GLfloat), (GLvoid*)(itt.pointer * sizeof(GLfloat) ) );
glEnableVertexAttribArray(itt.index);
}
}
glBindVertexArray(0);
When we comment out glBufferSubData so that glBufferData is always called, the problem area flickers and iterates through all textures, including the ones in other VAOs.
EDIT 2:
For some reason, everything works as expected when the text is rendered with a different mode than the picture, say GL_STREAM_DRAW and GL_DYNAMIC_DRAW, for instance. How can this be?
So the thing I messed up was that getDrawCount() was returning the number of VERTICES rather than the number of indices. Astonishingly, this didn't cause OpenGL to throw any errors and fixing it solved the flickering.

gl_VertexID always zero in any useful geometry shader

I am using indexed rendering and a geometry shader. If I pass gl_VertexID to the geometry shader, it works fine as long as I do not emit any vertices; if I emit one or more vertices, gl_VertexID (passed as any name) is zero. Why?
Using the shaders below, the geometry shader will put the correct indices into my feedback buffer, if and only if I comment out both EmitVertex calls. What am I missing?
(I can work around it, but it is bugging the hell out of me!)
VERTEX SHADER
#version 440
in vec4 position;
out VSOUT{
vec4 gl_Position;
int index;
} vsout;
uniform mat4 gl_ModelViewMatrix;
void main(){
gl_Position = gl_ModelViewMatrix * position;
vsout.index = gl_VertexID;
vsout.gl_Position = gl_Position;
}
GEOMETRY SHADER
#version 440
#extension GL_ARB_shader_storage_buffer_object : enable
layout (lines) in;
layout (line_strip) out;
in VSOUT{
vec4 gl_Position;
int index;
} vdata[];
layout (std430, binding=0) buffer FeedbackBuffer{
vec2 fb[];
};
void main(){
int i = vdata[0].index;
int j = vdata[1].index;
fb[gl_PrimitiveIDIn][0] = vdata[0].index;
fb[gl_PrimitiveIDIn][1] = vdata[1].index;
gl_Position = gl_in[0].gl_Position;
EmitVertex();
gl_Position = gl_in[1].gl_Position;
EmitVertex();
}
FRAGMENT SHADER
#version 430
out vec4 outputColor;
void main(){
outputColor = vec4(.5,.5,.5,.5);
}
So this looks like an nVidia implementation thing. If I run these shaders on a GeForce GTX580, behaviour is as described above. Using an AMD FirePro V5900, it behaves as I'd expect, with the correct values in the feedback buffer whether or not I emit vertices.

Simple GLSL render chain doesn't draw reliably

I have a simple compositing system which is supposed to render different textures and a background texture into an FBO. It also renders some primitives.
Here's an example:
I'm rendering using a simple GLSL shader for the texture and another one for the primitive. Also, I'm waiting for each shader to finish using glFinish after each glDrawArrays call.
So basically:
tex shader (background tex)
tex shader (tex 1)
primitive shader
tex shader (tex 2)
tex shader (tex 3)
When I only do this once, it works. But if I do another render pass directly after the first one finished, some textures just aren't rendered.
The primitive however is always rendered.
This doesn't happen always, but the more textures I draw, the more often this occurs.
Thus, I'm assuming that this is a timing problem.
I tried to troubleshoot for the last two days and I just can't find the reason for this.
I'm 100% sure that the textures are always valid (I downloaded them using glGetTexImage to verify).
Here are my texture shaders.
Vertex shader:
#version 150
uniform mat4 mvp;
in vec2 inPosition;
in vec2 inTexCoord;
out vec2 texCoordV;
void main(void)
{
texCoordV = inTexCoord;
gl_Position = mvp * vec4(inPosition, 0.0, 1.0);
}
Fragment shader:
#version 150
uniform sampler2D tex;
in vec2 texCoordV;
out vec4 fragColor;
void main(void)
{
fragColor = texture(tex, texCoordV);
}
And here's my invocation:
NSRect drawDestRect = NSMakeRect(xPos, yPos, str.texSize.width, str.texSize.height);
NLA_VertexRect rect = NLA_VertexRectFromNSRect(drawDestRect);
int texID = 0;
NLA_VertexRect texCoords = NLA_VertexRectFromNSRect(NSMakeRect(0.0f, 0.0f, 1.0f, 1.0f));
NLA_VertexRectFlipY(&texCoords);
[self.texApplyShader.arguments[#"inTexCoord"] setValue:&texCoords forNumberOfVertices:4];
[self.texApplyShader.arguments[#"inPosition"] setValue:&rect forNumberOfVertices:4];
[self.texApplyShader.arguments[#"tex"] setValue:&texID forNumberOfVertices:1];
GetError();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, str.texName);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glFinish();
The setValue:forNumberOfCoordinates: function is an object-based wrapper around OpenGL's parameter application functions. It basically does this:
glBindVertexArray(_vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, bytesForGLType * numVertices, value, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray((GLuint)self.boundLocation);
glVertexAttribPointer((GLuint)self.boundLocation, numVectorElementsForType, GL_FLOAT, GL_FALSE, 0, 0);
Here are two screenshots of what it should look like (taken after first render pass) and what it actually looks like (taken after second render pass):
https://www.dropbox.com/s/0nmquelzo83ekf6/GLRendering_issues_correct.png?dl=0
https://www.dropbox.com/s/7aztfba5mbeq5sj/GLRendering_issues_wrong.png?dl=0
(in this example, the background texture is just black)
The primitive shader is as simple as it gets:
Vertex:
#version 150
uniform mat4 mvp;
uniform vec4 inColor;
in vec2 inPosition;
out vec4 colorV;
void main (void)
{
colorV = inColor;
gl_Position = mvp * vec4(inPosition, 0.0, 1.0);
}
Fragment:
#version 150
in vec4 colorV;
out vec4 fragColor;
void main(void)
{
fragColor = colorV;
}
Found the issue... I didn't realize that the FBO is drawn to the screen already after the first render pass. This happens on a different thread and wasn't locked properly.
Apparently the context was switched while the compositing took place which explains why it caused different issues randomly depending on when the second thread switched the context.

OpenGL Mac OSX vertex shader not linking to fragment shader

I'm having problems to compile and execute a simple OpenGL application in Mac OSX 10.9. It works just fine in windows. But in Mac I keep getting some errors while linking the vertex shader to the fragment shader in the shaderProgram.
Here is my console Output.
4.1 INTEL-8.26.34
ERROR! could not link the shader program
WARNING: Output of vertex shader 'outColor' not read by fragment shader
ERROR: Input of fragment shader 'inColor' not written by vertex shader
Program ended with exit code: 0
Here is the Method that I'm using to link both together.
GLuint createShaderProgram(GLuint vertexShader, GLuint fragmentShader)
{
// Create and link the shader program
GLuint shaderProgram = glCreateProgram(); // create handle
if (!shaderProgram) {
ERROR("could not create the shader program", false);
return NULL_HANDLE;
}
glAttachShader(shaderProgram, vertexShader); // attach vertex shader
glAttachShader(shaderProgram, fragmentShader); // attach fragment shader
glLinkProgram(shaderProgram);
// check to see if the linking was successful
int linked;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linked); // get link status
if (!linked) {
ERROR("could not link the shader program", false);
int maxLength;
int length;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
char* log = new char[maxLength];
glGetProgramInfoLog(shaderProgram, maxLength,&length,log);
printf(log);
return NULL_HANDLE;
}
return shaderProgram;
}
Here is my vertexshader.
#version 410 core
layout (location = 0) in vec3 inPosition;
layout (location = 3) in vec3 inColor;
layout (location = 3) smooth out vec4 outColor;
void main()
{
gl_Position = vec4(inPosition, 1.0);
outColor = vec4(inColor, 1.0);
}
Here is the fragShader
#version 410 core
layout (location = 3) smooth in vec4 inColor;
layout (location = 0) out vec4 outColor;
void main()
{
outColor = inColor;
}
Thanks!!
the names need to match:
have the name of the output of the vertex shader match the name of the input of the fragment shader:
#version 410 core
layout (location = 0) in vec3 inPosition;
layout (location = 3) in vec3 inColor;
smooth out vec4 vertOutColor;
void main()
{
gl_Position = vec4(inPosition, 1.0);
vertOutColor = vec4(inColor, 1.0);
}
fragment shader:
#version 410 core
smooth in vec4 vertOutColor;
layout (location = 0) out vec4 outColor;
void main()
{
outColor = vertOutColor;
}

Combining two texture in fragment shader

I'm working on implementing deferred shading to my game. I have rendered the diffuse textures to a render target, and I have lighting rendered to a render target. Both of which I know are fine because I can render them straight to the screen with no problems. What I want to do is combine both the diffuse map and the light map in a shader to create a final image. Here is my current fragment shader, which results in a black screen.
#version 110
uniform sampler2D diffuseMap;
uniform sampler2D lightingMap;
void main()
{
vec4 color = texture(diffuseMap, gl_TexCoord[0].st);
vec4 lighting = texture(lightingMap, gl_TexCoord[0].st);
vec4 finalColor = color;
gl_FragColor = finalColor;
}
Shouldn't this result in the same thing as just straight up drawing the diffuse map?
I set the sampler2d with this method
void ShaderProgram::setUniformTexture(const std::string& name, GLint t) {
GLint var = getUniformLocation(name);
glUniform1i(var, t);
}
GLint ShaderProgram::getUniformLocation(const std::string& name) {
if(mUniformValues.find(name) != mUniformValues.end()) {
return mUniformValues[name];
}
GLint var = glGetUniformLocation(mProgram, name.c_str());
mUniformValues[name] = var;
return var;
}
EDIT: Some more information. Here is the code where I use the shader. I set the two textures, and draw a blank square for the shader to use. I know for sure, my render targets are working, as I said before, because I can draw them fine using the same getTextureId as I do here.
graphics->useShader(mLightingCombinedShader);
mLightingCombinedShader->setUniformTexture("diffuseMap", mDiffuse->getTextureId());
mLightingCombinedShader->setUniformTexture("lightingMap", mLightMap->getTextureId());
graphics->drawPrimitive(mScreenRect, 0, 0);
graphics->clearShader();
void GraphicsDevice::useShader(ShaderProgram* p) {
glUseProgram(p->getId());
}
void GraphicsDevice::clearShader() {
glUseProgram(0);
}
And the vertex shader
#version 110
varying vec2 texCoord;
void main()
{
texCoord = gl_MultiTexCoord0.xy;
gl_Position = ftransform();
}
In GLSL version 110 you should use:
texture2D(diffuseMap, gl_TexCoord[0].st); // etc.
instead of just the texture function.
And then to combine the textures, just multiply the colours together, i.e.
gl_FragColor = color * lighting;
glUniform1i(var, t);
The glUniform functions affect the program that is currently in use. That is, the last program that glUseProgram was called on. If you want to set the uniform for a specific program, you have to use it first.
The problem ended up being that I didn't enable the texture coordinates for the screen rectangle I was drawing.