Rendering quad but something is missing

Started by
5 comments, last by Gyiove Sparkle 9 years, 4 months ago

Hello everyone!

This is first time im rendering things with vbo and manually controlling matrices.
Here's the code:


namespace grpc
{
	classwindow *window;
	fw *render;
	glm::mat4 modelMatrix;
	glm::mat4 viewMatrix;
	glm::mat4 projectionMatrix;
	glm::mat4 modelViewMatrix;
	glm::mat4 modelViewProjectionMatrix;
	glm::mat3 normalMatrix;
	shader simple;

}

struct vertexData{
	float position[3];
	float normal[3];
	float tangent[3];
	float color[3];
	float uv[2];
};

struct vertexID{
	unsigned int id[3];
};

unsigned int vbo, vbo2; // vbo2 is vectex indexes ( is that how they call it? )
void graphic_frame()
{
	if( grpc::window->winactive ) // winactive is 0 when window is minimized
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glClearColor(0,0,0,0); // changing values here will affect the screen
                // so everything should just draw fine on screen

		glEnable(GL_DEPTH_TEST);
		
		double size[2];
		size[0] = (double)grpc::window->size[0];
		size[1] = (double)grpc::window->size[1];
		grpc::projectionMatrix = glm::perspective(50.0, size[0]/size[1], 1.0, 9999.0);
		grpc::modelMatrix = glm::mat4(1.0);
		grpc::viewMatrix = glm::mat4(1.0);
		
                // tried every angle/pos still no quad on screen
		grpc::viewMatrix *= glm::rotate(0.0f, glm::vec3(1.0, 0.0, 0.0));
		grpc::viewMatrix *= glm::rotate(0.0f, glm::vec3(0.0, 1.0, 0.0));
		grpc::viewMatrix *= glm::rotate(0.0f, glm::vec3(0.0, 0.0, 1.0));
		grpc::viewMatrix *= glm::translate(glm::vec3(10.0, 0.0, 0.0));

		grpc::simple.use();

                // cout << glGetUniformLocation(grpc::simple.index,"modelMatrix") is saying that it returns -1
                // it should not do it am i right?
		glUniformMatrix4fv(glGetUniformLocation(grpc::simple.index,"modelMatrix"),
1,GL_FALSE, &grpc::modelMatrix[0][0]);

		glUniformMatrix4fv(glGetUniformLocation(grpc::simple.index,"viewMatrix"),
1,GL_FALSE, &grpc::viewMatrix[0][0]);

		glUniformMatrix4fv(glGetUniformLocation(grpc::simple.index,"projectionMatrix"),
1,GL_FALSE, &grpc::projectionMatrix[0][0]);

		glUniformMatrix4fv(glGetUniformLocation(grpc::simple.index,"modelViewMatrix"),
1,GL_FALSE, &grpc::modelViewMatrix[0][0]);

		glUniformMatrix4fv(glGetUniformLocation(grpc::simple.index,"modelViewProjectionMatrix"),
1,GL_FALSE, &grpc::modelViewProjectionMatrix[0][0]);

		glUniformMatrix3fv(glGetUniformLocation(grpc::simple.index,"normalMatrix"),
1,GL_FALSE, &grpc::normalMatrix[0][0]);

		int vertex=glGetAttribLocation(grpc::simple.index,"vertex");
                // vertex variable holds 0 what should be fine

		int normal=glGetAttribLocation(grpc::simple.index,"normal");
                // normal variable holds -1 as well as tangent, color and UV ... weird

		int tangent=glGetAttribLocation(grpc::simple.index,"tangent");
		int color=glGetAttribLocation(grpc::simple.index,"color");
		int UV=glGetAttribLocation(grpc::simple.index,"UV");
	
		glBindBuffer(GL_ARRAY_BUFFER,vbo);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo2);
                // vbo holds 1 and vbo2 holds 2
	
		glEnableVertexAttribArray(vertex);
		glVertexAttribPointer(vertex,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),0);
	
		glEnableVertexAttribArray(normal);
		glVertexAttribPointer(normal,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(3*sizeof(float)));
	
		glEnableVertexAttribArray(tangent);
		glVertexAttribPointer(tangent,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(6*sizeof(float)));
	
		glEnableVertexAttribArray(color);
		glVertexAttribPointer(color,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(9*sizeof(float)));
	
		glEnableVertexAttribArray(UV);
		glVertexAttribPointer(UV,2,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(12*sizeof(float)));
	
		glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT,0);
	
		glDisableVertexAttribArray(vertex);
		glDisableVertexAttribArray(normal);
		glDisableVertexAttribArray(tangent);
		glDisableVertexAttribArray(color);
		glDisableVertexAttribArray(UV);
		glBindBuffer(GL_ARRAY_BUFFER,0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

		call_forward(grpc::render, tfunc::void_void); // that is doing nothing right now
		// after this func ends then SwapBuffers(win->render_device) will be called
	}
}


