# OpenGL passing tex coords to shader problem

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

## Recommended Posts

Hi,

I'm new to openGL and I'm trying to make a md2 model loader (old format i know but its simple) i've got the model on screen but I can't texture it correctly. instead parts of the texture appear on the model but it seems to be the wrong bit. i know i am reading in the texcoords correctly and i know my pixel data is fine as i have tested this. thus i belive my problem is passing in the texcoord data to the shaders.

I'm using C++, SDL and openGL.

here is some of my code

my draw method
 glUseProgram(program); glUniform3f(translate_uniform, translate[0], translate[1], translate[2]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures); glUniform1i(textures_uniform, 0); glEnableVertexAttribArray(position_attribute); glEnableVertexAttribArray(texcoord_attribute); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glVertexAttribPointer( position_attribute, /* attribute */ 3, /* size */ GL_FLOAT, /* type */ GL_FALSE, /* normalized? */ 0, /* stride */ (void*)0 /* array buffer offset */ ); glBindBuffer(GL_ARRAY_BUFFER, tex_coord_buffer); glVertexAttribPointer( texcoord_attribute, 2, GL_FLOAT, GL_FALSE, 0, (void*)0 ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); glDrawElements( element_type, /* mode */ 3 * this->num_trigs, /* count */ GL_UNSIGNED_SHORT, /* type */ (void*)0 /* element array buffer offset */ ); glDisableVertexAttribArray(position_attribute); glDisableVertexAttribArray(texcoord_attribute);

my make resources method
 /* buffers */ vertex_buffer = make_buffer( GL_ARRAY_BUFFER, g_vertex_buffer_data, sizeof(GLfloat) * this->num_verts * 3 ); tex_coord_buffer = make_buffer( GL_ARRAY_BUFFER, g_texcoor_data, sizeof(GLfloat) * ((this->num_trigs * 3) * 2) //used to be number of texcoords*2 but i don't think that is correct as the texture coords are on each trig ); element_buffer = make_buffer( GL_ELEMENT_ARRAY_BUFFER, g_element_buffer_data, sizeof(GLushort) * this->num_trigs * 3 ); /* textures */ textures = make_texture(); if (textures == 0) return 0; /* shaders */ vertex_shader = make_shader( GL_VERTEX_SHADER, "shaders/hello-gl.v.glsl" ); if (vertex_shader == 0) return 0; fragment_shader = make_shader( GL_FRAGMENT_SHADER, "shaders/hello-gl.f.glsl" ); if (fragment_shader == 0) return 0; program = make_program( vertex_shader, fragment_shader ); if (program == 0) return 0; textures_uniform = glGetUniformLocation(program, "textures"); translate_uniform = glGetUniformLocation(program, "tx"); position_attribute = glGetAttribLocation(program, "position"); texcoord_attribute = glGetAttribLocation(program, "texcoord"); return 1;

if anyone can help I'd be really thankful as I'm tearing my hair out trying to work out what is wrong.

##### Share on other sites
You have to make sure that each vertex has a corresponding texcoord. In other words, if you have 5 vertices (5 *3 = 15), then you also need 5 texcoord (5*2 = 10). You might want to check out
((this->num_trigs * 3) * 2 to see if it makes sense.

##### Share on other sites
Thanks for the response. I have looked at this more and the problem seems to come from the fact that some vertices have more then one texcoords. Which depends on what triangle its is. ie one bertex that is the join of two triangle could have two texcoords. how can this be draw on screen using modern opengl?

##### Share on other sites
It is not possible to draw it. A vertex is not just a vertex. It is a vertex, normal, texcoord0, texcoord2, tangent, weight and so on. A vertex is a group. So duplicate that vertex with 2 different texcoord0.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 96
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009644
• ### Who's Online (See full list)

There are no registered users currently online

×