Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 10 Feb 2011
Offline Last Active Mar 03 2013 01:29 PM

Topics I've Started

Texturing Problem using OpenTK/OpenGL 3.3

26 February 2013 - 03:54 PM

I am having troubles getting texturing working in my OpenTK/OpenGL 3.3 code. I am sure it is something simple that I am missing, but I just can't seem to get a texture on the square I am rendering. (I'm writing a rendering framework for a game).

The relevant code pieces are:

//setup Texcoord array.
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject[2]);
GL.BufferData<float>(BufferTarget.ArrayBuffer, (IntPtr)(sizeof(float) * texcoord_information.Count), texcoord_information.ToArray(), BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
//Set up our textures
for(int i = 0; i < Textures.Count; i++)
    Console.WriteLine("Preparing texture.");
    Uniform_To_Texture[GL.GetUniformLocation(Shader.GetProgram(), "diffuse")] = Textures[i];

int texture_unit = 0;
foreach (KeyValuePair<int, ITexture> kvp in Uniform_To_Texture)
    GL.Uniform1(kvp.Key, texture_unit);
    GL.ActiveTexture(TextureUnit.Texture0 + texture_unit);
    GL.BindTexture(TextureTarget.Texture2D, kvp.Value.GetTextureHandle());
if (!GL.IsVertexArray(VertexArrayObject))
    throw new OpenGLException("Vertex Array Object not set up correctly, cannot render!");
GL.DrawArrays(BeginMode.Triangles, 0, Vertices.Count);


Then the vertex and fragment shaders are:

//Vertex shader
#version 330
uniform mat4 viewmatrix, projmatrix, transformmatrix;

in vec3 position;
in vec3 normal;
in vec2 texcoord;

varying vec2 texturecoord;

void main()
    texturecoord = texcoord;
    gl_Position = projmatrix * viewmatrix * transformmatrix * vec4(position, 1.0) ;

//Fragment Shader
#version 330
uniform sampler2D diffuse;
varying vec2 texturecoord;

out vec4 color;
void main(void)
    color = texture2D(diffuse, texturecoord);


The square I am rendering is just coming up white, if that helps any. 

Problem With VBOs

10 February 2011 - 02:16 AM

Hello everyone,
It's been a long while since I last posted here, so long that I forgot my old username/password. Ah well.

I am very far behind in OpenGL spec, and am getting off the fixed function pipeline. I've been trying to get Vertex Buffer Objects to render to no avail. Could someone tell me what is wrong with this code?

//m_indices is a boost::shared_array<unsigned int>
//m_vertices is a boost::shared_array<vertexinformation> where vertex information is:
struct vertexinformation
   	float px, py, pz;
   	float nx, ny, nz;
   	float t1x, t1y;
   	float t2x, t2y;
   	float t3x, t3y;
   	float t4x, t4y;
   	float t5x, t5y;

//This is in my initialization
glGenBuffers(1, &m_vertexbufferobject);
glGenBuffers(1, &m_indexbufferobject);

glBindBuffer(GL_ARRAY_BUFFER, m_vertexbufferobject);
glBufferData(GL_ARRAY_BUFFER, m_numberofvertices * 16 * sizeof(float), m_vertices.get(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexbufferobject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_numberofindices * sizeof(unsigned int), m_indices.get(), GL_STATIC_DRAW);

//Render Code
glBindBuffer(GL_ARRAY_BUFFER, m_vertexbufferobject);
glVertexPointer(3, GL_FLOAT, 64, 0);
glNormalPointer(GL_FLOAT, 64, BUFFER_OFFSET(3*sizeof(float)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexbufferobject);
glDrawElements(GL_TRIANGLES, m_numberofindices, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

What it should be drawing is a quad consisting of two triangles. The vertices specified are the four corners, and the indices specified are the two triplets of vertices to create the triangle.

Can anyone see anything wrong with any of this?