screen coordinate not working with pixel values

SirYakalot
  • screen coordinate not working with pixel values SirYakalot

    I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why.

    If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates?

    // a struct for the vertex shader return value
    struct VSOut
    {
    float4 Col : COLOR;    // vertex color
    float4 Pos : SV_POSITION;    // vertex screen coordinates
    };
    
    // the vertex shader
    VSOut VS(float4 Col : COLOR, float4 Pos : POSITION)
    {
    VSOut Output;
    Output.Pos = Pos;    // set the vertex position to the input's position
    Output.Col = Col;    // set the vertex color to the input's color
    
    return Output;    // send the modified vertex data to the Rasterizer Stage
    }
    
    // the pixel shader
    float4 PS(float4 Col : COLOR) : SV_TARGET
    {
    return Col;    // set the pixel color to the color passed in by the Rasterizer Stage
    }
    
    // the primary technique
    technique10 Technique_0
    {
    // the primary pass
    pass Pass_0
    {
        SetVertexShader(CompileShader(vs_4_0, VS()));
        SetGeometryShader(NULL);
        SetPixelShader(CompileShader(ps_4_0, PS()));
    }
    }
    

  • Screen coordinates go from -1 to +1 along each axis. You can remap from pixel coordinates by doing

    x = (x / screenWidthInPixels) * 2.0 - 1.0
    y = (y / screenHeightInPixels) * 2.0 - 1.0
    

    This could be done in the vertex shader, in which case you'd have to pass in the screen width and height as parameters, or you could do it on the app side when you fill the vertex buffer.

Tags
c++ directx hlsl directx10
Related questions and answers
  • float4 Colour; // a struct for the vertex shader return value struct VSOut { float4 Col : COLOR; // vertex normal float4 Pos : SV_POSITION; // vertex screen coordinates }; // the vertex shader VSOut VS(float4 Norm : NORMAL, float4 Pos : POSITION) { VSOut Output; Output.Pos = mul(Pos, Transform); // transform the vertex from 3D to 2D Output.Col = AmbientCol; // set... return Output; // send the modified vertex data to the Rasterizer Stage } // the pixel shader float4 PS(float4 Col : COLOR) : SV_TARGET { return Col; // set the pixel color to the color passed

  • I'm writing a generic ShaderProgram class that compiles a set of Shader objects, passes args to the shader (like vertex position, vertex normal, tex coords etc), then links the shader components into a shader program, for use with glDrawArrays. My vertex data already exists in a VertexBufferObject that uses the following data structure to create a vertex buffer: class CustomVertex { public... ); glDisableClientState( GL_COLOR_ARRAY ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } Back to the Vertex Array Object though. My code for creating the Vertex

  • ); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i... float frac = (time - (float)(frameIndex1 - startFrame) / (float)(endFrame - startFrame + 1)) * (endFrame - startFrame + 1); // I ADDED THESE FROM NEHE'S TUTORIAL FOR FIRST PASS...], pos[2]); } } glEnd(); // ADDED THESE FROM NEHE'S FOR SECOND PASS (OUTLINE) glDisable (GL_TEXTURE_1D); // Disable 1D Textures ( NEW

  • [] = { // first input element: position {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0}, // second input element: color {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0} }; // use the input element descriptions to create the input layout device...() { } Cube::~Cube() { pBuffer->Release(); // why does this only work when put here? because it's created here? I thnk so, why not iBuffer though? } void Cube::Draw() { render_frame(); } void Cube

  • shader in every node. Other option I was thinking was making some helper functions to deal with the simpler cases, which would set some default parameters and would ask only the most basic ones... the Diffuse color variation or making it store any value that can change but making it so random that I woulnd't know how to relate the data to the other frames, or need a complex system for it. That's why... know if my speculations are ok, as I don't have much experience with 3d animations yet. I want to make a well decision as any option I choose would require a lot of work to get it to render and I

  • }; // for the old/new screen command // Some nCurses setup int r = 0, c = 0; // current row and column (upper-left is (0,0)) const int nrows = 56, // number of rows in window ncols = 79... from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've... saves the data into a vector which represents the console window. It is later used to print the "screen" to the console at a set rate (long double speed), as seen in the for loop after the map drawings

  • I would have thought that if the object is on-screen that this function should return screen coordinates. When used in conjunction with the directX draw text function, it works fine. Textual overlays...() will return a vector3 that has a 0 z component and x and y values from about 2 to roughly 33. Now how on earth can me with a calculator be better than the computer? what am I missing? 0.0f / 720.0f) * 2.0f... (texture coords for the screen go from -1 to 1 in each axis) but headPos has the value of -1.#IND000 in both the x and y. I think this may be because D3DXVec3Project is returning MASSIVE numbers, although I

  • ->texCoordY); glVertex3f(pos[0], pos[1], pos[2]); } } glEnd(); What I'd like to do is to calculate all positions before hand, store them in a Vertex array and then draw..._template_1.exe: 0xC0000005: Access violation reading location 0xed5243c0" pointing at line 7 Vec3f pos = v1->pos * (1 - frac) + v2->pos * frac; where the two Vs seems to have no value...I have a MD2 model loader, I am trying to substitute its immediate drawing function with a Vertex Buffer Object one.... I am getting a really annoying access violation reading error and I can't

  • I'm writing a simple class to draw all the debugging lines I have in my scene at once. The code in the draw loop is this so far: (If I put for example, 2 instead of temporary2DVerts.size() where I have marked with //THIS LINE, the code works fine.) when I run the code below the line breaks //HERE. Access violation reading location 0x00000000. seems like the create buffer line is not working, but why? what's the solution? D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(VERTEX) * temporary2DVerts.size();// THIS LINE bd.BindFlags = D3D10_BIND_VERTEX_BUFFER

Data information