Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: Scene isn't rendering - probably matrix issues?

  1. #1
    Intern Contributor
    Join Date
    May 2011
    Posts
    65

    Scene isn't rendering - probably matrix issues?

    Okay, well. First of all, this is the first time I've tried using vertex buffers with shaders, and for whatever reason, nothing is showing up when I set it to render the object.

    Here's my render() function:

    Code :
    float mvMat[16] = {	1, 0, 0, 0,
    					0, 1, 0, 0,
    					0, 0, 1, 0,
    					0, 0, 50, 1	};
    //mvMat is supposed to be an identity matrix translated 50 units forward
     
    float projMat[16];
     
    void Render(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	if (vbo != 0 && ibo != 0)
    	{
    		float mvpMat[16];
     
    		m3dMatrixMultiply44(mvpMat, mvMat, projMat);
     
    		glUseProgram(progFlatColor);
    		glUniform4f(uniformColor, 1.0, 0.0, 1.0, 1.0);
    		glUniformMatrix4fv(uniformMVP, 1, 0, mvpMat);
     
    		// Draw the main object
    		glBindBuffer(GL_ARRAY_BUFFER, vbo);
    		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
     
    		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, (const GLvoid*)0);
     
    		glDrawElements(GL_TRIANGLES, activeHeader.numTris, GL_UNSIGNED_SHORT, 0);
     
     
    		// Draw the wireframe
     
    		// Draw normals
     
    		// Draw PhysX shapes
     
    		// Draw bounding box/sphere
     
    		printf("ERROR: %i\n", glGetError());
    	}
     
    	SwapBuffers(g_hDC);
    }

    projMat is set up like so:
    Code :
    float fov = 70.0f;
    			float aspect = (rcClient.right/iRenderHeight);
    			float znear = 1.0f;
    			float zfar = 100.0f;
     
    			const float h = 1.0f/tan(fov*PI_OVER_360);
    			float neg_depth = znear-zfar;
    			projMat[0] = h / aspect;
    			projMat[1] = 0;
    			projMat[2] = 0;
    			projMat[3] = 0;
     
    			projMat[4] = 0;
    			projMat[5] = h;
    			projMat[6] = 0;
    			projMat[7] = 0;
     
    			projMat[8] = 0;
    			projMat[9] = 0;
    		    projMat[10] = (zfar + znear)/neg_depth;
    			projMat[11] = -1;
     
    			projMat[12] = 0;
    			projMat[13] = 0;
    			projMat[14] = 2.0f*(znear*zfar)/neg_depth;
    			projMat[15] = 0;

    And my SetupShaders() function looks like this:
    Code :
    unsigned long progFlatColor, progFlatTexture, progLitColor, progLitTexture;
    int uniformColor, uniformMVP = -1;
     
    bool DebugShader(unsigned int shader);
    bool DebugProgram(unsigned int program);
     
    const char VertSrcFlatColor[] = 
    "\
    //Flat Shader - Vertex\n\
    //Displays unshaded shapes using a uniform color\n\
    #version 120\n\
    \n\
    uniform mat4 MVP;\n\
    \n\
    attribute vec4 position;\n\
    \n\
    void main(void)\n\
    {\n\
    gl_Position = (position * MVP);\n\
    }\n\
    \0";
     
    const char FragSrcFlatColor[] =
    "\
    //Flat Shader - Fragment\n\
    \n\
    uniform vec4 color;\n\
    \n\
    void main(void)\n\
    {\n\
    gl_FragColor = color;\n\
    }\n\
    \0";
     
    enum ShaderMode
    {
    	SM_NONE = 0,
    	SM_CORE,
    	SM_ARB
    };
     
    void SetupShaders(void)
    {
    	//printf("INFO: vert shader: \n%s", VertSrcFlatColor);
    	//printf("INFO: frag shader: \n%s", FragSrcFlatColor);
     
    	ShaderMode sm = SM_NONE;
     
    	if (glewIsSupported("GL_VERSION_2_0"))
    	{
    		printf("INFO: Shaders supported natively in GL version 2.0 or higher.\n");
    		sm = SM_CORE;
    	}
    	else if (GLEW_ARB_vertex_shader && GLEW_ARB_vertex_shader)
    	{
    		printf("INFO: Shaders supported via extensions.\n");
    		sm = SM_ARB;
    	}
    	else
    	{
    		printf("ERROR: Shaders aren't supported.\n");
    		sm = SM_NONE;
    	}
     
    	unsigned long vertShader, fragShader;
     
    	const char* vv = VertSrcFlatColor;
    	const char* ff = FragSrcFlatColor;
     
    	switch (sm)
    	{
    	case SM_CORE:
    		vertShader = glCreateShader(GL_VERTEX_SHADER);
    		glShaderSource(vertShader, 1, &vv, NULL);
    		glCompileShader(vertShader);
    		if (!DebugShader(vertShader))
    		{
    			glDeleteShader(vertShader);
    			return;
    		}
     
    		fragShader = glCreateShader(GL_FRAGMENT_SHADER);
    		glShaderSource(fragShader, 1, &ff, NULL);
    		glCompileShader(fragShader);
    		if (!DebugShader(fragShader))
    		{
    			glDeleteShader(vertShader);
    			glDeleteShader(fragShader);
    			return;
    		}
     
    		progFlatColor = glCreateProgram();
    		glAttachShader(progFlatColor, vertShader);
    		glAttachShader(progFlatColor, fragShader);
     
    		glBindAttribLocation(progFlatColor, 0, "position");
     
    		glLinkProgram(progFlatColor);
    		glUseProgram(progFlatColor);
    		if (!DebugProgram(progFlatColor))
    		{
    			glDetachShader(progFlatColor, vertShader);
    			glDeleteShader(vertShader);
    			glDetachShader(progFlatColor, fragShader);
    			glDeleteShader(fragShader);
    			glDeleteProgram(progFlatColor);
    			return;
    		}
     
    		uniformColor = glGetUniformLocation(progFlatColor, "color");
    		uniformMVP = glGetUniformLocation(progFlatColor, "MVP");
     
    		if (uniformColor == -1 || uniformMVP == -1)
    			printf("ERROR: Could not find uniform locations.\n");
     
    		glDeleteShader(vertShader);
    		glDeleteShader(fragShader);
    		break;
     
    	case SM_ARB:
    		vertShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    		glShaderSourceARB(vertShader, 1, &vv, NULL);
    		glCompileShaderARB(vertShader);
    		fragShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
    		glShaderSourceARB(fragShader, 1, &ff, NULL);
    		glCompileShaderARB(fragShader);
     
    		progFlatColor = glCreateProgramObjectARB();
    		glAttachObjectARB(progFlatColor, vertShader);
    		glAttachObjectARB(progFlatColor, fragShader);
    		glLinkProgramARB(progFlatColor);
    		glUseProgramObjectARB(progFlatColor);
    		break;
     
    	default:
    		return;
    		break;
    	}
     
    	printf("INFO: Shaders successfully initialized.\n");
    }

    I'm about 99% sure that the buffers are getting filled properly - I printf'd the entire array of vertices, and they look correct.

  2. #2
    Intern Contributor
    Join Date
    May 2011
    Posts
    65

    Re: Scene isn't rendering - probably matrix issues?

    Bump?

    I could really use some help here, I really have no idea what I'm doing wrong. Am I handling matrices all wrong? Or am I doing it right, and the problem is elsewhere?

  3. #3
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    456

    Re: Scene isn't rendering - probably matrix issues?

    This line:
    Code :
    gl_Position = (position * MVP);
    should most likely be:
    Code :
    gl_Position = (MVP * position);

    Also, you should probably be using activeHeader.numTris * 3 in your glDrawElements call, since that argument is the number of vertices, not the number of triangles.

  4. #4
    Intern Contributor
    Join Date
    May 2011
    Posts
    65

    Re: Scene isn't rendering - probably matrix issues?

    I changed it as you suggested, but still nothing...

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Scene isn't rendering - probably matrix issues?

    Your projection matrix is wrong. Change it to this and see if it helps.

    Code :
    void GetPerspective(float fov, float asp, float zMin, float zMax, float* projMat) 
    {
     
    float yMax = zMin * tanf(fov*float(M_PI) / 360.0f);
    float yMin = -yMax;
    float xMin = yMin * asp;
    float xMax = -xMin; 
     
    projMat[0] = (2.0f * zMin) / (xMax - xMin);
    projMat[1] = 0;
    projMat[2] = (xMax + xMin) / (xMax - xMin);
    projMat[3] = 0;
     
    projMat[4] = 0;
    projMat[5] = (2.0f * zMin) / (yMax - yMin);
    projMat[6] = (yMax + yMin) / (yMax - yMin);
    projMat[7] = 0;
     
    projMat[8] = 0;
    projMat[9] = 0;
    projMat[10] = -((zMax + zMin) / (zMax - zMin));
    projMat[11] = -1;
     
    projMat[12] = 0;
    projMat[13] = 0;
    projMat[14] = -((2.0f * (zMax*zMin))/(zMax - zMin));
    projMat[15] = 0;
    }
    Regards,
    Mobeen

  6. #6
    Intern Contributor
    Join Date
    May 2011
    Posts
    65

    Re: Scene isn't rendering - probably matrix issues?

    Still nothing =(

  7. #7
    Intern Contributor
    Join Date
    May 2011
    Posts
    65

    Re: Scene isn't rendering - probably matrix issues?

    Still nothing =(

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Scene isn't rendering - probably matrix issues?

    What geometry are u pushing into the vbo? I mean the positions. Are u sure that they are within the near far clip range (1-100) that u have given. Try changing the near clip to 0.1?
    And I also dont see any viewing transformation, perhaps add in a translation of vec(0,0,-3) to move the camera three units on z axis.
    Regards,
    Mobeen

  9. #9
    Intern Contributor
    Join Date
    May 2011
    Posts
    65

    Re: Scene isn't rendering - probably matrix issues?

    Still nothing =(

  10. #10
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Scene isn't rendering - probably matrix issues?

    You did not answer me i asked
    What geometry are u pushing into the vbo? I mean the positions. Are u sure that they are within the near far clip range (1-100) that u have given?
    Can u show us what geometry u have?
    Regards,
    Mobeen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •