Jump to content

  • Log In with Google      Sign In   
  • Create Account

Frostraver

Member Since 27 Mar 2012
Offline Last Active Dec 18 2014 01:44 PM

Topics I've Started

Rendering multiple 2D objects

04 December 2014 - 01:23 PM

I'm trying to render multiple objects in my OpenGL based scene. I got one working with the following code:

This first piece of code is in the Initialize part of the object so it's only run once.

// Create Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo); // Generate 1 buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);	// Set vbo as active
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);	// load vertex data to vbo

GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

m_Program.CompileShaders("Assets/Shaders/Colorshading.vert", "Assets/Shaders/ColorShading.frag");
m_Program.LinkShaders();
glUseProgram(m_Program.GetID());

glBindFragDataLocation(m_Program.GetID(), 0, "outColor");

GLint posAttrib = glGetAttribLocation(m_Program.GetID(), "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);

GLint colAttrib = glGetAttribLocation(m_Program.GetID(), "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));

GLint texAttrib = glGetAttribLocation(m_Program.GetID(), "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

int width, height;
unsigned char* image = SOIL_load_image("Sample.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

After that, this is in the Draw method of the object which is run every tick:

// Render a test background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

// Bind the VAO
glBindVertexArray(vao);

//glUseProgram(m_Program.GetID());
// Draw the triangle !
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Starting from vertex 0; 3 vertices total -> 1 triangle

// CLear VAO binding
glBindVertexArray(0);

Now, I've done this in a second object, which is also drawn second because of the way the engine works and if I start the game I can only see the second object. I have no idea what I'm doing wrong. Am I binding the vbo for the object wrong? Am I doing something wrong in the drawing?

 

Also, a question paired to this one, how would I be able to change the layering of the seperate objects? Can I just change the z-value? That is what I have now but of course I can't see it.


Problem receiving packets

11 August 2013 - 01:08 PM

Hi everyone,

 

I'm trying to get my client to send a packet with some text and my server to receive the packet and display the text.

Currently I'm trying this locally so what I'm doing now is the following.

 

1) I send some text using sendto()

2) I try to receive the data using recvfrom()

 

The problem is that the recvfrom data doesn't seem to receive a packet. The two methods that I'm using are coded like this:

 

Used in step 1:

bool Networking::SendPacket(const void * message)
{
	// Set up address to send the packet to
	unsigned int a = 127;
    unsigned int b = 0;
    unsigned int c = 0;
    unsigned int d = 1;
    unsigned short port = 2890;

	unsigned int destination_address = ( a << 24 ) | ( b << 16 ) | ( c << 8 ) | d;
    unsigned short destination_port = port;

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl( destination_address );
    address.sin_port = htons(destination_port);

	// Try to send the packet
	int sent_bytes = sendto(handle, (const char*)message, sizeof(message), 0, (sockaddr*)&address, sizeof(sockaddr_in));

	// Check if the packet was sent successfully
    if (sent_bytes != sizeof(message))
    {
        printf("failed to send packet: return value = %d\n", sent_bytes);
        return false;
    }
	else
	{
		cout << "this packet was successfully sent: " << (const char*)message << "\n";
		return true;
	}

	return true;
}

Used in step 2:

void Networking::ReceivePacket()
{	
	unsigned char packet_data[256];

	#if PLATFORM == PLATFORM_WINDOWS
		typedef int socklen_t;
	#endif
			
	sockaddr_in from;
	socklen_t fromLength = sizeof( from );

	int received_bytes = recvfrom(handle, (char*)packet_data, sizeof(packet_data), 0, (sockaddr*)&from, &fromLength );

	if (received_bytes <= 0)
		return;

	cout << "RECEIVED DATA: " << (char*)packet_data << "\n";
}

Does anyone see what I'm doing wrong?

 

Thanks in advance


PARTNERS