void img_init();
void graphic_init()
{
	register_forward( "frame", func_add(graphic_frame) );
	glClearColor(0,0,0,0);
	glEnable(GL_DEPTH_TEST);

	grpc::modelMatrix = glm::mat4(1.0);
	grpc::viewMatrix = glm::mat4(1.0);
	grpc::projectionMatrix = glm::mat4(1.0);
	grpc::modelViewMatrix = glm::mat4(1.0);
	grpc::modelViewProjectionMatrix = glm::mat4(1.0);
	grpc::normalMatrix = glm::mat3(1.0);

	grpc::window = get_classwindow();
	create_forward("render", "void()");
	grpc::render = get_forward("render");

        // this function should load shaders correctly, i debugged everything before i started working on this.
        // it loads test.vs and test.frag and compiles it and fills the simple.index with index = glCreateProgram();
	grpc::simple.shader_load( "test", "shaderdetails..." );

	glBindFramebuffer(GL_FRAMEBUFFER,0);
	img_init();

	vertexData quad[6];
	quad[5].position[0] = 0.0f;
	quad[5].position[1] = 0.0f;
	quad[5].position[2] = -1.0f;
	quad[5].normal[0] = -1.0f;
	quad[5].normal[1] = -0.0f;
	quad[5].normal[2] = 0.0f;
	quad[5].uv[0] = 1.0f;
	quad[5].uv[1] = 0.0f;

	quad[4].position[0] = 0.0f;
	quad[4].position[1] = 1.0f;
	quad[4].position[2] = -1.0f;
	quad[4].normal[0] = -1.0f;
	quad[4].normal[1] = -0.0f;
	quad[4].normal[2] = 0.0f;
	quad[4].uv[0] = 0.0f;
	quad[4].uv[1] = 0.0f;

	quad[3].position[0] = 0.0f;
	quad[3].position[1] = 1.0f;
	quad[3].position[2] = 0.0f;
	quad[3].normal[0] = -1.0f;
	quad[3].normal[1] = -0.0f;
	quad[3].normal[2] = 0.0f;
	quad[3].uv[0] = 0.0f;
	quad[3].uv[1] = 1.0f;

	quad[2].position[0] = 0.0f;
	quad[2].position[1] = 0.0f;
	quad[2].position[2] = 0.0f;
	quad[2].normal[0] = -1.0f;
	quad[2].normal[1] = -0.0f;
	quad[2].normal[2] = 0.0f;
	quad[2].uv[0] = 1.0f;
	quad[2].uv[1] = 1.0f;

	quad[1].position[0] = 0.0f;
	quad[1].position[1] = 0.0f;
	quad[1].position[2] = -1.0f;
	quad[1].normal[0] = 1.0f;
	quad[1].normal[1] = 0.0f;
	quad[1].normal[2] = 0.0f;
	quad[1].uv[0] = 1.0f;
	quad[1].uv[1] = 0.0f;

	quad[0].position[0] = 0.0f;
	quad[0].position[1] = 1.0f;
	quad[0].position[2] = 0.0f;
	quad[0].normal[0] = -1.0f;
	quad[0].normal[1] = -0.0f;
	quad[0].normal[2] = 0.0f;
	quad[0].uv[0] = 0.0f;
	quad[0].uv[1] = 1.0f;

	glGenBuffers(1,&vbo);
	glBindBuffer(GL_ARRAY_BUFFER,vbo);
	glBufferData(GL_ARRAY_BUFFER,6*sizeof(vertexData), &quad,GL_STATIC_DRAW);
	vertexID id[2];
	id[0].id[0] = 0;
	id[0].id[1] = 1;
	id[0].id[2] = 2;
	id[1].id[0] = 0;
	id[1].id[1] = 2;
	id[1].id[2] = 3;
	
	glGenBuffers(1,&vbo2);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo2);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,6*sizeof(unsigned int),&id,GL_STATIC_DRAW);
	
	glBindBuffer(GL_ARRAY_BUFFER,0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}

shaders


// frag
void main()
{
	gl_FragColor=vec4(1.0,0.0,0.0,1.0);

}

// vertex
attribute vec3 vertex;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec3 color;
attribute vec2 UV;

varying vec3 outNormal;
varying vec3 outTangent;
varying vec3 outColor;
varying vec2 outUV;
varying vec3 position;


uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
	gl_Position=modelViewProjectionMatrix*vec4(vertex,1.0);
	position=vec3(modelViewMatrix*vec4(vertex,1.0));
	outNormal=normalMatrix*normal;
	outTangent=normalMatrix*outTangent;
	outColor=color;
	outUV=UV;

}

The problem is that only thing i see is black screen, why?

Advertisement

Change the translation for your view matrix to 0,0,-10 and create the positions for the quad like this:


