• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Swatchoos
       
       

       
      Hi Guys,
      We would like to present our new panel: Swatchoos, the most advanced swatch panel for Adobe Photoshop CC with lots of features and improvements like:
      - Group Creation and Management
      - Swatch and Group Reordering
      - Change brightness and darkness of any color
      - Sort Swatches by Luminosity or HUE
      - Fill any Shape/Text/Bitmap layer
      - Generate Swatches from bitmaps and .psd layers
      - Customisable the look of your color
      - Import .aco or bitmaps
      - Share your swatch palettes with others
      - Auto color naming
      and much much more.
       
      You can try it for free so go to www.swatchoos.com and test it out!
      SWATCHOOS_3dtotal_movie.mp4
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System; using System.Collections.Generic; using System.IO; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> vertices; private static List<Vector2> textures; private static List<Vector3> normals; private static List<int> indices; private static float[] verticesArray; private static float[] normalsArray; private static float[] texturesArray; private static int[] indicesArray; private static string[] lines; public static RawModel LoadObjModel(string filename, Loader loader) { if (!File.Exists("Contents/" + filename + ".obj")) { throw new FileNotFoundException("Error: wavefront file doesn't exist path: " + filename + ".png"); } vertices = new List<Vector3>(); textures = new List<Vector2>(); normals = new List<Vector3>(); indices = new List<int>(); lines = File.ReadAllLines("Contents/" + filename + ".obj"); try { foreach (string line in lines) { if (line == "" || line.StartsWith("#")) continue; string[] token = line.Split(' '); switch(token[0]) { case ("o"): string o = token[1]; break; case "v": Vector3 vertex = new Vector3(float.Parse(token[1]), float.Parse(token[2]), float.Parse(token[3])); vertices.Add(vertex); break; case "vn": Vector3 normal = new Vector3(float.Parse(token[1]), float.Parse(token[2]), float.Parse(token[3])); normals.Add(normal); break; case "vt": Vector2 texture = new Vector2(float.Parse(token[1]), float.Parse(token[2])); textures.Add(texture); break; case "f": texturesArray = new float[vertices.Count * 2]; normalsArray = new float[vertices.Count * 3]; verticesArray = new float[vertices.Count * 3]; indicesArray = new int[indices.Count]; int vertexPointer = 0; foreach (Vector3 vex in vertices) { verticesArray[vertexPointer++] = vex.X; verticesArray[vertexPointer++] = vex.Y; verticesArray[vertexPointer++] = vex.Z; } for (int i = 0; i < indices.Count; i++) { indicesArray[i] = indices[i]; } break; } } } catch (FileNotFoundException f) { throw new FileNotFoundException($"OBJ file not found: {f.FileName}", f); } catch (ArgumentException ae) { throw new ArgumentException("OBJ file is damaged", ae); } return loader.loadToVAO(verticesArray, texturesArray, indicesArray); } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
    • By otgs17
      You have 12 hours to search for supplies, transportation or you can continue your journey.
      But as soon as night falls, you must find shelter, turn it into a fortress to stand against the hordes of monsters.
      Equip soldiers, use bonuses and do not forget to follow the energy level of your fighters. 
      In battle be useful to your transport, it would be nice to find a tank.
      Every day the number of monsters to get more, they mutate into stronger and more dangerous forms.
      https://play.google.com/store/apps/details?id=com.otgs.FortTD&hl=en
      https://itunes.apple.com/us/app/fortress-td/id1367094965?mt=8
      https://www.youtube.com/watch?v=QIkXPikiRqc





    • By Masson
      Name: Dodging Dots
      Category: Arcade/Casual
      Description:
      Come up with your own strategy. Collect myriads of dots and maneuver from the coming blocks. Beware! There gonna be lots of them!
      The minimalistic world will captivate you for a long time. Try out!
      Features:
      Use upgrades to improve your game experience 🎮 Increase the velocity 🏄 of the dots to avoid the avalanche of blocks. Or you can increase the number of falling boosters to break 💥 through it, the choice is yours! Nice color shades that change throughout the game Ongoing game. Original soundtrack 🎧 Integration with Google Play Games Compete with other players 🏆 18 achievements to warm up your interest 🔥 Simple control, play with one finger Google Play: https://play.google.com/store/apps/details?id=com.groovin.blocks.delicate.survive.gameplay
       
      Feel free to leave comments. We really interested in improving this product and appreciate your support.
    • By khawk
      Pong Challenge!
      Make a single player Pong, with a twist. 

       
      Game Requirements
      The game must have: Start screen Key to return to the start screen Score system AI Player Graphics at least for the UI and players Sound effects for ball and paddle and when a player scores A unique gameplay element that makes your Pong game stand out from others The actual gameplay must happen on a single screen (no camera translation) Art Requirements
      The game must be 2D The art must be your own Duration
      4 weeks - November 1, 2017 to November 30, 2017
      Submission
      Post on this thread your entries:
      Link to the executable (specifying the platform) Screenshots: if the screenshots are too big, post just a few, if they are small, you can post more, just don't take the entire space A small post-mortem in a GameDev.net Blog, with a link posted in this thread, is encouraged, where you can share what went right, what went wrong, or just share a nifty trick Source-code link is encouraged
  • Advertisement
  • Advertisement

