Sign in to follow this  
evilsanta

OpenGL CG Shader Woes

Recommended Posts

evilsanta    130
Hello, I have a 2d opengl game (orthogonal projection) that I am working on. I want to mess around with shaders so I attempted to implement CG into my program. I can compile and set the shaders just fine, however I am having some trouble actually altering anything with shaders. When I apply a shader to a quad for example, it tends to just disappear completely. I have a feeling this is because I am using orthogonal projection, do I need to tell CG my screen coordinates for example? Additionally I am not attempting any complex shaders, just things that should alter the color of a quad. Any help is appreciated.

Share this post


Link to post
Share on other sites
Lord_Evil    680
When using shaders, you have to apply the modelview and projection matrices yourself, i.e. in your shader.

The output will be a coordinate in screen coordinates, i.e. in range [-1,1] for x and y and [0,1] for z. If a vertex lies outside that range it will be clipped away. If all of your quad's vertices lie outside, your quad might get clipped away completely.

As a short test (to get your shaders working) render a triangle with coordinates (-0.5,-0.5,0.0), (0.5,-0.5,0.0), (0.0, 0.5, 0.0) which should appear right in the center of the screen if you don't apply any transformations in the shader (note that this assumes that front facing polies are CCW wound).

Share this post


Link to post
Share on other sites
evilsanta    130
Thanks for your reply. When I disabled orthogonal projection, I was able to render the triangle like you suggested. This worked, my shader turned the triangle green.

But how would I apply the modelview and projection matricies when using orthogonal projection?

Share this post


Link to post
Share on other sites
evilsanta    130
Hi, the following is my code to enable 2d.



GLint iViewport[4];
glGetIntegerv( GL_VIEWPORT, iViewport );
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
glOrtho( iViewport[0], iViewport[0]+iViewport[2],
iViewport[1]+iViewport[3], iViewport[1], -1, 1 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );
glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );







But I think it's like you said, I need to apply the modelview and projection matrices myself in my shader. Right now the shader I am using doesn't appear to be handling any of that. I am using a sample shader provided with the cg library. I am just unsure how to have the shader modified so that it outputs the correct screen coordinates. Right now when I apply the shader to a quad in 2d mode the quad appears a lot bigger than it should be, it's green, but the location and size is not what it should be.

The following code is my shader, if anyone can give me any help with getting this to work I would appreciate it, even if it's just the commands required to get it to work, as right now I am almost completely clueless.


// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard. See page 38.

struct C2E1v_Output {
float4 position : POSITION;
float3 color : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)
{
C2E1v_Output OUT;

OUT.position = float4(position,0,1);
OUT.color = float3(0,1,0);

return OUT;
}






[Edited by - evilsanta on June 25, 2009 1:04:29 AM]

Share this post


Link to post
Share on other sites
Lord_Evil    680
First, your shader need the modelview and projection matrices as uniform parameters, next you need to apply them. Since the combined modelview projection matrix (mvp) doesn't change during shader execution you might calculate it on the CPU and pass the result to the shader.


// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard. See page 38.

struct C2E1v_Output {
float4 position : POSITION;
float3 color : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION, uniform float4x4 mvp)
{
C2E1v_Output OUT;

OUT.position = mul(mvp, float4(position,0,1));
OUT.color = float3(0,1,0);

return OUT;
}



Share this post


Link to post
Share on other sites
evilsanta    130
Hello again,

I modified my shader code to match yours. I still seem to be having trouble getting it to work though. The following is my code that multiplies the two matrices. It seems like it is correct, am I using the correct set parameter command?


GLfloat modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);

glMatrixMode(GL_PROJECTION_MATRIX);
glMultMatrixf(modelview);

GLfloat output[16];
glGetFloatv(GL_MATRIX_MODE, output);

cgGLSetParameterArray4f( mvp,0,16,output);

Share this post


Link to post
Share on other sites
stalef    607
It looks like your shader is a vertex shader, you probably want to make a fragment shader instead...

If you don't activate a vertex shader, fixed function pipeline will take care of the projection matrices for you.

Share this post


Link to post
Share on other sites
Lord_Evil    680
You might use cgGLSetMatrixParameterfc(...) or - for simplicity - cgGLSetStateMatrixParameter(<your parameter>, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); With the second, CG will handle the reading and multiplication for you.

Share this post


Link to post
Share on other sites
AverageJoeSSU    564
Quote:
Original post by Lord_Evil
You might use cgGLSetMatrixParameterfc(...) or - for simplicity - cgGLSetStateMatrixParameter(<your parameter>, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); With the second, CG will handle the reading and multiplication for you.


I use that and it works great... saves a lot of math for me... although the inverse modelview matrix calc was broken until last release.

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  

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now