void element_BeforeRendering(object sender, Objects.RenderEventArgs e) { CSharpGL.Objects.Texture2D texture = element.texture; texture.Bind(); if (element.blend) { GL.Enable(GL.GL_BLEND); GL.BlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); } mat4 modelMatrix = mat4.identity(); mat4 viewMatrix = this.camera.GetViewMat4(); mat4 projectionMatrix = this.camera.GetProjectionMat4(); ShaderProgram shaderProgram = element.shaderProgram; shaderProgram.Bind(); shaderProgram.SetUniform(WholeFontTextureElement.strtex, texture.Name); shaderProgram.SetUniform(WholeFontTextureElement.strcolor, 1.0f, 1.0f, 1.0f, 1.0f); IMVP iMVP = element as IMVP; iMVP.SetShaderProgram(projectionMatrix * viewMatrix * modelMatrix); }
protected override void DoRender(RenderEventArgs e) { CSharpGL.Objects.Texture2D texture = this.texture; texture.Bind(); if (this.blend) { GL.Enable(GL.GL_BLEND); GL.BlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); } ShaderProgram shaderProgram = this.shaderProgram; shaderProgram.Bind(); GL.ActiveTexture(GL.GL_TEXTURE0); GL.Enable(GL.GL_TEXTURE_2D); texture.Bind(); shaderProgram.SetUniform(strtex, 0); shaderProgram.SetUniform(WholeFontTextureElement.strcolor, 1.0f, 1.0f, 1.0f, 1.0f); shaderProgram.SetUniformMatrix4("MVP", (projectionMatrix * viewMatrix * modelMatrix).to_array()); GL.BindVertexArray(vao[0]); GL.DrawArrays(this.mode, 0, this.vertexCount); GL.BindVertexArray(0); texture.Unbind(); shaderProgram.Unbind(); GL.BindTexture(GL.GL_TEXTURE_2D, 0); if (this.blend) { GL.Disable(GL.GL_BLEND); } }
public Texture2D GetBumpTexture() { if (!bumpTextureInitialized) { lock (synObj) { if (!bumpTextureInitialized) { if (!string.IsNullOrEmpty(this.BumpFilename)) { var texture = new Texture2D(); var bitmap = new System.Drawing.Bitmap(this.BumpFilename); texture.Initialize(bitmap); this.BumpTexture = texture; } bumpTextureInitialized = true; } } } return this.BumpTexture; }
private void CreateTextureObject(FontTexture fontTexture) { this.texture = new Texture2D(); this.texture.Initialize(fontTexture.BigBitmap); }
private void toolStripItemOpenTexture_Click(object sender, EventArgs e) { if (openTextureDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.textureAdded = false; try { this.texture = new Texture2D(); this.textureImage = new Bitmap(openTextureDlg.FileName); this.texture.Initialize(this.textureImage); this.textureAdded = true; } catch (Exception ex) { this.textureAdded = false; MessageBox.Show(ex.Message); } } }
public void Render(ThreeDSModel4LegacyOpenGL model, PolygonModes mode) { if (TriangleIndexes == null) return; GL.PolygonMode(PolygonModeFaces.FrontAndBack, mode); // Draw every triangle in the entity foreach (var item in this.usingMaterialIndexesList) { var material = model.MaterialDict[item.Item1]; GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, material.Ambient); GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, material.Diffuse); GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, material.Specular); GL.Materialf(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, material.Shininess); Texture2D[] textures = new Texture2D[] { material.GetTexture(), material.GetBumpTexture(), material.GetReflectionTexture(), }; bool drawn = false; foreach (var texture in textures) { if (!(drawn && texture == null)) // 如果没有贴图,就只画一次。 { if (texture != null) { GL.Enable(GL.GL_TEXTURE_2D); texture.Bind(); } DrawTriangles(item, texture); if (texture != null) { texture.Unbind(); GL.Disable(GL.GL_TEXTURE_2D); } } drawn = true; } } if (this.usingMaterialIndexesList.Count == 0) { GL.Begin(GL.GL_TRIANGLES); foreach (var tri in this.TriangleIndexes) { // Vertex 1 if (normalized) { var normal = this.normals[tri.vertex1]; GL.Normal3d(normal.X, normal.Y, normal.Z); } { var vertex = this.Vertexes[tri.vertex1]; GL.Vertex3d(vertex.X, vertex.Y, vertex.Z); } // Vertex 2 if (normalized) { var normal = this.normals[tri.vertex2]; GL.Normal3d(normal.X, normal.Y, normal.Z); } { var vertex = this.Vertexes[tri.vertex2]; GL.Vertex3d(vertex.X, vertex.Y, vertex.Z); } // Vertex 3 if (normalized) { var normal = this.normals[tri.vertex3]; GL.Normal3d(normal.X, normal.Y, normal.Z); } { var vertex = this.Vertexes[tri.vertex3]; GL.Vertex3d(vertex.X, vertex.Y, vertex.Z); } } GL.End(); } //Console.WriteLine ( GL.GetError () ); }
private void DrawTriangles(Tuple<string, ushort[]> usingMaterialIndexes, Texture2D texture) { GL.Begin(GL.GL_TRIANGLES); foreach (var usingIndex in usingMaterialIndexes.Item2) { Triangle tri = this.TriangleIndexes[usingIndex]; // Vertex 1 if (normalized) { var normal = this.normals[tri.vertex1]; GL.Normal3d(normal.X, normal.Y, normal.Z); } if (texture != null) { var texCoord = this.TexCoords[tri.vertex1]; GL.TexCoord2f(texCoord.U, texCoord.V); } { var vertex = this.Vertexes[tri.vertex1]; GL.Vertex3d(vertex.X, vertex.Y, vertex.Z); } // Vertex 2 if (normalized) { var normal = this.normals[tri.vertex2]; GL.Normal3d(normal.X, normal.Y, normal.Z); } if (texture != null) { var texCoord = this.TexCoords[tri.vertex2]; GL.TexCoord2f(texCoord.U, texCoord.V); } { var vertex = this.Vertexes[tri.vertex2]; GL.Vertex3d(vertex.X, vertex.Y, vertex.Z); } // Vertex 3 if (normalized) { var normal = this.normals[tri.vertex3]; GL.Normal3d(normal.X, normal.Y, normal.Z); } if (texture != null) { var texCoord = this.TexCoords[tri.vertex3]; GL.TexCoord2f(texCoord.U, texCoord.V); } { var vertex = this.Vertexes[tri.vertex3]; GL.Vertex3d(vertex.X, vertex.Y, vertex.Z); } } GL.End(); }
protected override void DoInitialize() { skybox_prog = GL.CreateProgram(); ShaderHelper.vglAttachShaderSource(skybox_prog, ShaderType.VertexShader, skybox_shader_vs); ShaderHelper.vglAttachShaderSource(skybox_prog, ShaderType.FragmentShader, skybox_shader_fs); GL.LinkProgram(skybox_prog); object_prog = GL.CreateProgram(); ShaderHelper.vglAttachShaderSource(object_prog, ShaderType.VertexShader, object_shader_vs); ShaderHelper.vglAttachShaderSource(object_prog, ShaderType.FragmentShader, object_shader_fs); GL.LinkProgram(object_prog); GL.GenBuffers(1, cube_vbo); GL.BindBuffer(BufferTarget.ArrayBuffer, cube_vbo[0]); var cube_vertices = new UnmanagedArray<vec3>(8); cube_vertices[0] = new vec3(-1.0f, -1.0f, -1.0f); cube_vertices[1] = new vec3(-1.0f, -1.0f, 1.0f); cube_vertices[2] = new vec3(-1.0f, 1.0f, -1.0f); cube_vertices[3] = new vec3(-1.0f, 1.0f, 1.0f); cube_vertices[4] = new vec3(1.0f, -1.0f, -1.0f); cube_vertices[5] = new vec3(1.0f, -1.0f, 1.0f); cube_vertices[6] = new vec3(1.0f, 1.0f, -1.0f); cube_vertices[7] = new vec3(1.0f, 1.0f, 1.0f); GL.BufferData(BufferTarget.ArrayBuffer, cube_vertices, BufferUsage.StaticDraw); cube_vertices.Dispose(); GL.GenVertexArrays(1, vao); GL.BindVertexArray(vao[0]); GL.VertexAttribPointer(0, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero); GL.EnableVertexAttribArray(0); GL.GenBuffers(1, cube_element_buffer); GL.BindBuffer(BufferTarget.ElementArrayBuffer, cube_element_buffer[0]); GL.BufferData(BufferTarget.ElementArrayBuffer, cube_vertices, BufferUsage.StaticDraw); skybox_rotate_loc = GL.GetUniformLocation(skybox_prog, "tc_rotate"); object_mat_mvp_loc = GL.GetUniformLocation(object_prog, "mat_mvp"); object_mat_mv_loc = GL.GetUniformLocation(object_prog, "mat_mv"); skyboxTexLocation = GL.GetUniformLocation(skybox_prog, "tex"); objectTexLocation = GL.GetUniformLocation(object_prog, "tex"); var cube_indices = new UnmanagedArray<ushort>(16); // First strip cube_indices[0] = 0; cube_indices[1] = 1; cube_indices[2] = 2; cube_indices[3] = 3; cube_indices[4] = 6; cube_indices[5] = 7; cube_indices[6] = 4; cube_indices[7] = 5; // Second strip cube_indices[8] = 2; cube_indices[9] = 6; cube_indices[10] = 0; cube_indices[11] = 4; cube_indices[12] = 1; cube_indices[13] = 5; cube_indices[14] = 3; cube_indices[15] = 7; tex = new Texture2D(); //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"media\TantolundenCube.dds"); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"media\TantolundenCube.png"); tex.Initialize(bmp); vboObject.LoadFromVBM(@"media\unit_torus.vbm", 0, 1, 2); }