OpenGL How to render multiple objects with different shaders

Recommended Posts

I'm looking to render multiple objects (rectangles) with different shaders. So far I've managed to render one rectangle made out of 2 triangles and apply shader to it, but when it comes to render another I get stucked. Searched for documentations or stuffs that could help me, but everything shows how to render only 1 object. Any tips or help is highly appreciated, thanks!

Here's my code for rendering one object with shader!

 

	#define GLEW_STATIC
	
	#include <stdio.h>
	#include <GL/glew.h>
	#include <GLFW/glfw3.h>
	
	#include "window.h"
	
	#define GLSL(src) "#version 330 core\n" #src
	// #define ASSERT(expression, msg) if(expression) {fprintf(stderr, "Error on line %d: %s\n", __LINE__, msg);return -1;}
	
	int main() {
	    // Init GLFW
	    if (glfwInit() != GL_TRUE) {
	        std::cerr << "Failed to initialize GLFW\n" << std::endl;
	        exit(EXIT_FAILURE);
	    }
	    // Create a rendering window with OpenGL 3.2 context
	    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
	    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
	
	    // assing window pointer
	    GLFWwindow *window = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL);
	    glfwMakeContextCurrent(window);
	    // Init GLEW
	    glewExperimental = GL_TRUE;
	    if (glewInit() != GLEW_OK) {
	        std::cerr << "Failed to initialize GLEW\n" << std::endl;
	        exit(EXIT_FAILURE);
	    }
	    // ----------------------------- RESOURCES ----------------------------- //
	
	    // create gl data
	    const GLfloat positions[8] = {
	        -0.5f, -0.5f,
	         0.5f, -0.5f,
	         0.5f,  0.5f,
	        -0.5f, 0.5f,
	    };
	
	    const GLuint elements[6] = {
	        0, 1, 2,
	        2, 3, 0
	    };
	
	    // Create Vertex Array Object
	    GLuint vao;
	    glGenVertexArrays(1, &vao);
	    glBindVertexArray(vao);
	
	    // Create a Vertex Buffer Object and copy the vertex data to it
	    GLuint vbo;
	    glGenBuffers(1, &vbo);
	    glBindBuffer(GL_ARRAY_BUFFER, vbo);
	    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
	    // Specify the layout of the vertex data
	    glEnableVertexAttribArray(0); // layout(location = 0)
	    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
	    
	    // Create a Elements Buffer Object and copy the elements data to it
	    GLuint ebo;
	    glGenBuffers(1, &ebo);
	    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
	    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements),  elements, GL_STATIC_DRAW);
	    
	
	    // Create and compile the vertex shader
	    const GLchar *vertexSource = GLSL(
	        layout(location = 0) in vec2 position;
	        
	        void main() {
	            gl_Position = vec4(position, 0.0, 1.0);
	        }
	    );
	
	    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	    glShaderSource(vertexShader, 1, &vertexSource, NULL);
	    glCompileShader(vertexShader);
	
	    // Create and compile the fragment shader
	    const char* fragmentSource = GLSL(
	        out vec4 gl_FragColor;
	        uniform vec2 u_resolution;
	        
	        void main() {
	            vec2 pos = gl_FragCoord.xy / u_resolution;
	          
	            gl_FragColor = vec4(1.0);
	        }
	    );
	
	    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	    glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
	    glCompileShader(fragmentShader);
	
	    // Link the vertex and fragment shader into a shader program
	    GLuint shaderProgram = glCreateProgram();
	    glAttachShader(shaderProgram, vertexShader);
	    glAttachShader(shaderProgram, fragmentShader);
	    glLinkProgram(shaderProgram);
	    glUseProgram(shaderProgram);
	
	    // get uniform's id by name and set value
	    GLint uRes = glGetUniformLocation(shaderProgram, "u_Resolution");
	    glUniform2f(uRes, 800.0f, 600.0f);
	        
	    // ---------------------------- RENDERING ------------------------------ //
	
	    while(!glfwWindowShouldClose(window)) {
	        // Clear the screen to black
	        glClear(GL_COLOR_BUFFER_BIT);
	        glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
	        
	        // Draw a rectangle made of 2 triangles -> 6 vertices
	        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
	
	        // Swap buffers and poll window events
	        glfwSwapBuffers(window);
	        glfwPollEvents();
	    }
	
	    // ---------------------------- CLEARING ------------------------------ //
	
	    // Delete allocated resources
	    glDeleteProgram(shaderProgram);
	    glDeleteShader(fragmentShader);
	    glDeleteShader(vertexShader);
	    glDeleteBuffers(1, &vbo);
	    glDeleteVertexArrays(1, &vao);
	
	    return 0;
	}

 

