Jump to content
  • Advertisement
Sign in to follow this  
flyslasher

Shader issues. Not drawing a box.

This topic is 2105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I am trying to render a 2D white box. Well actually I wanted to render a texture, but am making sure that at a minimum I can render an empty square to know what I have done so far is working. Well it's not. I get a black screen with no white square as I expected.

Here is my drawing:

glClear( GL_COLOR_BUFFER_BIT );

//GLint iTexture = glGetUniformLocation( m_uiShaderProgram, "texture_sampler" );

glUseProgram( uiShaderProgram );

//glUniform1i( iTexture, 0 );

glUniformMatrix4fv( glGetUniformLocation( uiShaderProgram, "projection_matrix" ), 1, GL_FALSE, glm::value_ptr( mat4ProjectionMatrix ) );
glUniformMatrix4fv( glGetUniformLocation( uiShaderProgram, "view_matrix" ), 1, GL_FALSE, glm::value_ptr( mat4ViewMatrix ) );
glUniformMatrix4fv( glGetUniformLocation( uiShaderProgram, "model_matrix" ), 1, GL_FALSE, glm::value_ptr( mat4ModelMatrix ) );

//glActiveTexture( GL_TEXTURE0 );
//glBindTexture( GL_TEXTURE_2D, uiTextureID );

glBindBuffer( GL_ARRAY_BUFFER, uiVertexBuffer );
glVertexPointer( 3, GL_FLOAT, 0, (void*)0 );

glBindBuffer( GL_ARRAY_BUFFER, uiTexCoordBuffer );
glVertexPointer( 2, GL_FLOAT, 0, (void*)0 );

glDrawArrays( GL_QUADS, 0, 4 );

And these are my initializations for the various buffers:

//Shader program
const char* pchVertexShaderCode =
"#version 150\n"
"layout( location = 0 ) in vec3 vertex_position;"
"layout( location = 1 ) in vec2 vertex_uv;"
"out vec2 uv_coord;"
"uniform mat4 model_matrix, view_matrix, projection_matrix;"
"void main () {"
"  gl_Position = projection_matrix * view_matrix * model_matrix * vec4( vertex_position, 1.0 );"
"  uv_coord = vertex_uv;"
"}";

const char* pchFragmentShaderCode =
"#version 150\n"
"in vec2 uv_coord;"
"out vec4 frag_color;"
"uniform sampler2dD texture_sampler;"
"void main () {"
//"  frag_color = texture( texture_sampler, uv_coord ).rgb;"
"   frag_color = vec4( 1.0, 1.0, 1.0, 1.0 );"
"}";

unsigned int uiVertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( uiVertexShader, 1, &pchVertexShaderCode, NULL );
glCompileShader( uiVertexShader );

unsigned int uiFragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( uiFragmentShader, 1, &pchFragmentShaderCode, NULL );
glCompileShader( uiFragmentShader );

m_uiShaderProgram = glCreateProgram();
glAttachShader( uiShaderProgram, uiFragmentShader );
glAttachShader( uiShaderProgram, uiVertexShader );
glLinkProgram( uiShaderProgram );

//View matrix
mat4ViewMatrix = glm::lookAt( glm::vec3( 0.f, 0.f, 1.f ), glm::vec3( 0.f, 0.f, 0.f ), glm::vec3( 0.f, 1.f, 0.f ) );

//Projection matrix
mat4ProjectionMatrix = glm::ortho( 0.f, (float)iWindowWidth, (float)iWindowHeight, 0.f, 0.01f, 1000.f );

//Model matrix
glm::mat4 mat4T = glm::translate( glm::mat4( 1.f ), glm::vec3( m_fPosX, m_fPosY, 0.f ) );
glm::mat4 mat4Rx = glm::rotate( mat4T, m_fRotX, glm::vec3( 1.f, 0.f, 0.f ) );
glm::mat4 mat4M = glm::rotate( mat4Rx, m_fRotY, glm::vec3( 0.f, 1.f, 0.f ) );
glm::mat4 mat4S = glm::scale( glm::mat4( 1.f ), glm::vec3( m_fScale ) );
mat4ModelMatrix = mat4M * mat4S;

//Vertex and tex coord buffer
float fVertices[] = {
0.f, 0.f, 0.f,
100.f, 0.f, 0.f,
100.f, 100.f, 0.f,
0.f, 100.f, 0.f
};

float fTexCoords[] = {
0.f, 0.f,
1.f, 0.f,
1.f, 1.f,
0.f, 1.f
};

glGenBuffers( 1, &uiVertexBuffer );
glBindBuffer( GL_ARRAY_BUFFER, uiVertexBuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof( float ) * 12, fVertices, GL_STATIC_DRAW );

glGenBuffers( 1, &uiTexCoordBuffer );
glBindBuffer( GL_ARRAY_BUFFER, uiTexCoordBuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof( float ) * 8, fTexCoords, GL_STATIC_DRAW );

Share this post


Link to post
Share on other sites
Advertisement

Thanks. It helped solve a bug in my fragment shader, but I am getting a bug in my vertex shader that I don't reall understand.

"Error: 0:2: 'location' : syntax error syntax error"

Share this post


Link to post
Share on other sites

Okay. So I changed my version and did some checking to make sure that everything compiles/links fine, which it now does, but the problem still continues.

I tried using more simpled down shaders that should work, but nothing:

const char* pchVertexShaderCode =
    "#version 330\n"
    "layout( location = 0 ) in vec3 vertex_position;"
    "uniform mat4 model_matrix, view_matrix, projection_matrix;"
    "void main() {"
    "   gl_Position = projection_matrix * view_matrix * model_matrix * vec4( vertex_position, 1.0 );"
    "};";

const char* pchFragmentShaderCode =
    "#version 330\n"
    "out vec4 frag_color;"
    "void main() {"
    "   frag_color = vec4( 1.0, 1.0, 1.0, 1.0 );"
    "}";

Edit:
Okay I managed to draw the square by scaling my model matrix. I'm not sure, but it's not drawing my square correctly. If I leave the scale at for the model matrix, the square only appears as a single pixel on the screen.
 

Edited by PandaDragonThing

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!