//  x   y   z
// ----------
// -1, -1,  0  bottom left
//  1, -1,  0  bottom right
//  1,  1,  0  top right
// -1,  1,  0  top left
quad[0].position[0] = -1;
quad[0].position[1] = -1;
quad[0].position[2] =  0;
quad[1].position[0] =  1;
quad[1].position[1] = -1;
quad[1].position[2] =  0;
quad[2].position[0] =  1;
quad[2].position[1] =  1;
quad[2].position[2] =  0;
quad[3].position[0] = -1;
quad[3].position[1] =  1;
quad[3].position[2] =  0;

And the reason why you're getting -1 from glGetUniformLocation and glGetAttribLocation is that your shader program doesn't actually use those variables for anything, so they are optimized out. It still "fine" to call glUniformX(-1, ...) though, it shouldn't just do anything.

Derp


	vertexData quad[6];
	quad[5].position[0] = 0.0f;
	quad[5].position[1] = 0.0f;
	quad[5].position[2] = -1.0f;
	quad[5].normal[0] = -1.0f;
	quad[5].normal[1] = -0.0f;
	quad[5].normal[2] = 0.0f;
	quad[5].uv[0] = 1.0f;
	quad[5].uv[1] = 0.0f;

	quad[4].position[0] = 0.0f;
	quad[4].position[1] = 1.0f;
	quad[4].position[2] = -1.0f;
	quad[4].normal[0] = -1.0f;
	quad[4].normal[1] = -0.0f;
	quad[4].normal[2] = 0.0f;
	quad[4].uv[0] = 0.0f;
	quad[4].uv[1] = 0.0f;

	quad[3].position[0] = -1.0f;
	quad[3].position[1] =  1.0f;
	quad[3].position[2] =  0.0f;
	quad[3].normal[0] = -1.0f;
	quad[3].normal[1] = -0.0f;
	quad[3].normal[2] = 0.0f;
	quad[3].uv[0] = 0.0f;
	quad[3].uv[1] = 1.0f;

	quad[2].position[0] =  1.0f;
	quad[2].position[1] =  1.0f;
	quad[2].position[2] =  0.0f;
	quad[2].normal[0] = -1.0f;
	quad[2].normal[1] = -0.0f;
	quad[2].normal[2] = 0.0f;
	quad[2].uv[0] = 1.0f;
	quad[2].uv[1] = 1.0f;

	quad[1].position[0] =  1.0f;
	quad[1].position[1] = -1.0f;
	quad[1].position[2] =  0.0f;
	quad[1].normal[0] = 1.0f;
	quad[1].normal[1] = 0.0f;
	quad[1].normal[2] = 0.0f;
	quad[1].uv[0] = 1.0f;
	quad[1].uv[1] = 0.0f;

	quad[0].position[0] = -1.0f;
	quad[0].position[1] = -1.0f;
	quad[0].position[2] =  0.0f;
	quad[0].normal[0] = -1.0f;
	quad[0].normal[1] = -0.0f;
	quad[0].normal[2] = 0.0f;
	quad[0].uv[0] = 0.0f;
	quad[0].uv[1] = 1.0f;

// and
grpc::viewMatrix *= glm::translate(glm::vec3(0.0, 0.0, -10.0));

now that quad is everywhere, no matter what angle or pos, quad is in screen, thats weird.

Also, i wanted that the quad is made of 2 triangles. ( in case that was misunderstood )

The quad is made from 2 triangles, and you need 4 vertices to make a quad. You need 6 indices to create 2 triangles, and that's what you are doing correctly with the index buffer. First triangle is made from vertices 0, 1, 2 and the second triangle is made from vertices 0, 2, 3. In this case the vertices 4 and 5 are unnecessary.

Derp

fixed that now the problem is:

grpc::viewMatrix *= glm::translate

no matter what values i bring there the quad is still on screen ( quad covers full screen, always )

You're transforming the vertices by modelViewProjectionMatrix, but I don't see code for calculating that matrix. You are probably missing that.

Derp

You're transforming the vertices by modelViewProjectionMatrix, but I don't see code for calculating that matrix. You are probably missing that.

oh yea, thank you. its working now


		grpc::projectionMatrix = glm::perspective(50.0, size[0]/size[1], 1.0, 9999.0);
		grpc::modelMatrix = glm::mat4(1.0);
		grpc::viewMatrix = glm::mat4(1.0);
		
		grpc::viewMatrix *= glm::rotate(0.0f, glm::vec3(1.0, 0.0, 0.0));
		grpc::viewMatrix *= glm::rotate(0.0f, glm::vec3(0.0, 1.0, 0.0));
		grpc::viewMatrix *= glm::rotate(0.0f, glm::vec3(0.0, 0.0, 1.0));
		grpc::viewMatrix *= glm::translate(glm::vec3(0.0, 0.0, -300.0));

		grpc::modelViewMatrix=grpc::viewMatrix*grpc::modelMatrix;
		grpc::modelViewProjectionMatrix=grpc::projectionMatrix*grpc::viewMatrix*grpc::modelMatrix;
		grpc::normalMatrix=glm::mat3(grpc::modelViewProjectionMatrix);

This topic is closed to new replies.

Advertisement