Edited by alex1997

Share this post


Link to post
Share on other sites
Advertisement

Without seeing how you are currently doing it, I can only really give a generic example, but you basically just want to create all your shaders, select the shader you wish to use - render all items that use this shader, switch to the next shader, render all items that use that shader, and so on

Switching shaders can be expensive, so thats why you will want to do all items for each shader before moving to the next, rather then switching shaders depending on what your rendering at the time.

If you can show a small bit of code of how you are currently doing this, im sure you will get more specific anwers

Share this post


Link to post
Share on other sites
26 minutes ago, McGrane said:

Without seeing how you are currently doing it, I can only really give a generic example, but you basically just want to create all your shaders, select the shader you wish to use - render all items that use this shader, switch to the next shader, render all items that use that shader, and so on

Switching shaders can be expensive, so thats why you will want to do all items for each shader before moving to the next, rather then switching shaders depending on what your rendering at the time.

If you can show a small bit of code of how you are currently doing this, im sure you will get more specific anwers

Thanks! I've pasted my code above And yes, beofre rendering wanna attach the shader and when the objects are ready draw them.

Share this post


Link to post
Share on other sites

Yeah, so in the above, you are creating a shader- and then telling opengl to use this shader:

glUseProgram(shaderProgram);

If you wish to use another shader, you do the same as you have done above in creating, compiling and linking another shader. You may want to extend your above to read shaders from text files instead of implementing them in code, and maybe making a shader class to encapsulate all these methods to just make it easier for maintainability. However to just get this working as is, say you have two shaders, and have compiled the into shaderProgram1 and shaderProgram2 then too use them you could just do something like:

3 hours ago, alex1997 said:

glUseProgram(shaderProgram1);

// Draw a rectangle made of 2 triangles -> 6 vertices glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);

glUseProgram(shaderProgram2);

// Draw a rectangle made of 2 triangles -> 6 vertices glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);

If you haven't already, check out https://learnopengl.com/ there are sections on shaders, and code examples for how he has set up his shader classes - along with alot of other great information

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement